I am Arduino. I come in pieces.

Now that I’ve had my first PComp class, I can say that I’ve been formally introduced to Arduino. Pulling that little breadboard out of the box brought back memories of my 5th grade science project building computer circuits with my dad (though my teacher’s mention of voltage dividers brought back less pleasant memories of Electronics Lab in college). It’s pretty amazing to use one of these things, and a great way to get back in the habit of C coding. I read a piece in Wired a few months ago about a guy who used Arduino to build a light up suit that he controlled from his iPhone. I need to get the idea machine rolling so I can start thinking up stuff like that.

The first part of the assignment was pretty straightforward. I set up the board with so that the two outer outside columns were for ground and the two inner outside columns were for power. This allows more room to set up the wiring for LEDs and the switch. These components were all connected to power through pulldown resistors, and were each also connected to ground.

With the following code, I programmed the Arduino to leave the red LED on when the switch is open, and to turn it off while turning the green LED on if the switch is closed:

 [code lang="c"] void setup()
{
  pinMode(2, INPUT);  //switch at pin 2
  pinMode(3, OUTPUT);  //green LED at pin 3
  pinMode(4, OUTPUT);  //red LED at pin 4
}

void loop()
{
  if(digitalRead(2)==HIGH)  //switch closed
  {
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  }
  else  //switch open
  {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
  }
}
[/code]

Here’s the accompanying video to show how it works: