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?

asked Jun 17, 2019 at 18:44
9
  • please use true and false as boolean values Commented Jun 17, 2019 at 19:01
  • 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"? Commented Jun 17, 2019 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. Commented Jun 17, 2019 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? Commented Jun 17, 2019 at 22:49
  • @JRobert the problem is code inside if (lcdScroll == LOW){} only runs once but if i want to have the time to update in real time, time functions have to be in a loop not only run once. Commented Jun 18, 2019 at 5:55

1 Answer 1

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");
}
answered Jun 18, 2019 at 8:16
4
  • i'm very new to this can you please implant that in the code so i can get the whole idea? Commented Jun 18, 2019 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... Commented Jun 18, 2019 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! Commented Jun 18, 2019 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. Commented Jun 18, 2019 at 14:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.