Need help to light up RGB LED at certain range of voltages.

Hey guys I need some help in implementing this idea. The idea is that I want RGB led to show 3 colours at different range of temperature. For example when temperature is 40-50 = RED, 30-40 = Yellow and 20-30 = green. I'm using LM35 temperature sensor to read the temperature. I have already coded to read the temperature and now want to know how to turn RGB LED in different colours at different temperature.

By, the way did anyone come up with a sample code to do something like this in arduino playground other than http://www.insanegiantrobots.com/arduino/rgbled.html

Thanks.

t = readTemp();
if (t < 30) doLed(0,255,0);
else if (t< 40) doLed(255,255,0);
else doLed(255,0,0);

This also takes values below 20 and above 50 in account.

An nicer version with gradient colors could be

t = readTemp();
int red = constrain((t-30)*25, 0, 255);
int green = 255 - constrain((t-40)*25, 0, 255);
doLed(red, green, 0);

code not tesated but give it a try …

t = readTemp();
int red = constrain((t-30)*25, 0, 255);
int green = 255 - constrain((t-40)*25, 0, 255);
doLed(red, green, 0);

can you give me some comments for the codes please? I’m not quite sure I understand the logic (im just learning this).

Thanks.

You basically need to fade each of the LEDs in the RGB LED. Do you have a common anode or a common cathode RGB LED as this will affect the way you wire it up. The basic idea for one LED is shown here:- http://arduino.cc/en/Tutorial/Fading For common cathode LEDs just have a common ground, (the cathode) and feed each anode from a different PWM capable pin with it's own resistor.