Need help with my code basic

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
}

I THINK IVE FIXED THE PROBLEM BY INSTEAD OF HAVING

if (temp < cold) { //cold
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, HIGH);
    Serial.println(" It's Cold.");

i changed it to

if (temp < cold) { //cold
    digitalWrite(RED_PIN, HIGH);
    digitalWrite(GREEN_PIN, HIGH);
    digitalWrite(BLUE_PIN, LOW);
    Serial.println(" It's Cold.");

Re-write the above two line as follows:

Serial.print((int)5.0 * val * 100 / 1024); //please see note at bottom regarding this formula.
int temp = (int)(5.0 * val * 100 / 1024);
1 Like

I would recommend:

// Temperature is volts * 100
const float Vref = 5.0;
int tempC = ((val * Vref) / 1024.0) * 100.0;
Serial.print(tempC); 

My academic curiosity:
1. By int tempC, I have decalred a 16-bit (2-byte) storage area/variable.
2. ((val * Vref) / 1024.0) * 100.0 will be evaluated to 32-bit (8-byte) binarya32 formatted floating point number having integer part and fractional part.

3. Then should I not impose casting (the int) on Step-2 to get the 16-bit integer part and assign it to variable tempC of Step-1?

Conversion is done as part of the assignment operation. The result on the right is converted to the type on the left. The conversion from 'float' to 'int' is done automatically.

Beautiful!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.