multi Color LED to Temp sensor

I am wanting to use a series of RGB led’s that will change colors and blend together based on the outside temperature. The end product is going to be a wall hanging with frosted glass that will change color based on the temperature outside. What I am wanting to do with the program is set it so Blue = 0 degrees F, green = 60 degrees F, and red = 120 degrees F. At values in between the solid colors I am wanting to use an analog output to fade led’s together. So essentially 15 degrees would be 75% blue, 25% green. 30 degrees would be 50% blue 50% green, 45 degrees would be 25% blue 75% green, and so on.

to answer some questions that may come up unrelated to this part of the program, I have a thermistor set up and running well. I have tested it at 32 degrees in ice water and boiling water at 212. it is only off by a 1/2 degree at the bottom end and 1.5 degree at the top which may be attributed to the ± in resistors. I also will have the temperature on the wall hanging on an LED so it will actually be functional and hopefully good looking.

I have played with a few different ideas such as having them blend to a set amount based on a temp range and fade in and out to appear they are always changing. sample below.

if (temp >= 70 && temp < 90) { // RED + Green
for (i = 40; i < 230; i++) {
int j = (270 - i);
analogWrite(redPin, i);
analogWrite(bluePin, 255);
analogWrite(greenPin, j);
delay(50);
}
for (i = 230; i > 40; i–) {
int j = (270 - i);
analogWrite(redPin, i);
analogWrite(bluePin, 255);
delay(50);
}
}

