I decided to start out with this project by coding it up on Tinkercad. I didn't really like the way the code was written, so I wanted to play with the code without having to worry that I wired it wrong (this ended up being a very good idea).
I set it up specifically so that it flashed red five times after doing the "show off the main colors" part (mainColors()) so you'd know that the "show off all the possible colors" part (showSpectrum()) was coming up. Then I had it flash green five times so you'd know it was starting over again.
The next step was to move it over to the board itself. Didn't work. Took everything apart and rewired it again. Didn't work. Troubleshot by replacing multicolor LED with individual color LEDs. Nope. Tried putting in a regular LED on pin 13 to make sure code was at least coming over to the board. Learned that the ground from the LED needed to go back to the same ground that the Arduino was coming from. Yep, that was good, but rest still didn't work. Eight years later...
So that was fun! And then lo and behold, everything worked! I recorded the video in the dark because it made it easier to see the colors.
(I played around with Camtasia on this one to try and show the code alongside the LEDs, but it runs really fast - might help to run YouTube at .5x to see it better!)
Here is the circuit diagram, exported from Tinkercad.
// Here's a new trick: putting the word "const" in front of a
// variable indicates that this is a "constant" value that will
// never change. (You don't have to do this, but if you do, the
// Arduino will give you a friendly warning if you accidentally
// try to change the value, so it's considered good form.)
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
// This variable controls how fast we loop through the colors.
// (Try changing this to make the fading faster or slower.)
int DISPLAY_TIME = 500; // In milliseconds
int DELAY_TIME = 10; // in milliseconds
void setup(){
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Do this so we get outputs for troubleshooting if necessary
// Serial.begin(9600);
} // end setup
void loop(){
int i;
// Just in case we need this.
// Serial.println(1000);
mainColors();
// Flash red light five times so we know the next
// phase is coming
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
for (i = 0; i < 5; i++){
digitalWrite(RED_PIN, LOW);
delay(500);
digitalWrite(RED_PIN, HIGH);
delay(300);
} // end for
showSpectrum();
// Flash green light five times so we know the next
// phase is coming
digitalWrite(RED_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
for (i = 0; i < 5; i++){
digitalWrite(GREEN_PIN, LOW);
delay(500);
digitalWrite(GREEN_PIN, HIGH);
delay(300);
} // end for
} // end loop
/* The function mainColors() shows off some of
the colors we can get:
Color | RGB Code
--------------
Red = 100
Green = 010
Blue = 001
Yellow = 110
Cyan = 011
Purple = 101
White = 111 */
void mainColors()
{
// Off (all LEDs off):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
// Red (turn just the red LED on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
// Green (turn just the green LED on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
// Blue (turn just the blue LED on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
// Yellow (turn red and green on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
// Cyan (turn green and blue on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
// Purple (turn red and blue on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
// White (turn all the LEDs on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
} // end mainColors()
// To get the pattern, I played with the RGB Finder
// located here: https://www.rapidtables.com/web/color/RGB_Color.html
void showSpectrum(){
int colorVal;
// Keep Red High and have Blue run 0 to 255
analogWrite(RED_PIN, 255);
analogWrite(BLUE_PIN, 0);
analogWrite(GREEN_PIN, 0);
for (colorVal = 0; colorVal < 255 ; colorVal++) {
analogWrite(BLUE_PIN,colorVal);
delay(DELAY_TIME); // Delay for 10 ms
}
// Keep Blue High and run Red 254 to 0
for (colorVal = 254; colorVal >=0 ; colorVal--) {
analogWrite(RED_PIN,colorVal);
delay(DELAY_TIME);
}
// Keep Blue High and run Green 0 to 255
for (colorVal = 0; colorVal < 255 ; colorVal++) {
analogWrite(GREEN_PIN,colorVal);
delay(DELAY_TIME);
}
// Keep Green High and run Blue 254 to 0
for (colorVal = 254; colorVal >=0 ; colorVal--) {
analogWrite(BLUE_PIN,colorVal);
delay(DELAY_TIME);
}
// Run Red 0 to 255
for (colorVal = 0; colorVal < 255 ; colorVal++) {
analogWrite(RED_PIN,colorVal);
delay(DELAY_TIME);
}
// Run Green Down 254 to 0
for (colorVal = 254; colorVal >=0 ; colorVal--) {
analogWrite(GREEN_PIN,colorVal);
delay(DELAY_TIME);
}
} // end showSpectrum
No comments:
Post a Comment