Thursday, April 25, 2013

Counting code with button pressing action!!

For our Arduino code, we decided to attach the brute force 5 blink counting code with the button pushing code. This way when you press the button, the code will start the blink counting. This will continue in a loop forever until you press the button again to turn the blinking off. So basically we made and ON/OFF to count to five.


Here's the extremely un-elegant code with the button pressing action:


const int buttonPin = 9;     // the number of pushbutton pin
const int led =  13;      // the number of LED pin

// variables that will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  
  // Pushbutton pin as the input
  pinMode(buttonPin, INPUT);     
  
  // Make the LED pin an output
  pinMode(led, OUTPUT);      
}

void loop(){

  buttonState = digitalRead (buttonPin); // read the state of the pushbutton value:

  if (buttonState == HIGH) {    // BUTTON PRESS
  
  //BLINK ONCE
  digitalWrite(led, HIGH);   // ONE
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off
  delay(1000); 
  
  //BLINK TWICE
  digitalWrite (led, HIGH);   //   ONE
  delay(500);               // wait for a second
  digitalWrite (led, LOW);    // turn the LED off 
  delay(500);               // wait for a second
  digitalWrite(led, HIGH);   //   TWO
  delay(500);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off 
  delay(1000);               // wait for a second
  
  //BLINK THREE TIMES
  digitalWrite(led, HIGH);     //   ONE 
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);     // turn the LED off 
  delay(500);                 // wait for a second
  digitalWrite(led, HIGH);    //   TWO
  delay(500);                // wait for a second
  digitalWrite(led, LOW);     // turn the LED off 
  delay(500);                 // wait for a second
  digitalWrite(led, HIGH);   //   THREE
  delay(500);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off 
  delay(1000);              // wait for a second
  
  //BLINK FOUR TIMES
  digitalWrite(led, HIGH);   //   ONE
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);    // turn the LED off 
  delay(500);                 // wait for a second
  digitalWrite(led, HIGH);    //   TWO
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);     // turn the LED OFF 
  delay(500);                 // wait for a second
  digitalWrite(led, HIGH);    //   THREE
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);    // turn the LED off 
  delay(500);                 // wait for a second
    digitalWrite(led, HIGH);   //   FOUR 
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);     // turn the LED off 
  delay(1000);               // wait for a second

  //BLINK FIVE TIMES
  digitalWrite(led, HIGH);     //   ONE
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);      // turn the LED off 
  delay(500);                 // wait for a second
  digitalWrite(led, HIGH);    //   TWO
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);     // turn the LED OFF 
  delay(500);                 // wait for a second
  digitalWrite(led, HIGH);    //   THREE
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);     // turn the LED off 
  delay(500);                 // wait for a second
    digitalWrite(led, HIGH);  //   FOUR 
  delay(500);                 // wait for a second
  digitalWrite(led, LOW);     // turn the LED off 
  delay(500);                 // wait for a second
    digitalWrite(led, HIGH);   //   FIVE 
  delay(1000);                 // wait for a second
  digitalWrite(led, LOW);     // turn the LED off 
  
  } 
  
 //Push the button again
  else {
    digitalWrite(led, LOW);     // LIGHT OFF
  }
}







Here is the code for the more elegant way of doing the counting loop, but we didn't include the button push:


const int led =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(led, OUTPUT);
}

void loop() {
  int i = 1;  //counter variable
  int j = 1;  //counter variable

  for(i=1; i<6; ++i){ // tells the LED to blink 5 times
    for(j=1; j<=i; ++j){ //tells the LED to restrict the number of blinks to the value of i/j
      digitalWrite(led, HIGH);   // 1 Flash
      delay(200);
      digitalWrite(led, LOW);
      delay(400);
    }
  delay(1000);
  }
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(led, ledState);
  }
}


No comments:

Post a Comment