RGB Led according to temperature

My code is bad and I should feel bad :~
Sometimes on low temps the red shows up, same with high temps - the blue shows up.
Does any one mind doing a little optimization or helping me find what causes the issue?

#include <OneWire.h>
#include <DallasTemperature.h>

const int sensPin = 9;
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

OneWire oneWire(sensPin);
DallasTemperature sensors(&oneWire);

DeviceAddress insideThermometer = { 0x28, 0xE7, 0x19, 0x74, 0x03, 0x00, 0x00, 0x7F };

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  sensors.begin();
  sensors.setResolution(insideThermometer, 10);
  pinMode(redPin, OUTPUT); 
  pinMode(greenPin, OUTPUT); 
  pinMode(bluePin, OUTPUT);
  analogWrite(bluePin, 255);
  analogWrite(redPin, 255);
  analogWrite(greenPin, 255);

}

void loop() {
  sensors.requestTemperatures();
  float temp = sensors.getTempC(insideThermometer);
  int blue = 255 - (map(temp, 31, 0, 0, 63) * 4);
  int green = 255 - (map(temp, 34, 26, 0, 100) * 2);
  int red = 255 - (map(temp, 50, 31, 0, 63) * 4);
  analogWrite(bluePin, blue);
  analogWrite(greenPin, green);
  analogWrite(redPin, red);
  Serial.print("Inside temperature is: ");
  Serial.print(temp);
  Serial.print("\n\r");
  delay(1000);
}

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...

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...

Same applies...

bubulindo:
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...

Ok, do this:

for (long temp=0; temp <100; temp++) {
  int blue = 255 - (map(temp, 31, 0, 0, 63) * 4);
  int green = 255 - (map(temp, 34, 26, 0, 100) * 2);
  int red = 255 - (map(temp, 50, 31, 0, 63) * 4);
Serial.print("blue ");
Serial.println(blue);
Serial.print("green ");
Serial.println(green);
Serial.print("red ");
Serial.println(red);

What do you see?

Issue fixed, as you've said, the values were going negative, I've simply constrained them 0, 255 :slight_smile:
Thanks for help :slight_smile:

:slight_smile: