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?
1 Answer 1
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?ElectronSurf– ElectronSurf2019年06月18日 09:02:10 +00:00Commented 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...Sim Son– Sim Son2019年06月18日 09:38:49 +00:00Commented 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!Sim Son– Sim Son2019年06月18日 12:22:35 +00:00Commented 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.ElectronSurf– ElectronSurf2019年06月18日 14:29:06 +00:00Commented Jun 18, 2019 at 14:29
true
andfalse
as boolean valuesif (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.