if (temp >= 90) { // RED
for (i = 40; i < 230; i++) {
analogWrite(redPin, i);
analogWrite(bluePin, 255);
analogWrite(greenPin, 255);
delay(50);
}
for (i = 230; i > 40; i–) {
analogWrite(redPin, i);
analogWrite(bluePin, 255);
analogWrite(greenPin, 255);
delay(50);

the code above was OK, but i feel like my limited programming ability is really hindering my ability to do this project right. So I’m wondering if someone can point me in the right direction or at least give me the start of logic I’m needing to get this done.

Thanks!

-Jon

   for (i = 40; i < 230; i++)
   {
     int j = (270 - i);
     analogWrite(redPin, i);
     analogWrite(bluePin, 255);
     analogWrite(greenPin, j);
     delay(50);
   }

Why do you feel it necessary to set the blue pin to the same value 190 times?

I don’t understand what you are trying to do here. If the color is supposed to reflect the temperature, what are the fading loops for?

PaulS:

   for (i = 40; i < 230; i++)

{
     int j = (270 - i);
     analogWrite(redPin, i);
     analogWrite(bluePin, 255);
     analogWrite(greenPin, j);
     delay(50);
   }



Why do you feel it necessary to set the blue pin to the same value 190 times?

I don't understand what you are trying to do here. If the color is supposed to reflect the temperature, what are the fading loops for?

Sure, that code was my original attempt at getting something working. I’m sure the code is choppy at best but right about at my programming level at this point. I had to take VB for college but it didn’t stick to say the least. The idea of that program was if the temp is a certain level, say 75F the red and green led would be on and they would crossfade to make my project look “alive”. I have since decided that is stupid and am looking to do the program the way I stated above, but am having trouble figuring out how to write what I want.

-Jon

but am having trouble figuring out how to write what I want.

Generally, when I am in this boat, it is because I do not have clear requirements. If that is not your case, please list the requirements, and we can help you convert them to code.

PaulS:

but am having trouble figuring out how to write what I want.

Generally, when I am in this boat, it is because I do not have clear requirements. If that is not your case, please list the requirements, and we can help you convert them to code.

I am wanting to use a series of RGB led's that will change colors and blend together based on the outside temperature. What I am wanting to do with the program is set it so Blue = 0 degrees F, green = 60 degrees F, and red = 120 degrees F. At values in between the solid colors I am wanting to use an analog output to fade led's together. So essentially 15 degrees would be 75% blue, 25% green. 30 degrees would be 50% blue 50% green, 45 degrees would be 25% blue 75% green, and so on.

Is this enough of an idea of what I am wanting?

What I am wanting to do with the program is set it so Blue = 0 degrees F, green = 60 degrees F, and red = 120 degrees F.

You have defined the color levels at specific points (0 degrees --> 0, 0, 255; 60 degrees --> 0, 255, 0; 120 degrees --> 255, 0, 0). This is a hard requirement (as in easy to implement).

At values in between the solid colors I am wanting to use an analog output to fade led's together.

I'm not seeing anything like a requirement here. This is a statement of what function/pin to use. Of course, this is how intermediate temperature colors will be displayed.

So essentially 15 degrees would be 75% blue, 25% green.

This is an incomplete requirement. You have 2 60 degree temperature spreads (0 to 60 and 60 to 120). For one of them, you have partially identified an intermediate point (there is no red component specified). One could assume that this means red is 0 all the way from 0 to 60 degrees, but, one should not have to make assumptions.

If the blue component is to vary linearly from 100% to 0% as the temperature rises from 0 to 60 degrees, you should explicitly state this. Similarly, if the green component is to vary linearly from 0% to 100%, you should explicitly state this.

You need similar statements to define what the program should do between 60 and 120 degrees.

For completeness, you should describe what the program should do for temperatures below 0 or above 120 degrees.

When this is done, creating a function that takes 4 arguments (temp, ref to r, ref to g, and ref to b) and sets r, g, and b appropriately, based on temp, will be easy to write.

One thing that occurs to me. You should determine if analogWrite(redPin, 240) creates an intense enough red light.

If so, then each degree increase of temperature from 0 to 60 would simply result in a 4 point increase/decrease in a color component, enabling the use of integer arithmetic instead of floating point arithmetic.

PaulS:

What I am wanting to do with the program is set it so Blue = 0 degrees F, green = 60 degrees F, and red = 120 degrees F.

You have defined the color levels at specific points (0 degrees → 0, 0, 255; 60 degrees → 0, 255, 0; 120 degrees → 255, 0, 0). This is a hard requirement (as in easy to implement).

At values in between the solid colors I am wanting to use an analog output to fade led’s together.

I’m not seeing anything like a requirement here. This is a statement of what function/pin to use. Of course, this is how intermediate temperature colors will be displayed.

So essentially 15 degrees would be 75% blue, 25% green.

This is an incomplete requirement. You have 2 60 degree temperature spreads (0 to 60 and 60 to 120). For one of them, you have partially identified an intermediate point (there is no red component specified). One could assume that this means red is 0 all the way from 0 to 60 degrees, but, one should not have to make assumptions.

If the blue component is to vary linearly from 100% to 0% as the temperature rises from 0 to 60 degrees, you should explicitly state this. Similarly, if the green component is to vary linearly from 0% to 100%, you should explicitly state this.

You need similar statements to define what the program should do between 60 and 120 degrees.

For completeness, you should describe what the program should do for temperatures below 0 or above 120 degrees.

When this is done, creating a function that takes 4 arguments (temp, ref to r, ref to g, and ref to b) and sets r, g, and b appropriately, based on temp, will be easy to write.

Thanks for your attempts to help me so far PaulS. I apologize that I can’t write down my idea’s concisely enough for you’re purposes. I suppose some of that is i’m just getting started with “real” programming and i don’t have the literal step by step and take nothing for granted idea wired into my brain yet (which I am working on).

so it should look something like this
< 0 = blue 255, green 0, red 0.
from 0 to 60 the blue led should reduce from 255 to 0 linearly,
from 0 to 60 the green led should rise from 0 to 255 linearly,
from 0 to 60 red led should be 0.

from 61 to 120 the blue led should be 0.
from 61 to 120 the green led should reduce from 255 to 0 linearly,
from 61 to 120 the red led should rise from 255 to 0 linearly,

120 = red 255, blue 0, green 0.

I hope this is concise enough. again, thanks for you help PaulS.

I apologize that I can’t write down my idea’s concisely enough for you’re purposes.

It’s not my purposes. It’s your project that you need to write code for. What I am seeing is that you are struggling to define the requirements well enough to write code to implement. Often, for our own projects, it is harder to pin down requirements than when someone else is going to write the code. Generally, this is because when someone else is to write the code, that person is knowledgeable enough to ask the right questions.

I suppose some of that is i’m just getting started with “real” programming and i don’t have the literal step by step and take nothing for granted idea wired into my brain yet (which I am working on).

Partly. And partly because getting the requirements right is hard. Knowing the difference between a complete requirement and a partial requirement, a statement of fact, and wishful thinking is not easy. It comes with experience, though. I’ve been at this 30+ years, and I still have to go back to the customer for clarification from time to time.

so it should look something like this
< 0 = blue 255, green 0, red 0.

from 0 to 60 the blue led should reduce from 255 to 0 linearly,
from 0 to 60 the green led should rise from 0 to 255 linearly,
from 0 to 60 red led should be 0.

from 61 to 120 the blue led should be 0.
from 61 to 120 the green led should reduce from 255 to 0 linearly,
from 61 to 120 the red led should rise from 255 to 0 linearly,

120 = red 255, blue 0, green 0.

Now, this is easy to translate into code.

if(temp < 0)
{
   r = 0;
   g = 0;
   b = 255;
}
else if(temp < 60)
{
   r = 0;
   g = map(temp, 0, 60, 255, 0);
   b = map(temp, 0, 60, 0, 255);
}
else if(temp < 120)
{
   r = map(temp, 61, 120, 0, 255);
   g = map(temp, 61, 120, 255, 0);
   b = 0;
}
else
{
   r = 255;
   g = 0;
   b = 0;
}

where r, g, and b are ints that define the color level. Using r, g, and b in digitalWrite() statements is trivial, and is left as an exercise for the student.

Just to point out something:-

from 0 to 60 the blue led should reduce from 255 to 0 linearly,

While you can reduce the intensity of the light linearly it will not look like that to the eye. This is because the response of the eye is very non linear. http://en.wikipedia.org/wiki/Gamma_correction

The other curve ball is that any colour you generate is not perceived as an absolute colour but it depends on colours around it. Look at the yellow and brown squares here, they are the same colour. http://www.internetbestsecrets.com/2009/06/ames-window-and-color-perception.html So when you actually finally do what you want it might not look like you expect.

It is so simple it almost bothers me why I couldn't get it figured out. I keep forgetting about the map function. Thank you PaulS and i'll give that a try tonight when I get home from work.

Grumpy_Mike: Just to point out something:-

from 0 to 60 the blue led should reduce from 255 to 0 linearly,

While you can reduce the intensity of the light linearly it will not look like that to the eye. This is because the response of the eye is very non linear. http://en.wikipedia.org/wiki/Gamma_correction

The other curve ball is that any colour you generate is not perceived as an absolute colour but it depends on colours around it. Look at the yellow and brown squares here, they are the same colour. http://www.internetbestsecrets.com/2009/06/ames-window-and-color-perception.html So when you actually finally do what you want it might not look like you expect.

Thanks for pointing this out! i'll give PaulS's code a try tonight and see what I think. From there i'm sure I can make correction.