Wednesday, January 2, 2013

Day 3: Multicolored LEDs and a cool side project

The circuit involved in circuit three of the Sparkfun Inventor's Kit (SIK) was extremely simple. It basically involved plugging three LEDs of different colors (all in one convenient package pictured below) into a circuit.

The really cool bits were in the programming where I was introduced to two new concepts. First was the function analogWrite(PIN, Intensity) which basically simulates analog output. The SIK guide has a nice explanation of the function, but basically what it does is flickers a digital output on and off at various speeds to approximate analog output. The result in the case of the LED was changing intensities of the LEDs based on the values in the intensity level put into the analogWrite function.

The other important concept was the use of a for loop in programming the Arduino. I've encountered loops many times in the past doing other types of programming but this is the first time I've encountered it in this setting. Unsurprisingly, they worked in exactly the same way. The function that was created basically cycled the three color LED through the color spectrum by varying the brightness of the three LEDs. Since the analogWrite function takes intensities between 0 and 255, it was simply a matter of creating a new function (called showSpectrum) that looped through values of 0 and 255 on each of the three colors. The code looked like this:

void showSpectrum()
  int x;  // define an integer variable called "x"
  // Now we'll use a for() loop to make x count from 0 to 767
  // (Note that there's no semicolon after this line!
  // That's because the for() loop will repeat the next
  // "statement", which in this case is everything within
  // the following brackets {} )

  for (x = 0; x < 768; x++)

  // Each time we loop (with a new value of x), do the following:

    showRGB(x);  // Call RGBspectrum() with our new x
    delay(10);   // Delay for 10 ms (1/100th of a second)

// showRGB()

// This function translates a number between 0 and 767 into a
// specific color on the RGB LED. If you have this number count
// through the whole range (0 to 767), the LED will smoothly
// change color through the entire spectrum.

// The "base" numbers are:
// 0   = pure red
// 255 = pure green
// 511 = pure blue
// 767 = pure red (again)

// Numbers between the above colors will create blends. For
// example, 640 is midway between 512 (pure blue) and 767
// (pure red). It will give you a 50/50 mix of blue and red,
// resulting in purple.

// If you count up from 0 to 767 and pass that number to this
// function, the LED will smoothly fade between all the colors.
// (Because it starts and ends on pure red, you can start over
// at 0 without any break in the spectrum).

void showRGB(int color)
  int redIntensity;
  int greenIntensity;
  int blueIntensity;

  // Here we'll use an "if / else" statement to determine which
  // of the three (R,G,B) zones x falls into. Each of these zones
  // spans 255 because analogWrite() wants a number from 0 to 255.

  // In each of these zones, we'll calculate the brightness
  // for each of the red, green, and blue LEDs within the RGB LED.

  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

  // Now that the brightness values have been set, command the LED
  // to those values

  analogWrite(RED_PIN, redIntensity);
  analogWrite(BLUE_PIN, blueIntensity);
  analogWrite(GREEN_PIN, greenIntensity);

I've left the comments in so that everything is clear. Not too terribly complicated.

And finally for the good stuff. After finishing exercise 3, I thought to myself, wouldn't it be fun if I could make the LEDs cycle through the spectrum based on twisting the potentiometer? Thus my very first independent arduino project was born. Here is my needlessly dramatic video of the results.

In the end the project basically involved making a circuit that was a hybrid of the potentiometer circuit created in circuit 2 with the circuit I had just created. I created one circuit which plugged the potentiometer into the analog input of the arduino and left the three color LED just the way it was in circuit 3.

The programming was slightly trickier and I've uploaded it to scribd here. Most of the code is pretty easy if you understand circuit three. The only tricky part is to realize that the output from the potentiometer ranges from 1-1023 while the function we've created to cycle through the color spectrum goes from 1-766. As a result, we need to map the values 1-1023 to 1-766 and this is done by multiplying the output from the potentiometer by a factor 766/1023.

And there you have it! A cute little side project!

No comments:

Post a Comment