Improving an Arduino LED Mood Cube (Simple) (Video Included)

by Akitaowo in Circuits > Arduino

140 Views, 0 Favorites, 0 Comments

Improving an Arduino LED Mood Cube (Simple) (Video Included)

S__19374093.jpg

After seeing an LED small mood cube project created by 'earl, I decided to do an improved version of the LED Mood Cube. My version will be more complex than the original one, as it will be slightly larger than the original one, have two more colors comparing to the original cube (added yellow and white), have an infinite amount of rotation of colors, etc. This should be a good project to practice on further usage of LED lights for those who understood the concept on connecting LED lights with wires.

Materials

learn_arduino_breadboard_half.jpg
F7617324-01.jpg

Here are some materials you will need to make this Mood Cube:

  • Breadboard
  • Arduino - (I have Leonardo here)
  • Arduino power supply / USB cable
  • Breadboard
  • Jumper Wires (Lots of them, I used 29 wires)
  • Red LED x 2
  • Blue LED x 2
  • Green LED x 2
  • Yellow LED x 2
  • White LED x 1
  • 9 resistors
  • Box big enough to fit the breadboard (I used a shoe box)
  • Utility Knife
  • Paper

Code

Some explanation for the code given here:

The credit of the codes go to the original source of my project as the editor of the project created these codes. I merely improved some of them by making them more complex. In some codes, you might see //改 in the end. This means that this code is edited by me, so it is different from my original source.

I also has a version of the code on Arduino Creator.

