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;
}