As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Thursday, April 25, 2013

Noble + Gavia | Blinking LED and Button Switch

Here's the code for incrementing the LED up to five blinks.  I merged it with the code for the button so every time you turn the LED on with the button it also runs the blinking loop.


/* Button Controlled Increment Blink to Five Hack */
#define LED 13 // the pin for the LED
#define BUTTON 7 // the input pin where the pushbutton is connected
int counter = 1;
int i;
int val = 0; // val will be used to store the state of the input pin
int old_val = 0; // this variable stores the previous value of "val"
int state = 0; // 0 = LED off and 1 = LED on


void setup() {
 pinMode(LED, OUTPUT); // tell Arduino LED is an output
 pinMode(BUTTON, INPUT); // and BUTTON is an input
}


void loop() {
   val = digitalRead(BUTTON); // read input value and store it
 
   if ((val == HIGH) && (old_val == LOW)){
     state = 1 - state;
     counter = 0; // Reset the counter so that you can run the for loop again when you press the button
     delay(10);
   }
 
   old_val = val; // val is now old, let's store it
 
   if (state == 1) { // If the button is telling the LED to turn on, run the blink loop
     while(counter <= 5) {
       for (i = 0; i < counter; i++) {
         digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
         delay(250);               // wait
         digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
         delay(250);               // wait
        }
        delay(1000);
        counter++;
      }
      digitalWrite(LED, HIGH);
    } else {
      digitalWrite(LED, LOW);
    }
}

No comments:

Post a Comment