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.

Friday, May 10, 2013

Maddz & Candz: Arduino Practice Codes

We used to code from the Arduino Example files and modified it to produce both the brute force and for loop code. Both codes essentially makes the LED blink five times (counting 1, 2, 3, 4, 5), then wait for a while, and begin counting again.

Blink Code (Brute Force)
// Used Pin 13 for the LED
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);     
}


// the loop routine runs over and over again forever:
void loop() {
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(500);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(500);   // wait for a second
 
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(500);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(500);   // wait for a second
 
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(500);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(500);   // wait for a second
 
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(500);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(500);   // wait for a second
 
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(500);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(500);   // wait for a second
 
//waits 1 minute before blinking again
 delay (60000);
}


Blink Code (For Loop)

// Used Pin 13 as LED
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
 // LED blinks so long as count is less than 5.
 for (int count=0; count<5; count++) {
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
   delay(500);               // wait for a second
   digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
   delay(500);   // wait for a second
 }
// Waits for 1 minute before blinking again.
 delay(60000);
}



Button
Using the example from http://www.arduino.cc/en/Tutorial/Button, we were able to get the button system working. Here is the code we used from the website:


// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

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

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);     
}

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

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {     
   // turn LED on:    
   digitalWrite(ledPin, HIGH);  
 }
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW);
 }
}


Below is a video showing the button at work:


No comments:

Post a Comment