Project 3 –Lighting up RGB LED’S
 |
Molly explaining to me that her Pony has RGB LED's in them that use similar programs that we wrote. |
Hardware concepts we learned were:
- ·
How a RGB LED has a Red, Green, and Blue diode
that can be used together to create any possible color
-
Programming concepts we learned were:
- ·
Constant vs global vs local variables
- ·
Using subroutines (functions)
- ·
Using a For..To.. Loop with conditional
statements
Our Program- 2
different programs. The first one lights
up each of the 9 “main” colors one at a time.
Then a “rainbow” program that goes though 768 different colors one by
once, gradually going through the rainbow, turning on and off faster than the
human eye can detect.
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int DISPLAY_TIME = 100;
void setup()
{ pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN,
OUTPUT);
pinMode(BLUE_PIN,
OUTPUT);}
void loop()
{mainColors();
showSpectrum();}
void mainColors()
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN,
HIGH);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);}
showSpectrum()
void showSpectrum()
{ int x;
for (x = 0; x <
768; x++)
{ showRGB(x);
delay(10); }}
void showRGB(int color)
{ int redIntensity;
int greenIntensity;
int blueIntensity;
if (color <=
255) // zone 1
{ 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) // zone 2
{ redIntensity = 0; // red is always off
greenIntensity =
255 - (color - 256); // green on to off
blueIntensity =
(color - 256); // blue off to on
}
else // color >=
512 // zone 3
{ redIntensity = (color - 512); // red off to on
greenIntensity =
0; // green is always
off
blueIntensity =
255 - (color - 512); // blue on to off
}
analogWrite(RED_PIN,
redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN,
greenIntensity);}