Why time and temprature is not updating on the display?

Why the time and temp functions does not update in real time on the LCD display when they’re in a if statement but they update outside, in loop function?

what’s the solution to this without adding “blocking code”?

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


#define ONE_WIRE_BUS 2
int Heater= 3;
int Switch1= 4;

Time t;
LiquidCrystal_I2C lcd(0x27, 16, 2);
DS3231  rtc(SDA, SCL);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Push button

boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean lcdScroll = LOW;
boolean lcdBacklight = LOW;
boolean lcdWelcome = LOW;

unsigned long previousMillis = 0;
const long BacklightTime = 10000;

void setup() {
  Serial.begin(115200);
  pinMode(Heater, OUTPUT);
  pinMode(Switch1, INPUT);
  lcd.begin();
  rtc.begin();
}
void loop() {
  float testTemp = sensors.getTempCByIndex(0);
  sensors.requestTemperatures();
  t = rtc.getTime();
  if(lcdWelcome == LOW){
      lcd.setCursor(0,0);
      lcd.print("Welcome, Admin!");
      lcd.setCursor(0,1);
      lcd.print("press Start");
  }

  // LCD
  newSwitchState = digitalRead(Switch1);
    if (millis() - previousMillis >= BacklightTime) {
      lcd.noBacklight();
      lcdBacklight = LOW;
    }
    else if ( lcdBacklight == LOW ){
      lcd.backlight();
      lcdBacklight = HIGH;
    }
    if ( newSwitchState != oldSwitchState ){
       if ( newSwitchState == HIGH ){
       previousMillis = millis();
       lcdWelcome = HIGH;
       lcd.clear();
      if ( lcdScroll == LOW ){
        lcd.setCursor(0,0);
        lcd.print(testTemp);
        lcd.print("   ");
        lcd.print(rtc.getTimeStr());
        lcd.setCursor(0,1);
        lcd.print(rtc.getDOWStr());
        lcd.print("   ");
        lcd.print(rtc.getDateStr());
        lcdScroll = HIGH;
      }
      else{
        lcd.clear();
        lcdScroll = LOW;
      }
       }
       oldSwitchState = newSwitchState;
    }
}

Okay, so after some research i found out that i need to repeat the IF statement but i don't want to use "while" because it's a code blocking, help please?

Do you want the temperature and time to display once each time the switch is pressed, or should it update constantly while the switch is pressed?

david_2018:
Do you want the temperature and time to display once each time the switch is pressed, or should it update constantly while the switch is pressed?

i want it to update in real time, yes constantly.

In this portion of your code, you are comparing newSwitchState to oldSwitchState - that code is used when you only want to do something once, when the state of the switch changed from high to low, or from low to high. If you want to update the display constantly while the switch is HIGH, instead of just when it changes, then eliminated the test for the change in switch state and only use the test for newSwitchState == HIGH.

  if ( newSwitchState != oldSwitchState ) {
    if ( newSwitchState == HIGH ) {
      previousMillis = millis();
      lcdWelcome = HIGH;
      lcd.clear();
      if ( lcdScroll == LOW ) {
        lcd.setCursor(0, 0);
        lcd.print(testTemp);
        lcd.print("   ");
        lcd.print(rtc.getTimeStr());
        lcd.setCursor(0, 1);
        lcd.print(rtc.getDOWStr());
        lcd.print("   ");
        lcd.print(rtc.getDateStr());
        lcdScroll = HIGH;
      }
      else {
        lcd.clear();
        lcdScroll = LOW;
      }
    }
    oldSwitchState = newSwitchState;
  }

david_2018:
In this portion of your code, you are comparing newSwitchState to oldSwitchState - that code is used when you only want to do something once, when the state of the switch changed from high to low, or from low to high. If you want to update the display constantly while the switch is HIGH, instead of just when it changes, then eliminated the test for the change in switch state and only use the test for newSwitchState == HIGH.

i removed that part as you said and it is not updating the information on the display.

can someone please help this old man to figure this out?

Ah, the test for lcdScroll == low is keeping it from displaying more than once. Did not notice that.

david_2018:
Ah, the test for lcdScroll == low is keeping it from displaying more than once. Did not notice that.

i can't just remove that! it's part of how it supposed to work...

i can't just remove that! it's part of how it supposed to work...

How is it supposed to work?

boolean lcdScroll = LOW;

What should happen when lcdScroll == HIGH and what should happen when lcdScroll == LOW?

cattledog:
How is it supposed to work?

boolean lcdScroll = LOW;

What should happen when lcdScroll == HIGH and what should happen when lcdScroll == LOW?

it toggles between two states(shows different info on LCD) when push button pressed.

To switch screens with the button press, and then have the active screen updated while displayed, you should separate the screen selection button press from the display. You have not included any information about the alternative screen display in your code.

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


#define ONE_WIRE_BUS 2
int Heater = 3;
int Switch1 = 4;

Time t;
LiquidCrystal_I2C lcd(0x27, 16, 2);
DS3231  rtc(SDA, SCL);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Push button

boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean lcdScroll = LOW;
boolean lcdBacklight = LOW;
boolean lcdWelcome = LOW;

unsigned long previousMillis = 0;
const long BacklightTime = 10000;

