3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Twitter Mood Light - The World's Mood in a Box

Step 10Programming step 4: RGB LED

Programming step 4: RGB LED
A simple library for setting the colour of an RGB LED. The library will fade between the colours as the world mood changes, and will flash if it is a significant change in mood.


*** update ***

If you find the colours look wrong, try removing the "255 -" from the analogWrite calls.
Thanks to shobley for finding this.
More info at http://www.stephenhobley.com/blog/2010/06/11/arduino-world-mood-light-using-twitter-and-wishield/

*** end update ***

/*
 The led is initially set to be currentColorID and over time will fade
 to desiredColorID with a time delay, fadeDelay, measured in ms, between
 each step. No effort is made to scale the step size for each rgb
 channel so each may not complete at the same time.
*/
void LED::FadeTo(int desiredColorID)
{
      // check for valid colorID
 if (desiredColorID >= NUM_COLORS ||
      desiredColorID < 0)
    {
      //logger.log("invalid Color id")
      return;
    }
   
 // get a local copy of the colors
 Color currentColor;
 currentColor.r = Colors[m_currentColorID].r;
 currentColor.g = Colors[m_currentColorID].g;
 currentColor.b = Colors[m_currentColorID].b;
 
 Color desiredColor;
 desiredColor.r = Colors[desiredColorID].r;
 desiredColor.g = Colors[desiredColorID].g;
 desiredColor.b = Colors[desiredColorID].b;
 
 bool done = false;
 
 while (!done)
 {
    // move each of r,g,b a step closer to the desiredColor value
   
    if (currentColor.r < desiredColor.r)
    {
      currentColor.r++;
    }
    else if (currentColor.r > desiredColor.r)
    {
      currentColor.r--;
    }
   
    if (currentColor.g < desiredColor.g)
    {
      currentColor.g++;
    }
    else if (currentColor.g > desiredColor.g)
    {
      currentColor.g--;
    }
   
    if (currentColor.b < desiredColor.b)
    {
      currentColor.b++;
    }
    else if (currentColor.b > desiredColor.b)
    {
      currentColor.b--;
    }
 
    // write the new rgb values to the correct pins
    analogWrite(m_redPin, 255 - currentColor.r);  
    analogWrite(m_greenPin, 255 - currentColor.g);
    analogWrite(m_bluePin, 255 - currentColor.b);  
   
    // hold at this color for this many ms
    delay(m_fadeDelay);
   
    // done when we have reach desiredColor 
    done = (currentColor.r == desiredColor.r &&
            currentColor.g == desiredColor.g &&
            currentColor.b == desiredColor.b);
           
 } // while (!done)
 
 m_currentColorID = desiredColorID;
}
 
 

« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
29
Followers
1
Author:RandomMatrix