2 LED’s with Arduino

So for my first project with Arduino, I made 2 LED’s fade and get brighter in a loop.  Basically when one LED is at its brightest the other should be off, and vice versa.

It wasn’t too hard, and the example code provided with the compiler helped me to get started.

Ben Schoeler - 6/1/2012

Fade with 2 LED's

This example shows how to fade LED's on pins 9 and 10

The brightness of each LED will be opposite of the other LED

int yellow_led = 9;
int red_led = 10; // the pin that the LED is attached to
int yellow_brightness = 0; // how bright the LED is
int fadeAmount = 5; // how fast to change the LED's brightness
int red_brightness = 0;

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 and pin 10 to be outputs:
pinMode(yellow_led, OUTPUT);
pinMode(red_led, OUTPUT);

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9 (yellow):
analogWrite(yellow_led, yellow_brightness);

// set the brightness of pin 10 (red):
analogWrite(red_led, red_brightness);

// change the brightness for next time through the loop:
yellow_brightness = yellow_brightness + fadeAmount;

// reverse the direction of the fading at the ends of fade:
if (yellow_brightness == 0 || yellow_brightness == 255) {
fadeAmount = -fadeAmount;

//set red brightness to be the opposite of yellow brightness
red_brightness = 255 - yellow_brightness;

// wait for 30 milliseconds to see the dimming effect

I hope to do one of these Arduino projects every day.  I think tomorrow I’m going to set out working on an I2C interface with an LCD screen I have.

Sneak peak: