Monday, September 19, 2011

Fading 2 LEDS Challenge.Using an Arduino!

This project is for beginners on arduino who want a challenge.(if it is a challenge!?!)
You have to create the fading led effect on 2 leds. But each led has to fade all the way off so then the fading on may start on the other led. Like So-


If you gave up or want the answer here is the source code(there might be other ways to do it, so if you can recreate the effect it is fine. comment below your answer)-

/* This source is for fading 2 leds one at a time
 As soon as one led is off the other will start to turn on
 */


int brightness=0; //sets the brightness of the led. it starts as off or 0
int change = 5; //change for bightness
int ledon = 6; // ledon is the led which is currently on
int ledchange = 1; // switches which led is going to turn on
int prevled =5; // represents the previous led that was on.

void setup()
{
  pinMode(5,OUTPUT); //led is on pin 5
  pinMode(6,OUTPUT); // anothe led on pin 6
}

void loop()
{

  analogWrite(ledon, brightness); // starts to set the led on
  brightness = brightness + change; // creates the fade effect

  if (brightness == 255){ //if brightness reaches 255 then
    change=-change;       // change will become negative and cause led to fade out
    ledchange=-ledchange; //this will switch which led is on
  }

if (brightness == 0){             //if brightness is 0 or nothing then
    ledon =ledchange+ledon;       //led which will turn on is changed
    prevled =ledon - ledchange;   //SETs the led which was previously on so 
    analogWrite(prevled,0);       // it will stay off without this the led will stay on
    change=-change;               // change, changes again to fade the led up.
  }

  delay(30);              //delay for a smooth effect
}





1 comment:

  1. Just wondering if you could provide a wiring schematic on how you wired up the Arduino up to the LEDs on the breadboard, it's been a while since i've done a little wiring and I'd like to give this cool project a go.

    Thanks.

    ReplyDelete