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.

Wednesday, April 24, 2013

Count sketch with a while or for loop; Button sketch — M. Simone; S. Churng

In this version of the Blink sketch, our code blinks an LED 5 times, then pauses 5 seconds, then goes back to blinking.

/*
  authors: sarah churng; mallika simone
  Turns on an LED on for one second, then off for one second, for a count of 5 times.
 */

// LED on pin 9
int ledPin = 9;                 // set LED connected to pin 9
int maxnum = 5;                 // set the max blink num to 5
int count = 0;                  // start the count at 0

void setup() {                
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);     
}

void loop() {
  for(count = 0; count < maxnum; count++){ // iterate 5 times
    digitalWrite(ledPin, HIGH);            // turn the LED on 
    delay(1000);                           // wait 1 second
    digitalWrite(ledPin, LOW);             // turn LED off
    delay(1000);                           // wait 1 second
  }
  delay(5000);                             // after 5 blinks, wait 5 seconds
}

Alternatively, a while loop can be used to count down from maxnum to 0:

void loop() {
  while(count < maxnum){                   // iterate until we count to 5
    digitalWrite(ledPin, HIGH);            // turn the LED on 
    delay(1000);                           // wait 1 second
    digitalWrite(ledPin, LOW);             // turn the LED off 
    delay(1000);                           // wait 1 second
    count++;                               // increase count
  }
  delay(5000);                             // after 5 blinks, wait 5 seconds
}

This is a photo of our button setup. The LED at pin 13 on the Arduino board lights up when the button is pushed.



No comments:

Post a Comment