Introduction: Arduino Examples #1 Make an RGB Led Randomly Flash Different Colors

In this instructable I will show you how to get an rgb led to randomly flash different colors using an Arduino. If you have any questions, comments or suggestions for other Arduino examples please feel free to leave a comment and I will write back as soon as i can.

Step 1: Get the Hardware Required

Hardware Required
1-arduino
1-330ohm resistor
1-common cathode or common anode rgb led
1-breadboard
5-jumper wires

Step 2: Wire It Up (Common Cathode)

First connect a 330ohm resistor between the Arduino's ground and the rgb led's ground. Next connect the red pin to pin 11, green pin to pin 12, blue pin to pin 13. Then go to step 2 to program the Arduino.

Step 3: Program the Arduino (Common Cathode)

Now using your computer open up the Arduino software. Then connect your Arduino to your computer. Next copy and paste all the text between the //. Then upload the program to your Arduino and try it out.

// 
int ledcolor = 0;
int a = 1000; //this sets how long the stays one color for
int red = 11; //this sets the red led pin
int green = 12; //this sets the green led pin
int blue = 13; //this sets the blue led pin

void setup() { //this sets the output pins

pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

void loop() {
int ledcolor = random(7); //this randomly selects a number between 0 and 6

switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 204);
delay(a);
analogWrite(red, 0);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, HIGH);
delay(a);
digitalWrite(green, LOW);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(blue, LOW);
break;
case 3: //if ledcolor equals 3 then the led will turn yellow
analogWrite(red, 160);
digitalWrite(green, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
break;
case 4: //if ledcolor equals 4 then the led will turn cyan
analogWrite(red, 168);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(blue, LOW);
break;
case 5: //if ledcolor equals 5 then the led will turn magenta
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
case 6: //if ledcolor equals 6 then the led will turn white
analogWrite(red, 100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
}


//

Step 4: Wire It Up (Common Anode)

First connect a 330ohm resistor between the Arduino's 5v pin and the rgb led's positive pin. Next connect the red pin to pin 11, green pin to pin 12, blue pin to pin 13. Then go to step 5 to program the Arduino.

Step 5: Program the Arduino (Common Anode)

Now using your computer open up the Arduino software. Then connect your Arduino to your computer. Next copy and paste all the text between the //. Then upload the program to your Arduino and try it out.
//
int ledcolor = 0;
int a = 1000; //this sets how long the stays one color for
int red = 11; //this sets the red led pin
int green = 12; //this sets the green led pin
int blue = 13; //this sets the blue led pin

void setup() { //this sets the output pins

pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

void loop() {
int ledcolor = random(7); //this randomly selects a number between 0 and 6

switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 51);
delay(a);
analogWrite(red, 255);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, LOW);
delay(a);
digitalWrite(green, HIGH);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, LOW);
delay(a);
digitalWrite(blue, HIGH);
break;
case 3: //if ledcolor equals 3 then the led will turn yellow
analogWrite(red, 95);
digitalWrite(green, LOW);
delay(a);
analogWrite(red, 255);
digitalWrite(green, HIGH);
break;
case 4: //if ledcolor equals 4 then the led will turn cyan
analogWrite(red, 168);
digitalWrite(blue, LOW);
delay(a);
analogWrite(red, 255);
digitalWrite(blue, HIGH);
break;
case 5: //if ledcolor equals 5 then the led will turn magenta
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
delay(a);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
break;
case 6: //if ledcolor equals 6 then the led will turn white
analogWrite(red, 155);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
delay(a);
analogWrite(red, 255);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
break;
}

}
// 
If you have any questions, comments or suggestions for other Arduino examples please feel free to leave a comment and I will write back as soon as i can.