July 22nd, 2008

Lesson 2: Button

Next, we’ll learn how to read a switch with the Arduino and use a button to control an LED.

You can run the following example code for this:

/*
* Button
* by DojoDave <http://www.0j0.org>
*
* Turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7.
*
* http://www.arduino.cc/en/Tutorial/Button
*/

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT);      // declare LED as output
pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
val = digitalRead(inputPin);  // read input value
if (val == HIGH) {            // check if the input is HIGH
digitalWrite(ledPin, LOW);  // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}

July 22nd, 2008

Lesson 1: Blink

The “hello world” of Arduino: getting an LED to blink.

In this example, plug the positive leg of the LED (the longer leg) into Pin 13, and the negative leg (shorter one) into Ground. Then, compile and upload the following code:

/*
* Blink
*
* The basic Arduino example. Turns on an LED on for one second,
* then off for one second, and so on… We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*
* http://www.arduino.cc/en/Tutorial/Blink
*/

int ledPin = 13; // LED connected to digital pin 13

void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop() // run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

Hooray! Blinky!

July 21st, 2008

Code and Examples

Here’s links for the code and examples used during today’s session:

Code

Presentation