I am trying to make a project where a number is sent to serial via a python program. Then an Arduino is to read the number and perform a specific function (here printing something on an lcd) for a specified duration. However the code doesn't work as intended.
Here is the code running on the Arduino:
#include <LiquidCrystal.h>
boolean flag1, flag2;
const int rs = 3, en = 2, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
String a;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("hello");
delay(1000);
lcd.clear();
}
void loop() {
lcd.setCursor(0, 0);
if(Serial.available())
{
int c = Serial.read();
if(c == 49) { // 1 is pressed
flag1 = true;
flag2 = false;
}
else if(c == 50) { // 2 is pressed
flag2 = true;
flag1 = false;
}
}
if(flag1) {
lcd.write("location 1");
delay(3000);
lcd.clear();
}
if(flag2) {
lcd.write("location 2");
delay(3000);
lcd.clear();
}
delay(50);
}
When the number is pressed it shows the text however it keeps it on the screen forever. I need it to run once (for the specified delay duration) then reset to blank screen. When another number is entered, it changes the text but again keeps it on forever.
Clear the flag after reading it:
if(flag1) {
flag1 = false;
// ...