<p>/*<br>  Code for cross-fading 3 LEDs, red, green and blue (RGB)
  To create fades, you need to do two things:
   1. Describe the colors you want to be displayed
   2. List the order you want them to fade in</p><p>  DESCRIBING A COLOR:
  A color is just an array of three percentages, 0-100,
   controlling the red, green and blue LEDs</p><p>  Red is the red LED at full, blue and green off
    int red = { 100, 0, 0 }
  Dim white is all three LEDs at 30%
    int dimWhite = {30, 30, 30}
  etc.</p><p>  Some common colors are provided below, or make your own</p><p>  LISTING THE ORDER:
  In the main part of the program, you need to list the order
   you want to colors to appear in, e.g.
   crossFade(red);
   crossFade(green);
   crossFade(blue);</p><p>  Those colors will appear in that order, fading out of
     one color and into the next</p><p>  In addition, there are 5 optional settings you can adjust:
  1. The initial color is set to black (so the first color fades in), but
     you can set the initial color to be any other color
  2. The internal loop runs for 1020 interations; the 'wait' variable
     sets the approximate duration of a single crossfade. In theory,
     a 'wait' of 10 ms should make a crossFade of ~10 seconds. In
     practice, the other functions the code is performing slow this
     down to ~11 seconds on my board. YMMV.
  3. If 'repeat' is set to 0, the program will loop indefinitely.
     if it is set to a number, it will loop that number of times,
     then stop on the last color in the sequence. (Set 'return' to 1,
     and make the last color black if you want it to fade out at the end.)
  4. There is an optional 'hold' variable, which pasues the
     program for 'hold' milliseconds when a color is complete,
     but before the next color starts.
  5. Set the DEBUG flag to 1 if you want debugging output to be
     sent to the serial monitor.</p><p>     The internals of the program aren't complicated, but they
     are a little fussy -- the inner workings are explained
     below the main loop.</p><p>  April 2007, Clay Shirky <clay.shirky@nyu.edu>
*</clay.shirky@nyu.edu></p><p>/ Output
int ylwPin = 5;   // Yellow LED, connected to digital pin 5 //改
int redPin = 6;   // Red LED,   connected to digital pin 6 //改
int grnPin = 7;  // Green LED, connected to digital pin 7 //改
int bluPin = 8;  // Blue LED,  connected to digital pin 8 //改
int whiPin = 9;   // White LED,   connected to digital pin 9 //改
int ylwPin2 = 10;  // Yellow LED, connected to digital pin 10 //改
int redPin2 = 11;   // Red LED,   connected to digital pin 11 //改
int grnPin2 = 12;  // Green LED, connected to digital pin 12 //改
int bluPin2 = 13;  // Blue LED,  connected to digital pin 13 //改</p><p>// Color arrays
int black[9]  = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };//改
int white[9]  = { 100, 100, 100, 100, 100, 100, 100, 100, 100 };//改
int red[9]    = { 0, 0, 100, 0, 0, 0, 100, 0, 0 };//改
int green[9]  = { 0, 100, 0, 0, 0, 0, 0, 100, 0 };//改
int blue[9]   = { 0, 0, 0, 100, 0, 0, 0, 0, 100 };//改
int yellow[9] = { 100, 0, 0, 0, 0, 100, 0, 0, 0 }; //改
int purple[9]  = { 0, 50, 0, 50, 0, 0, 50, 0, 50 }; //改
int orange[9] = { 50, 50, 0, 0, 0, 50, 50, 0, 0 }; //改
int pink[9]   = { 0, 50, 0, 0, 50, 0, 0, 50, 0, }; //改
// etc.</p><p>// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];
int ylwVal = black[3]; //改
int whiVal = black[4]; //改</p><p>int wait = 15;      // 10ms internal crossFade delay; increase for slower fades //改
int hold = 1;       // Optional hold when a color is complete, before the next crossFade //改
int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 0;     // How many times should we loop before stopping? (0 for no stop) //改
int j = 0;          // Loop counter for repeat</p><p>// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
int prevY = ylwVal;
int prevW = whiVal; //改</p><p>// Set up the LED outputs
void setup()
{
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);
  pinMode(ylwPin, OUTPUT); //改
  pinMode(whiPin, OUTPUT); //改
  pinMode(grnPin2, OUTPUT); //改
  pinMode(bluPin2, OUTPUT); //改
  pinMode(ylwPin2, OUTPUT); //改
  pinMode(redPin2, OUTPUT); //改</p><p>  if (DEBUG) {           // If we want to see values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput
  }
}</p><p>// Main program: list the order of crossfades
void loop()
{
  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);
  crossFade(white);
  crossFade(pink);
  crossFade(purple);
  crossFade(orange);</p><p>  if (repeat) { // Do we loop a finite number of times?
    j += 1;
    if (j >= repeat) { // Are we there yet?
      exit(j);         // If so, stop.
    }
  }
}</p><p>/* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS</p><p>  The program works like this:
  Imagine a crossfade that moves the red LED from 0-10,
    the green from 0-5, and the blue from 10 to 7, in
    ten steps.
    We'd want to count the 10 steps and increase or
    decrease color values in evenly stepped increments.
    Imagine a + indicates raising a value by 1, and a -
    equals lowering it. Our 10 step fade would look like:</p><p>    1 2 3 4 5 6 7 8 9 10
  R + + + + + + + + + +
  G   +   +   +   +   +
  B     -     -     -</p><p>  The red rises from 0 to 10 in ten steps, the green from
  0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.</p><p>  In the real program, the color percentages are converted to
  0-255 values, and there are 1020 steps (255*4).</p><p>  To figure out how big a step there should be between one up- or
  down-tick of one of the LED values, we call calculateStep(),
  which calculates the absolute gap between the start and end values,
  and then divides that gap by 1020 to determine the size of the step
  between adjustments in the value.
*/</p><p>int calculateStep(int prevValue, int endValue) {
  int step = endValue - prevValue; // What's the overall gap?
  if (step) {                      // If its non-zero,
    step = 1020 / step;            //   divide by 1020
  }
  return step;
}</p><p>/* The next function is calculateVal. When the loop value, i,
   reaches the step size appropriate for one of the
   colors, it increases or decreases the value of that color by 1.
   (R, G, and B are each calculated separately.)
*/</p><p>int calculateVal(int step, int val, int i) {</p><p>  if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
    if (step > 0) {              //   increment the value if step is positive...
      val += 1;
    }
    else if (step < 0) {         //   ...or decrement it if step is negative
      val -= 1;
    }
  }
  // Defensive driving: make sure val stays in the range 0-255
  if (val > 255) {
    val = 255;
  }
  else if (val < 0) {
    val = 0;
  }
  return val;
}</p><p>/* crossFade() converts the percentage colors to a
   0-255 range, then loops 1020 times, checking to see if
   the value needs to be updated each time, then writing
   the color values to the correct pins.
*/</p><p>void crossFade(int color[]) { //改
  // Convert to 0-255
  int R = (color[0] * 255) / 100;
  int G = (color[1] * 255) / 100;
  int B = (color[2] * 255) / 100;
  int Y = (color[3] * 255) / 100; //改
  int W = (color[4] * 255) / 100; //改</p><p>  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G);
  int stepB = calculateStep(prevB, B);
  int stepY = calculateStep(prevY, Y); //改
  int stepW = calculateStep(prevW, W); //改</p><p>  for (int i = 0; i <= 1020; i++) {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);
    ylwVal = calculateVal(stepY, ylwVal, i); //改
    whiVal = calculateVal(stepW, whiVal, i); //改</p><p>    analogWrite(redPin, redVal);   // Write current values to LED pins
    analogWrite(grnPin, grnVal);
    analogWrite(bluPin, bluVal);
    analogWrite(ylwPin, ylwVal); //改
    analogWrite(whiPin, whiVal); //改
    analogWrite(grnPin2, grnVal); //改
    analogWrite(bluPin2, bluVal); //改
    analogWrite(ylwPin2, ylwVal); //改
    analogWrite(redPin2, redVal); //改</p><p>    delay(wait); // Pause for 'wait' milliseconds before resuming the loop</p><p>    if (DEBUG) { // If we want serial output, print it at the
      if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
        Serial.print("Loop/RGBYW: #");
        Serial.print(i);
        Serial.print(" | ");
        Serial.print(redVal);
        Serial.print(" / ");
        Serial.print(grnVal);
        Serial.print(" / ");
        Serial.println(bluVal);
        Serial.print(" / ");
        Serial.println(ylwVal); //改
        Serial.print(" / "); //改
        Serial.println(whiVal); //改
        Serial.print(" / "); //改
      }
      DEBUG += 1;
    }
  }
  // Update current values for next loop
  prevR = redVal;
  prevG = grnVal;
  prevB = bluVal;
  prevY = ylwVal; //改
  prevW = whiVal; //改
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}</p>

