Why is my first "if" line only blinking?

I've made a basic thermostat code; when the temp is too low or too high (>40 and <-20), pin 13 (>40) and 7 (<-20) is supposed to turn high and stay high until the temp turns "normal" (-19 to 39). But when the temp is too high (which is the first "if" line in my code), it only "pulses" (blinks). I've tried switching place with the other "if" line, but the same happens, now with the "if (T <= -20)" line.
why is it happening?
here's the code:

#include <LiquidCrystal.h>
int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(7,OUTPUT);
}

void loop() {

  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T = T - 273.15;
  
  //this is where it only "pulses"
  if (T >= 40) {
   lcd.print(" Too hot");
   delay (500);
   lcd.setCursor(1, 1);
   lcd.print("Temp = ");
   lcd.print(T);
   lcd.print(" C");
   delay (500);
   digitalWrite(7, HIGH);
  }
  else {
   digitalWrite(7, LOW);
  }

  //this works as it's supposed to
  if (T <= -20) {
   lcd.print(" Too cold");
   delay (500);
   lcd.setCursor(1, 1);
   lcd.print("Temp = ");
   lcd.print(T);
   lcd.print(" C");
   delay (500);
   digitalWrite(13, HIGH);
  }
  else {
   digitalWrite(13, LOW);
  }
  lcd.setCursor(1, 1);
  lcd.print("Temp = ");
  lcd.print(T);
  lcd.print(" C");
  delay(1000);
  lcd.clear();
  delay(0.1);
}

here's another code (less complicated i guess) with the same problem

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

void setup() {

Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
}

void loop() {

Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
Tc = T - 273.15;

Serial.print("Temperature: ");
Serial.print(Tc);
Serial.println(" C");
delay(500);

if(Tc<= -20){
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
digitalWrite(11,LOW);
}
else{
digitalWrite(10,LOW);
digitalWrite(13,LOW);
digitalWrite(11,HIGH);
}

if(Tc>= 30){
digitalWrite(10,HIGH);
digitalWrite(13,HIGH);
digitalWrite(11,LOW);
}
else{
digitalWrite(10,LOW);
digitalWrite(13,LOW);
digitalWrite(11,HIGH);
}
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Please post your code in tags... if you don't it's a real pain for anyone that wants to help, because probably the first thing they normally will do is try and copy/paste your code into their IDE... it not easy if you've posted it as formatted text so you won't get much help.

It's also helpful to show a diagram / schematic of how everything is hooked up.

and what Arduino are you using? ... also helpful.

The second if has an else that will get executed if the T° is not <= -20°

Also you call this at every loop

so you issue a clear
(and the delay function does not accept a float)

you need only one series of if / else if / else if / else to ensure there are no 2 conditions caught

if (Tc > 40) {
  // code when strictly above 40
  ...
} else if (Tc < -20) {
  // code when strictly  below -20
  ...
} else {
  // code when in between  [-20, 40]
  ...
}

try to not issue a clear if you can. Update the display only when it's due (eg T° change for example), and repaint only the parts that are necessary. that will get rid of the blinking effect.

1 Like

What exactly blinks? Do you have LEDs connected? or are you talking about the LCD?

You don't seem to have an lcd.begin statement anywhere?

Hi,
If your LCD blinks. it is because of;

 lcd.clear();

Remove that line and simply over write your variable on the LCD screen with blanks, just before displaying the variable again.

Tom... :grinning: :+1: :coffee: :australia:

thanks