Hi guys,
Im pretty new to coding with an arduino. I have made a circuit which changes the colour of the RBG light based on the LM35 temp sensor. My code works fine except for one thing, when the temperature falls below 20 degrees it changes to a red colour instead of blue.
Help will be appreciated.
const int hot = 23; //set hot parameter
const int cold = 20; //set cold parameter
int varPin = A0; //A0 is the analog in pin
int val = 0; //This will store the current value from the sensor
int oldVal = 0; //This will store the last value from the sensor to compare.
const int RED_PIN = 9;//the digital pin for red pin
const int GREEN_PIN = 10;//the digital pin for green pin
const int BLUE_PIN = 11;//the digital pin for blue pin
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
oldVal = val; //Store the last value in the variable
val = analogRead(varPin); //Store the new value in its variable
if (oldVal != val) //If the temperature has changed, tell us the new temp
{
Serial.print("Temp: ");
Serial.print(5.0 * val * 100 / 1024); //please see note at bottom regarding this formula.
Serial.println(" Degrees(Celsius)");
int temp = (5.0 * val * 100 / 1024);
if (temp < cold) { //cold
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
Serial.println(" It's Cold.");
}
else if (temp >= hot) { //hot
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
Serial.println(" It's Hot.");
}
else { //fine
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
Serial.println(" It's Fine.");
}
}
delay(500); //Wait half a second, then do it again. (This allows the line to settle between reads. Not really required, but it does remove a bit of jitter
}