Intro to Arduino – Part 5 – RGB LED and Moving into the Kit’s Tutorial

Ok, we are getting close to the point where you can get pretty much your entire lesson out of the tutorial document that came with your starter kit. In chapter 4 of the stock tutorial doc they teach you how to use another, much cooler type of LED that can display different colors.

There are going to be some slight differences still because you are working on a Chromebook using codebender to write and upload your code, while the document is going to be referencing a windows computer using a different code editor. Most of the steps are the same.

To complete this section I have a copy of chapter 4 from the stock tutorial for you to download here:

You will download this and go through the steps and instructions contained within.

When you get to the part where they talk about the code for this chapter, copy and paste this code below into an empty codebender window and upload it to your Arduino:

// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6

void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
}

// define variables
int redValue;
int greenValue;
int blueValue;

// main loop
void loop()
{
#define delayTime 10 // fading time between colors

redValue = 255; // choose a value between 1 and 255 to change the color.
greenValue = 0;
blueValue = 0;

// this is unnecessary as we've either turned on RED in SETUP
// or in the previous loop ... regardless, this turns RED off
// analogWrite(RED, 0);
// delay(1000);

for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
{
redValue -= 1;
greenValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(RED, 255 - redValue);
// analogWrite(GREEN, 255 - greenValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
delay(delayTime);
}

redValue = 0;
greenValue = 255;
blueValue = 0;

for(int i = 0; i < 255; i += 1) // fades out green bring blue full when i=255
{
greenValue -= 1;
blueValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(GREEN, 255 - greenValue);
// analogWrite(BLUE, 255 - blueValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
delay(delayTime);
}

redValue = 0;
greenValue = 0;
blueValue = 255;

for(int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
{
// The following code has been rearranged to match the other two similar sections
blueValue -= 1;
redValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(BLUE, 255 - blueValue);
// analogWrite(RED, 255 - redValue);
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
delay(delayTime);
}
}

If you get it working, you are doing great! If you run into problems, don’t be too discouraged. Check in with your coach or mentor to iron out any issues with your wiring and to make sure the code is working properly.

Once you get the RGB LED working, take a closer look at the code that was provided to control it. Feel free to change and tweak some things to see if you can change the speed or behavior of the code as we have done before with the board’s built-in LED.

Make note on any of the confusing parts of the code and ask your coach or mentor to help explain how certain sections work.

Once you are comfortable with this section you can move on to the next section where we will learn how to use buttons to control things.