Set Up

S__19374091.jpg
S__19374090.jpg
S__19374089.jpg
S__19374088.jpg
S__19374086.jpg
S__19374092.jpg

  1. Get the breadboard.
  2. Demo of connecting wires so an LED light can shine:
    1. Put the LED on the breadboard. Place the longer end on the left and the shorter end on the right.
    2. Place one end of a jumper wire on a spot that is on the same row with the longer end of the LED. Place the other end onto the Digital PWM section. The code specified the yellow LEDs to be connected to 10 and 5, red ones to 6 and 11, blue ones to 8 and 13, green ones to 7 and 12, and finally white LED to 9.
    3. Place one end of a resistor on the same row with the shorter end of the LED. Place the other end at somewhere near.
    4. Place an end of another jumper wire with the same row with the end of the resistor which is not on the same row with the shorter end of the LED. Place the other end of the wire on the negative charge row.
    5. Place an end of yet another jumper wire on the negative charge row and place its other end on GND.
  3. Repeat Step 2 for 8 times as you want 9 LEDs to shine
  4. Place the breadboard inside the box. A few reminders here:
    1. I used a power supply. Create a small hole for your wires using an utility knife to pass through the box and connect to the breadboard.
    2. Make sure the box as one side that is open. A shoe box already has one side that is open. If the box is fully closed, cut of one side of the box to make an open space.
    3. Cover the side with nothing with paper. This is for LED lights to shone through paper.

Video/LED Mood Cube

Mood Cube