2

My LCD info is called from an IF statement but since it's clock and temperature info i don't want to just show the information but update the information constantly in real time. i don't want to use (while) loop or any other "code-blocking".

Here's the whole 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());
        lcdScroll = HIGH;
      }
      else{
        lcd.setCursor(0,0);
        lcd.print(rtc.getDOWStr());
        lcd.print("   ");
        lcd.print(rtc.getDateStr());
        lcdScroll = LOW;
      }
       }
       oldSwitchState = newSwitchState;
    }
}

How can i update the functions inside the if(lcdScroll == LOW){} and else{} statements constantly to show on display?

  • please use true and false as boolean values – Juraj Jun 17 at 19:01
  • @Juraj thanks but does it make any difference? – Hamed Jun 17 at 19:07
  • 1
    I'm not clear on what your problem is - I don't see any blocking code here - you test the clock and the switches and either act on them or don't, which is appropriate. Also, what do you mean by "How to repeat an IF statement"? – JRobert Jun 17 at 21:33
  • 1
    Also, do not compare values with true, false, 1, 0, or HIGH or LOW, just write if (variable), by default all nonzero values will result in executing the if statement. – Michel Keijzers Jun 17 at 21:42
  • well your void loop() is already a while loop.....What is it that you are wanting to repeat that is not already happening? – Chad G Jun 17 at 22:49
2

Like you suggested in your comment, you can implement a state machine, but the simplest would probably be setting a flag bool a_flag in this if(lcdScroll==LOW) clause and checking if(a_flag) in the highest scope of the loop() section. This is where you print("test");.

In respect to your code this will look like:

bool a_flag=false;
void loop() {
  // some irrelevant stuff
  if ( newSwitchState != oldSwitchState) {
    // some stuff
    if ( lcdScroll == LOW ){
      // stuff you would handle here
      a_flag=true;
    }
    else {
      // stuff you would handle here
      a_flag=false;
    }
  }
  if (a_flag) Serial.println("lcdScroll is LOW");
  else Serial.println("lcdScroll is HIGH");
}

Beside this general solution you could simply do the following in your specific case:

loop(){

  // your actual code

  if (lcdScroll) Serial.println("lcdScroll is LOW");
  else Serial.println("lcdScroll is HIGH");
}
  • i'm very new to this can you please implant that in the code so i can get the whole idea? – Hamed Jun 18 at 9:02
  • @newbie You need to clear this flag somewhere and I'm not sure where (probably in the corresponding else{}). I'll add an implementation of this soon... – Sim Son Jun 18 at 9:38
  • See my example implementation. This will continuously print the corresponding message. Note also that the backlight will behave erronuously when millis runs over! – Sim Son Jun 18 at 12:22
  • i asked this question both in reddit and arduino forum and they didn't/couldn't give me an answer. you sir are a legend. thank you and god bless you. – Hamed Jun 18 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.