Tuesday, September 9, 2008

Physical Computing Week 1 Homework

I finally got my Arduino Diecimila to work on my computer. I was really excited about programming in Arduino. It seemed like such a simple language that I just wanted to dive in and play around a bit.

I played with for loops allowing the LEDs to blink at either an exponentially increasing or decreasing amount. The LED that turns on depends on the position of a switch, whether it is open or closed. Here is my code:

// declare variables:
int switchPin = 2; // digital input pin for a switch
int yellowLedPin = 3; // digital output pin for an LED
int redLedPin = 4; // digital output pin for an LED
int switchState = 0; // the state of the switch

void setup() {
pinMode(switchPin, INPUT); // set the switch pin to be an input
pinMode(yellowLedPin, OUTPUT); // set the yellow LED pin to be an output
pinMode(redLedPin, OUTPUT); // set the red LED pin to be an output
}

void loop() {
switchState = digitalRead(switchPin); // read the switch input:

if (switchState == 1) // if the switch is closed:
for (int i=10; i >= 1; i--){
digitalWrite(yellowLedPin, HIGH); // turn on the yellow LED
delay(i*(i*10));
digitalWrite(yellowLedPin, LOW); // turn off the yellow LED
delay(i*(i*10));
}
digitalWrite(redLedPin, LOW); // turn off the red LED
}
else if (switchState == 0){ // if the switch is open:
for (int i=1; i <= 10; i++){
digitalWrite(redLedPin, HIGH); // turn on the red LED
delay(i*(i*10));
digitalWrite(redLedPin, LOW); // turn off the red LED
delay(i*(i*10));
}
digitalWrite(yellowLedPin, LOW); // turn off the yellow LED
}
}
Here is a video:


The LEDs blinked the way I wanted them to, but the switch wouldn't change the lit LED until after the currently lit one was finished its for loop. I tried adding an if statement with a break to get the for loop to stop, but it didn't work.

4 comments:

  1. pretty cool matt!

    just saw a post about a magazine cover with e-ink, which blinks graphics. thought i'd share it...
    http://www.cpluv.com/www/feeditem/6602

    ReplyDelete
  2. Matt, cool video. I am glad that you are learning this stuff. I was trying to tell my friend about your program, would you mind writing about it so I can just send him to the blog? Looking good!

    ReplyDelete
  3. I was looking over your code and wondered if you changed it slightly to say something similar to (rather than copying the whole thing):

    // obviously I have no idea about Arduino
    // programming, but would this work?
    void loop() {
    for (int i=10; i >= 1; i--){
    // read the switch input at the beginning of
    // each loop iteration
    switchState = digitalRead(switchPin);
    digitalWrite((ledPin + switchState), HIGH);
    delay(i*(i*10));
    digitalWrite((ledPin + switchState), LOW);
    delay(i*(i*10));
    }
    }

    Basically, you set ledPin to 3, if the switchState is 0, ledPin plus 0 is 3 which is controlling the yellow led. If, however, the switchState is 1, it adds 1 to the ledPin making it 4 which is the red led. That way it should work inside the for loop.

    Will this work? Obviously you will have to refactor the whole program, but it will end up making the code a little over half as long as it is now. Oh yeah, I am not sure if there is a switch statement or if you'll have to use an if else to check the value of switchState and if it is 0, making sure that the red led is off, else making sure the yellow led is off or low.

    If it works post video :)

    ReplyDelete
  4. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. homeworkdoer

    ReplyDelete