I am having some trouble with my code need help ASAP noob here

I am making a simple circuit which uses a lm35 temperature sensor, and 3 LEDS ranging from RED, GREEN and BLUE. I have combined 2 different pieces of code to make this work. The lights are supposed to change depending on the temperature. BUT even if it goes below 21 degrees celsius it still prints as "it's hot" i dont see whats the problem help will be appreciated.

const int hot = 23; //set hot parameter
const int cold = 21; //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.
void setup() {
pinMode(A2, INPUT); //sensor
pinMode(2, OUTPUT); //blue
pinMode(3, OUTPUT); //green
pinMode(4, OUTPUT); //red
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)");
}

if (val < cold) { //cold
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
Serial.println(" It's Cold.");
}
else if (val >= hot) { //hot
digitalWrite(2, LOW);
  digitalWrite(3, LOW);
digitalWrite(4, HIGH);
Serial.println(" It's Hot.");
}
else { //fine
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, 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
}

Welcome

Because val is the raw value returned by analogRead.

You have to store the result of 5.0 * val * 100 / 1024 in a variable temperature, and use this variable instead of val, for comparison with hot and cold.

I am a complete noob here could you please show me how that code will look like thanks. I understand what you mean but have no clue on how to execute it

@guix already showed you what it will look like
Try for yourself, and if you get stuck, ask back here.

i tried this edited version after guix's help but even though when the temp is above 20 it still prints as "It's cold"

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.
int temp = (5.0 * val * 100 / 1024);
void setup() {
pinMode(A2, INPUT); //sensor
pinMode(2, OUTPUT); //blue
pinMode(3, OUTPUT); //green
pinMode(4, OUTPUT); //red
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)");

if (temp < cold) { //cold
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
Serial.println(" It's Cold.");
}
else if (temp >= hot) { //hot
digitalWrite(2, LOW);
  digitalWrite(3, LOW);
digitalWrite(4, HIGH);
Serial.println(" It's Hot.");
}
else { //fine
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, 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
}

...seems to be the key point here

Take a close look at how variable 'temp' gets its value...

I was looking at the conditional serial print.

That too. There are probably more mistakes - coffee to drink here, though...

i understand what you guys are syaing, i have to store the return value of the formula as temp and then use that to compare with hot and cold, ive tried doing it but no luck.

You need to do it every time you take a reading.

Any changes to your code, please repost the revised sketch so we can see what you did.

Move int temp = (5.0 * val * 100 / 1024); in the appropriate place ( in loop(), after val has been updated.. :slight_smile: )

Thank you guys

Nope.

here is the final working code

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.

void setup() {
pinMode(A2, INPUT); //sensor
pinMode(2, OUTPUT); //blue
pinMode(3, OUTPUT); //green
pinMode(4, OUTPUT); //red
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(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
Serial.println(" It's Cold.");
}
else if (temp >= hot) { //hot
digitalWrite(2, LOW);
  digitalWrite(3, LOW);
digitalWrite(4, HIGH);
Serial.println(" It's Hot.");
}
else { //fine
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, 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
}

Yay!

1 Like

You are right... :slight_smile:

A small point, but you should do the calculation first then print the result of the calculation instead of doing the calculation twice.

2 Likes

No. It should be neither. DON'T use pinMode() for pins that are only used for analogRead().

Yes, lots of people do it. Yes, it is harmless, MOST of the time. However, on the Arduino Nano using pinMode() on pin A6 or A7 will go off the end of the digital_pin_to_port_PGM[] and digital_pin_to_bit_mask_PGM[] tables.

1 Like