void setup() {
  Serial.begin(115200);
  pinMode(Heater, OUTPUT);
  pinMode(Switch1, INPUT);
  lcd.begin();
  rtc.begin();
}
void loop()
{
  float testTemp = sensors.getTempCByIndex(0);
  sensors.requestTemperatures();
  t = rtc.getTime();
  if (lcdWelcome == LOW)
  {
    lcd.setCursor(0, 0);
    lcd.print("Welcome, Admin!");
    lcd.setCursor(0, 1);
    lcd.print("press Start");
  }

  // LCD
  newSwitchState = digitalRead(Switch1);

  if (millis() - previousMillis >= BacklightTime)
  {
    lcd.noBacklight();
    lcdBacklight = LOW;
  }
  else if (lcdBacklight == LOW )
  {
    lcd.backlight();
    lcdBacklight = HIGH;
  }

  if (newSwitchState != oldSwitchState )
  {
    if (newSwitchState == HIGH )
    {
      previousMillis = millis();
      lcdWelcome = HIGH;
      lcd.clear();
      if (lcdScroll == HIGH) lcdScroll = LOW;
      else if (lcdScroll == LOW) lcdScroll = HIGH;
    }

    if (lcdScroll == LOW )
    {
      lcd.setCursor(0, 0);
      lcd.print(testTemp);
      lcd.print("   ");
      lcd.print(rtc.getTimeStr());
      lcd.setCursor(0, 1);
      lcd.print(rtc.getDOWStr());
      lcd.print("   ");
      lcd.print(rtc.getDateStr());
      //lcdScroll = HIGH;
    }
    else
    {
      lcd.setCursor(0, 0);
      lcd.print("Second Screen");
      //lcd.clear();
      //lcdScroll = LOW;
    }
  }
oldSwitchState = newSwitchState;
}

cattledog:
To switch screens with the button press, and then have the active screen updated while displayed, you should separate the screen selection button press from the display. You have not included any information about the alternative screen display in your code.

isn’t my question and code clear?

“if (lcdScroll == LOW )” only runs once, i want it to run as long as its true over and over so that way i can have my time, date etc functions updated…

"if (lcdScroll == LOW )" only runs once, i want it to run as long as its true over and over so that way i can have my time, date etc functions updated...

Did you try the revised code provided in reply #11? The LOW screen should run as long as you do not push the button to select the HIGH screen. The button press switches between the two screens. The selected screen will run and update continuously.

isn't my question and code clear?

it toggles between two states(shows different info on LCD) when push button pressed

Not entirely to me. What do you want to be in the HIGH and LOW screens selected by the button?

cattledog:
Did you try the revised code provided in reply #11? The LOW screen should run as long as you do not push the button to select the HIGH screen. The button press switches between the two screens. The selected screen will run and update continuously.

the code you provided doesn't change anything, if we add a Serial.println("test"); to if (lcdScroll == LOW ){} you'll see it only runs (ONCE), what i want to do is if that statement is "true" go check other statements and comeback to current statement and read it again and this process goes into a loop.

cattledog:
Not entirely to me. What do you want to be in the HIGH and LOW screens selected by the button?

different time, temp and other sensors is gonna be shown every time push button pressed. and IT IS showing them but for example clock seconds is not running (not updating constantly).

i hope it is clear now.
thanks for the answers btw.

I made an error with the location of some brackets in the code provided with reply #11 and left the screen display within the selection code. :frowning:

Try this revision which actually separates them.

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


#define ONE_WIRE_BUS 2
int Heater = 3;
int Switch1 = 4;

Time t;
LiquidCrystal_I2C lcd(0x27, 16, 2);
DS3231  rtc(SDA, SCL);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Push button

boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean lcdScroll = LOW;
boolean lcdBacklight = LOW;
boolean lcdWelcome = LOW;

unsigned long previousMillis = 0;
const long BacklightTime = 10000;

void setup() {
  Serial.begin(115200);
  pinMode(Heater, OUTPUT);
  pinMode(Switch1, INPUT);
  lcd.begin();
  rtc.begin();
}
void loop()
{
  float testTemp = sensors.getTempCByIndex(0);
  sensors.requestTemperatures();
  t = rtc.getTime();
  if (lcdWelcome == LOW)
  {
    lcd.setCursor(0, 0);
    lcd.print("Welcome, Admin!");
    lcd.setCursor(0, 1);
    lcd.print("press Start");
  }

  // LCD
  newSwitchState = digitalRead(Switch1);

  if (millis() - previousMillis >= BacklightTime)
  {
    lcd.noBacklight();
    lcdBacklight = LOW;
  }
  else if (lcdBacklight == LOW )
  {
    lcd.backlight();
    lcdBacklight = HIGH;
  }
//screen selection
  if (newSwitchState != oldSwitchState )
  {
    if (newSwitchState == HIGH )
    {
      previousMillis = millis();
      lcdWelcome = HIGH;
      lcd.clear();
      if (lcdScroll == HIGH) lcdScroll = LOW;
      else if (lcdScroll == LOW) lcdScroll = HIGH;
    }
  }
 //screen display 
  if (lcdScroll == LOW )
  {
    lcd.setCursor(0, 0);
    lcd.print(testTemp);
    lcd.print("   ");
    lcd.print(rtc.getTimeStr());
    lcd.setCursor(0, 1);
    lcd.print(rtc.getDOWStr());
    lcd.print("   ");
    lcd.print(rtc.getDateStr());
    //lcdScroll = HIGH;
  }
  else
  {
    lcd.setCursor(0, 0);
    lcd.print("Second Screen");
    //lcd.clear();
    //lcdScroll = LOW;
  }

  oldSwitchState = newSwitchState;
}