Monday, August 2, 2010

My First Program - Fade LED

The first thing I did when I got my Arduino Duemilanove from Hacktronics was of course open the box. Inside was the Arduino, a solderless breadboard, five 270 ohm resistors, six LED's in assorted colors, a micro pushbutton switch, an assortment of colored jumpers, a USB cable, and a bubblegum flavored dum-dum ($50 delivered).

I downloaded the Linux version of the Arduino software.

I plugged in the USB cable. The board comes preloaded with the blink sketch, and sure enough, it started winking at me. Ok, big deal, that's boring. So I connected Gnd and Digital pin 9 to the breadboard. Pin 9 goes to the + on the LED (anode), the - of the LED (cathode) connected to the 270 ohm resistor, and the other end of the resistor to Gnd. Then I loaded the Fade sketch as follows:
int ledPin = 9;
// LED connected to digital pin 9

void setup() {
// nothing happens in setup
}

void loop() {

// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
AnalogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30); }

// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

Now I have a LED that fades in and out :-)
Now on to bigger and better things.

No comments:

Post a Comment