Step 4Digital Arduino Code
But don't take our word for it download the code below and get playing. To do this simply...
- Copy the code below and paste it into a an empty Arduino sketch.
- Compile and upload the sketch to your Arduino board.
- Enjoy as your three LEDs light up Red, Green, and Blue.
- If you go to the loop() section of the code under example 1 you can see where you change the colors. Try a few other variations
- To see how we control each LED look at the setColor() function.
- Next comment out (add //) to the three lines of example 1.
- Un-comment (delete //) from in front of every line in the Example 2 section of the loop() code
- Upload this to your board and watch as your three LEDs change colour randomly.
- Play around with the code to see how it works and make your own fun color functions.
Appendix 1: _RGBL_Digital.pde
//--- bof RGBL - RGB Digital Preamble//RGB LED pinsint ledDigitalOne[] = {14, 15, 16}; //the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePinint ledDigitalTwo[] = {9, 10, 11}; //the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePinint ledDigitalThree[] = {3, 5, 6}; //the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePinconst boolean ON = LOW; //Define on as LOW (this is because we use a common Anode RGB LED (common pin is connected to +5 volts)const boolean OFF = HIGH; //Define off as HIGH//Predefined Colorsconst boolean RED[] = {ON, OFF, OFF}; const boolean GREEN[] = {OFF, ON, OFF}; const boolean BLUE[] = {OFF, OFF, ON}; const boolean YELLOW[] = {ON, ON, OFF}; const boolean CYAN[] = {OFF, ON, ON}; const boolean MAGENTA[] = {ON, OFF, ON}; const boolean WHITE[] = {ON, ON, ON}; const boolean BLACK[] = {OFF, OFF, OFF}; //An Array that stores the predefined colors (allows us to later randomly display a color)const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};//--- eof RGBL - RGB Digital Preamblevoid setup(){ for(int i = 0; i < 3; i++){ pinMode(ledDigitalOne[i], OUTPUT); //Set the three LED pins as outputs pinMode(ledDigitalTwo[i], OUTPUT); //Set the three LED pins as outputs pinMode(ledDigitalThree[i], OUTPUT); //Set the three LED pins as outputs }}void loop(){/* Example - 1 Set a color Set the three LEDs to any predefined color*/ setColor(ledDigitalOne, RED); //Set the color of LED one setColor(ledDigitalTwo, GREEN); //Set the color of LED two setColor(ledDigitalThree, BLUE); //Set the color of LED three/* Exampe - 2 Go through Random Colors Set the LEDs to a random color*/ //int rand = random(0, sizeof(COLORS) / 2); //get a random number within the range of colors // setColor(ledDigitalOne, COLORS[rand]); //Set the color of led one to a random color //rand = random(0, sizeof(COLORS) / 2); //Set the color of LED 2 to a random color // setColor(ledDigitalTwo, COLORS[rand]); //rand = random(0, sizeof(COLORS) / 2); //Set the color of LED 3 to a random color // setColor(ledDigitalThree, COLORS[rand]); //delay(1000); }/* Sets an led to any color led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin) color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)*/void setColor(int* led, boolean* color){ for(int i = 0; i < 3; i++){ digitalWrite(led[i], color[i]); }}/* A version of setColor that allows for using const boolean colors*/void setColor(int* led, const boolean* color){ boolean tempColor[] = {color[0], color[1], color[2]}; setColor(led, tempColor);}| « Previous Step | Download PDFView All Steps | Next Step » |














































I'm sure this is possible, and something like an Arduino would be the way to go, at least to test the idea. However a basic Arduino (like the one I'm still learning to play around with) won't have enough sockets for that many individual LEDs in series - if I'm reading your comment right then maybe you could just have a single socket connected to multiple LEDs in parallel (well, four sockets, since the LEDs have four pins). You won't be able to control them individually but perhaps that won't matter to you.
The dial itself could be pretty much any random dial that physically fits - as far as I'm aware they all work as variable resistors, so the Arduino would just figure out how far the dial was turned. No problem.
The only other problem I can think of is power consumption - the Arduino may not put out enough power for that many LEDs, so you might need to plug in a separate power source, and make a power circuit to deal with that too.
Perhaps someone more qualified could be more help. :)
Good luck! Let us know when you've posted your own 'structable for this project!
Thanks!