Journal #7699

Posted 12 years ago2012-03-25 10:18:39 UTC
Striker StrikerI forgot to check the oil pressure
I need a function that decreases linearly for an Arduino mini-project. I need to somehow program the RGB led to go from blue to green, to yellow to red as the temperature varies and that means that I have to individually control 3 pins.

I have a couple of "if"s for each pin.

While the red pin is easy to control, as I simply have to gradually increase the output from 0 to 255( gradual control of electronics with arduino is achieved through PWM(pulse width modulation), in this case in 256 steps) in the interval of, say, 45 to 120 degrees C, the green and the blue pin is more complicated.

The blue pin will be simply at its maximum value if the temperature drops bellow 0C, but from 0C to 25C it has to gradually decrease in intensity.
Before going into more details I want to say that Arduino has a special "map" function that takes an interval of values and maps it to another interval. So what I am doing is this:
map(temp, 0, 25, 0, 255); 
temp is what the sensor outputs( well it actually outputs a voltage that is used to compute the temperature). 0-25 is the interval that will be mapped to 0-255. I would have made a function that maps the values myself but I have no idea how, but it's kind of redundant since there's already one.

So I have to basically decrease the value from 255 to 0 as the temperature increases from 0 to 25. Is there a magical formula for decreasing linearly? Because using something like f(x)=1/x gives me an asymptote and only the first 2 or 3 values are "visible" on the LED.

For the green pin I have to do a combination of what I'm doing for the blue and red pin. From 15C to 45C it will gradually increase in intensity, but from 45C to 70C it will gradually decrease.

Anyone has any suggestions?

TL;DR I need a function that decreases linearly. Any suggestions?

11 Comments

Commented 12 years ago2012-03-25 10:27:03 UTC Comment #57933
Are there floating point numbers or do I only get map()?
Commented 12 years ago2012-03-25 10:36:07 UTC Comment #57937
Since I'm operating with ints I suppose everything is an approximated int. The PWM output must be an int.
Commented 12 years ago2012-03-25 10:50:20 UTC Comment #57934
int blue = map(temp, 0, 25, 255, 0);
int green = 0;
if(temp < 45)
{
green = map(temp, 15, 45, 0, 255);
}
else
{
green = map(temp, 45, 70, 255, 0);
}
int red = map(temp, 45, 120, 0, 255)
Commented 12 years ago2012-03-25 10:51:33 UTC Comment #57940
I really need to get into Arduino, I've heard so much about it, but never used it.
But i still prefer making a protoboard model with analog electronics + LCD screen + micro controller and than plotting it and finishing it on a vitroplast board.
Commented 12 years ago2012-03-25 12:05:12 UTC Comment #57931
Ah yes, i remember your journal when you first got it. Your journal inspired me so I've been thinking about getting one but i know NOTHING about electronics so that's what is holding me back. Maybe i should just go for it and purchase a starters kit, a beginners book and start creating simple things like blinking LEDs or something...
Commented 12 years ago2012-03-25 12:41:50 UTC Comment #57938
"int blue = map(temp, 0, 25, 255, 0);" - I can't believe I haven't thought about this. I don't now if it works but if it does, then *facepalm.
But my curiosity remains - is there a function that decreases linearly?
Commented 12 years ago2012-03-25 12:58:36 UTC Comment #57936
I don't really understand what you're asking...surely linear progression is one of the simplest things in programming. Especially in only one dimension.

In case that's really what you're asking:

If you have a value x in the range between a and b, x is [x/(b-a)] distance from a.
If you have another range from c to d, you can get an equivalent point y in that range with y = c+[distance*(d-c)]

Of course distance will need to be a floating point number because it's between 0 and 1.
Commented 12 years ago2012-03-25 14:45:11 UTC Comment #57935
f(x)=-x

...

:P
Commented 12 years ago2012-03-25 15:10:55 UTC Comment #57939
No it's not that. I actually discovered what it was. It's f(x)=a-x. So yes, pretty *facepalm worthy.

[EDIT] Although that was the function I was looking after, I don't need it anymore. Grim's suggestion to put the values in reverse in the map function actually works. This just saved me from a lot of hassle.
Commented 12 years ago2012-03-25 15:55:10 UTC Comment #57932
I'm so confused.
Commented 12 years ago2012-03-25 18:02:01 UTC Comment #57941
I remember when we programmed a thermometer with LED's in QBASIC. Good times.

You must log in to post a comment. You can login or register a new account.