My first coding experience with an Uno is a vivid reminder why thorough planning is a key to successful completing projects. I have electronic parts laying around my room for another project and figured why not code my first sketch from using them. It basically takes a bi-color LED and fades from orange to red, then back to orange, and then green, and at last back to orange. It only requires couple of wires, a breadboard, 1k ohm resistor, and a bi-color LED.
My code is base off of the blink program included with the Arudino IDE, but for some odd reason I had trouble figuring out how do I do the switching fade. Also I came to a realization why those swiggles next to certain numbers, as described by Ben Heck in the video below…
Took awhile (half a day, WTF?), but I managed to complete it. Below is the code I created; feel free to modify it at your will. I’m thinking of creating an interrupt version for more efficient code.
Red, Green, and Orange Fade
4 Sep 2013
An add-on to the fade led example from Arduino
This incorporates a Bi-Color LED (Red and Green) to fade in and out
from red to green and vise-versa. This is realized by initializing the LEDs to maximum voltage, then
fading one color at a time.
Remember, because there’s a “varying” voltage the signal is very much analog requiring the LED to hookup
to a PWM port. This is why I am using pins 10 & 9.
Serial code is added for debugging purposes
Variables which can be viewed and used in any function at any given time
Remember they are global, so if you don’t want to have their values changed at all,
pre-syntax const (constant) before defined data type will retain it’s intial value for good.
const int green_led = 10; // green led assigned to pin 10
const int red_led = 9; // red led assigned to pin 9
const int int_bright = 255; // intially how bright the LED is
int light = int_bright;
int fadeAmount = -5;
boolean color_change = 1; // 0 = green, 1 = red
Defining functions within and outside of the scope of the project
void switchup(int bright);
// Intialization and Setup section
//setup serial communication at 9600 bps
// defines pins 10 & 9 as output pins
Serial.println(“Pins 8 and 9 are set to output”);
// Intiailize pins 10 & 9 to output voltage
// Serial.print(“Green and red LED outputting “);
void switchup(int bright)
if(bright == 0)
fadeAmount = -fadeAmount;
// main program loop
while(light <= 255)
if(color_change != true)
// Serial.print(“Green LED currently outtputing at “);
// Serial.print(“Red LED currently outtputing at “);
light += fadeAmount;
light = int_bright;
fadeAmount = -fadeAmount;
color_change = !color_change; //Flip to next color fade
If you have any questions or comments feel free to ask!