RGB Led according to temperature

bubulindo:
This was on the arduino website

For the mathematically inclined, here's the whole function
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Judging by your calls to map, the result may not be what you wanted. shouldn't it be like this?

  int blue = 255 - (map(temp, 0,31, 0, 63) * 4);

int green = 255 - (map(temp, 26, 34, 0, 100) * 2);
  int red = 255 - (map(temp, 31,50, 0, 63) * 4);



Also, imagine the temperature is 20. your green will be -175. Which, converted to unsigned char to write in the PWM register will be a bit different than what you expect. 

Why don't you adjust the temperature to the PWM directly? 



map(temp, green_low, green_high, 0, 255); //or some other PWM values that do what you want...

The led is an anode not a cathode, that is why the max is min and min is max...