Hey guys! I'm a new Arduino user and for a project in my computer science class, we have to take one of the openings projects (that Arduino provides - SIK circuits) and further develop them. I decided to do circuit #3- RGB LED. I intended to further the code by adding two more led lights and coding them all to do different color at a time. To do this, I thought it was as simple as just adding 3 more lights and copy and pasting certain codes for 2 more RGB lights, well that didn't work.
So i have attached a picture of my Arduino and the code below and hopefully you guys can help me out to see where I went wrong. Thanks so much that would be so helpful.
my code:
//light 1
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
const int DISPLAY_TIME = 1000; // used in mainColors() to determine the
// length of time each color is displayed.
//light 2
const int RED_PIN2 = 8;
const int GREEN_PIN2 = 6;
const int BLUE_PIN2 = 7;
const int DISPLAY_TIME2 = 1000; // used in mainColors() to determine the
// length of time each color is displayed.
//light 3
const int RED_PIN3 = 1;
const int GREEN_PIN3 = 2;
const int BLUE_PIN3 = 3;
const int DISPLAY_TIME3 = 1000; // used in mainColors() to determine the
// length of time each color is displayed.
void setup() //Configure the Arduino pins to be outputs to drive the LEDs
{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(RED_PIN2, OUTPUT);
pinMode(GREEN_PIN2, OUTPUT);
pinMode(BLUE_PIN2, OUTPUT);
pinMode(RED_PIN3, OUTPUT);
pinMode(GREEN_PIN3, OUTPUT);
pinMode(BLUE_PIN3, OUTPUT);
}
void loop()
{
mainColors(); // Red, Green, Blue, Yellow, Cyan, Purple, White
//showSpectrum(); // Gradual fade from Red to Green to Blue to Red
}
void mainColors()
{
// all LEDs off
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
digitalWrite(RED_PIN2, LOW);
digitalWrite(GREEN_PIN2, LOW);
digitalWrite(BLUE_PIN2, LOW);
delay(DISPLAY_TIME2);
digitalWrite(RED_PIN3, LOW);
digitalWrite(GREEN_PIN3, LOW);
digitalWrite(BLUE_PIN3, LOW);
delay(DISPLAY_TIME3);
// Red
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
digitalWrite(RED_PIN2, HIGH);
digitalWrite(GREEN_PIN2, LOW);
digitalWrite(BLUE_PIN2, LOW);
delay(DISPLAY_TIME2);
digitalWrite(RED_PIN3, HIGH);
digitalWrite(GREEN_PIN3, LOW);
digitalWrite(BLUE_PIN3, LOW);
delay(DISPLAY_TIME3);
// Green
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
digitalWrite(RED_PIN2, LOW);
digitalWrite(GREEN_PIN2, HIGH);
digitalWrite(BLUE_PIN2, LOW);
delay(DISPLAY_TIME2);
digitalWrite(RED_PIN3, LOW);
digitalWrite(GREEN_PIN3, HIGH);
digitalWrite(BLUE_PIN3, LOW);
delay(DISPLAY_TIME3);
// Blue
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
digitalWrite(RED_PIN2, LOW);
digitalWrite(GREEN_PIN2, LOW);
digitalWrite(BLUE_PIN2, HIGH);
delay(DISPLAY_TIME2);
digitalWrite(RED_PIN3, LOW);
digitalWrite(GREEN_PIN3, LOW);
digitalWrite(BLUE_PIN3, HIGH);
delay(DISPLAY_TIME3);
// Yellow (Red and Green)
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
digitalWrite(RED_PIN2, HIGH);
digitalWrite(GREEN_PIN2, HIGH);
digitalWrite(BLUE_PIN2, LOW);
delay(DISPLAY_TIME2);
digitalWrite(RED_PIN3, HIGH);
digitalWrite(GREEN_PIN3, HIGH);
digitalWrite(BLUE_PIN3, LOW);
delay(DISPLAY_TIME3);
// Cyan (Green and Blue)
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
digitalWrite(RED_PIN2, LOW);
digitalWrite(GREEN_PIN2, HIGH);
digitalWrite(BLUE_PIN2, HIGH);
delay(DISPLAY_TIME2);
digitalWrite(RED_PIN3, LOW);
digitalWrite(GREEN_PIN3, HIGH);
digitalWrite(BLUE_PIN3, HIGH);
delay(DISPLAY_TIME3);
// Purple (Red and Blue)
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
digitalWrite(RED_PIN2, HIGH);
digitalWrite(GREEN_PIN2, LOW);
digitalWrite(BLUE_PIN2, HIGH);
delay(DISPLAY_TIME2);
digitalWrite(RED_PIN3, HIGH);
digitalWrite(GREEN_PIN3, LOW);
digitalWrite(BLUE_PIN3, HIGH);
delay(DISPLAY_TIME3);
// White (turn all the LEDs on)
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
digitalWrite(RED_PIN2, HIGH);
digitalWrite(GREEN_PIN2, HIGH);
digitalWrite(BLUE_PIN2, HIGH);
delay(DISPLAY_TIME2);
digitalWrite(RED_PIN3, HIGH);
digitalWrite(GREEN_PIN3, HIGH);
digitalWrite(BLUE_PIN3, HIGH);
delay(DISPLAY_TIME3);
}
void showSpectrum()
{
for (int x = 0; x <= 767; x++)
{
RGB(x); // Increment x and call RGB() to progress through colors.
delay(10); // Delay for 10 ms (1/100th of a second) - to help the "smoothing"
}
void RGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;
color = constrain(color, 0, 767); // constrain the input value to a range of values from 0 to 767
// if statement breaks down the "color" into three ranges:
if (color <= 255) // RANGE 1 (0 - 255) - red to green
{
redIntensity = 255 - color; // red goes from on to off
greenIntensity = color; // green goes from off to on
blueIntensity = 0; // blue is always off
}
else if (color <= 511) // RANGE 2 (256 - 511) - green to blue
{
redIntensity = 0; // red is always off
greenIntensity = 511 - color; // green on to off
blueIntensity = color - 256; // blue off to on
}
else // RANGE 3 ( >= 512)- blue to red
{
redIntensity = color - 512; // red off to on
greenIntensity = 0; // green is always off
blueIntensity = 767 - color; // blue on to off
}
// "send" intensity values to the Red, Green, Blue Pins using analogWrite()
analogWrite(RED_PIN, redIntensity);
analogWrite(GREEN_PIN, greenIntensity);
analogWrite(BLUE_PIN, blueIntensity);
}
