20x4 I2C LCD Flashing between Menus

Hi All,

I completed this model on Wokwi.
Code is in the link.
Have made it this morning and the LCD screen is flicking between the menu options.






This occured on the simulation when I removed the resistors however was solved when I returned them.

I would appreciate any feedback.

Thanks again.

Hello arduinoidea
Post your sketch, well formated, with comments and in so called code tags "</>" and a none-Fritzing schematic to see how we can help.
Have a nice day and enjoy programming in C++ and learning.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.

Hi Paulpaulson,

I have been trying to create a schematic using various software (Eagle, Arduino Cloud) and often some do not have the components I have (LCD 20x$ with I2C) or they're simply difficult to use (Eagle). However, if you click on the link, it is all right there.

Hope you are still able to help.

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

// Relay Delay Time //

int rt = 1000;

//On Time and Off Time

const int OnHour = 5;
const int OnMin = 30;
const int OnSec = 00;

const int OffHour = 5;
const int OffMin = 31;
const int OffSec = 00;

int x = OffMin - OnMin;
int y = 0;

const int OnHour1 = 6;
const int OnMin1 = 15;

const int OffHour1 = 6;
const int OffMin1 = 16;
const int OffSec1 = 00;

int c = OffMin1 - OnMin1;
int v = 0;

// End of On and Off

// Relay Pin Allocation
const int RELAY_PIN = 3;

bool isAlarm = false, isTimer = false, isStartTime2 = false;
const int led = 13;
const int pushbtn1 = 9; 
const int pushbtn2 = 8;
const int pushbtn3 = 7;
const int pushbtn4 = 6;
////////////////************* Manual Button Code *********************///////////////////////
const int manualButton = 5;
////////////////************* Manual Button Code *********************///////////////////////

////////////////************* Power On Start Time Code *********************///////////////////////
int hour=4, minute=30, second=0, pis=0, mls=0, lastTime=0, now;
int hourAlarm=5, minuteAlarm=30, secondAlarm=0;
int hourStartTime2=0, minuteStartTime2=0, secondStartTime2=0;
int mode=0;
int flag = 0, flagAlarm = 0;  //indicates 0 = AM, 1 = PM
////////////////************* Power On Start Time Code *********************///////////////////////

int btnstate1; //indicates pushbtn's state
int btnstate2;
int btnstate3;
int btnstate4;

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.begin(16, 2);
  pinMode(pushbtn2, INPUT);
  pinMode(pushbtn3, INPUT);
  pinMode(pushbtn4, INPUT);
  pinMode(pushbtn1, INPUT);
  ////////////////************* Manual Button Code *********************///////////////////////
  pinMode(manualButton, INPUT);
  pinMode(13, OUTPUT);
////////////////************* Manual Button Code *********************///////////////////////
  pinMode(led, OUTPUT);
  Serial.begin(9600);

}

void loop(){
  if(mode==0)clock();
  else if(mode==1)setTime();
  else if(mode==2)setAlarm();
  else if(mode==3)setTimer();
  else if(mode==4)setAlarm();

////////////////************* Manual Button Code *********************///////////////////////
digitalWrite(3, digitalRead(5));
////////////////************* Manual Button Code *********************///////////////////////

////////////////********* Start Time (alarm) 1 Relay On Code *******///////////

if(hour == hourAlarm && minute == minuteAlarm && second == secondAlarm)
for (y= 0; y <x*60/7; y++)
{ digitalWrite(RELAY_PIN, HIGH);
 delay(rt);
digitalWrite(RELAY_PIN, LOW);
 delay(rt);
 }
 {
  delay(rt);
digitalWrite(RELAY_PIN, LOW);
}
}
////////////////********* Start Time (alarm) 1 Relay On Code *******///////////

//////////****************CHECKING FOR NEW SCREEN***************//////////
void checkState(){
  btnstate1=digitalRead(pushbtn1);
  btnstate2=digitalRead(pushbtn2);
  btnstate3=digitalRead(pushbtn3);
  btnstate4=digitalRead(pushbtn4);
  if(btnstate1|btnstate2|btnstate3|btnstate4) {
     if(btnstate1==1){
       mode=1;
     }
     if(btnstate2==1){
       mode=2;
     }
     if(btnstate3==1){
       mode=3;
     }
     if(btnstate4==1){
       mode=4;
     }
    }
    else {
       mode=0;
    }
}

//////////****************CLOCK***************//////////
void clock(){

    lcd.setCursor(0,2);
    lcd.print("On Time ");
    lcd.print(hourAlarm);
    lcd.print(":");
    lcd.print(minuteAlarm);
    lcd.print(":");
    lcd.print(secondAlarm);
    if(flag==0) lcd.print(" AM");
    if(flag==1) lcd.print(" PM");

  lcd.setCursor(0,0);
  lcd.print("Current Time");
  lcd.setCursor(0,1);
    if(hour<10)lcd.print("0");
  lcd.print(hour);
  lcd.print(":");
  if(minute<10)lcd.print("0");
  lcd.print(minute);
  lcd.print(":");
  if(second<10)lcd.print("0");
  lcd.print(second);

 if(flag==0) lcd.print(" AM");
 if(flag==1) lcd.print(" PM");
 
 checkState();
 delay(200);
 runningClock(&hour, &minute, &second, &now, &lastTime, &mls, &flag);
}

void runningClock(int* _hour,int* _minute, int* _second, int* _now, int* _lastTime, int* _mls, int* _flag){
 int increHour=0; 
 *_now=(millis());
 *_mls+=(*_now-*_lastTime);
 *_lastTime=*_now;

 if(*_mls >= 1000){
  *_second+=(*_mls/1000); 
  *_mls%=1000;
  }
 if(*_second >= 60){
   *_minute+=*_second/60;
   *_second%=60;
  }
 if(*_minute >= 60){
   increHour=*_minute/60;
    *_minute%=60;
  }
  if(increHour%24==0); //hour as before
  else {
    increHour%=24;
    if(increHour<12){
      if(*_hour==12){
        *_hour=increHour;
      }
      else if(*_hour+increHour>=12){
        *_hour-=12;
        *_flag=*_flag==0?1:0;
      }
    }
    else{
      *_hour=((*_hour+increHour)%12);
      *_flag=*_flag==0?1:0;
      
    }
  if(*_flag==0 & *_hour==12)*_hour=0;
  else if(*_flag==1 & *_hour==0)*_hour=12;
 }
}


//////////****************TIMESETTING***************//////////
void setTime() {
  int *main_var;
  int X, Y;
  lcd.clear();
  
  int currentChangeIn = 1; //1 is hour 2 is minute and so on
  while(true) 
  {
    lcd.setCursor(0,0);
    lcd.print("Set Time");
    lcd.setCursor(0,1);
    if(hour<10)lcd.print("0");
    lcd.print(hour);
    lcd.print(":");
    if(minute<10)lcd.print("0");
    lcd.print(minute);
    lcd.print(":");
    if(second<10)lcd.print("0");
    lcd.print(second);

    if(flag==0) lcd.print(" AM");
    if(flag==1) lcd.print(" PM");
    
    btnstate1=digitalRead(pushbtn1);
    btnstate2=digitalRead(pushbtn2);
    btnstate3=digitalRead(pushbtn3);
    btnstate4=digitalRead(pushbtn4);
    if(btnstate1==1){
      mode=0;
      delay(200);
      break;
    }
    if(btnstate3==1){
      if(currentChangeIn==3)currentChangeIn=0;
      currentChangeIn+=1;
      delay(200);
    }
    if(currentChangeIn==1){
      main_var = &hour;
      X = 0;
      Y = 1;
     
      if(digitalRead(pushbtn2)==1){
        hour+=1;
        if(hour==13)hour=1;
        else if(hour==12 & flag==0)hour=0;
        delay(200);
      }
    }
    if(currentChangeIn==2){
      main_var = &minute;
      X = 3;
      Y = 1;
     
      if(digitalRead(pushbtn2)==1){
        minute+=1;
        if(minute==60)minute=0;
        delay(200);
      }
    }
    if(currentChangeIn==3){
      main_var = &second;
      X = 6;
      Y = 1;
     
      if(digitalRead(pushbtn2)==1){
        second+=1;
        if(second==60)second=0;
        delay(200);
      }
    }
    if(btnstate4==1){
      flag=flag==0?1:0;
      if(flag==0 & hour==12)hour=0;
      else if(flag==1 & hour==0)hour=12;
      delay(200);
    }
    blink(*main_var,X,Y);
  runningClock(&hour, &minute, &second, &now, &lastTime, &mls, &flag);
  }
 lcd.clear();
}



//////////********Button 2 ********Start Time 1***************//////////
void setAlarm() {
  int *main_var;
  int X, Y;
  lcd.clear();
  
  int currentChangeIn = 1; //1 is hour 2 is minute and so on
  while(true) 
  {
    lcd.setCursor(0,0);
    lcd.print("Start Time 1");
    lcd.setCursor(0,1);
    if(hourAlarm<10)lcd.print("0");
    lcd.print(hourAlarm);
    lcd.print(":");
    if(minuteAlarm<10)lcd.print("0");
    lcd.print(minuteAlarm);
    lcd.print(":");
    if(secondAlarm<10)lcd.print("0");
    lcd.print(secondAlarm);

    if(flagAlarm==0) lcd.print(" AM");
    if(flagAlarm==1) lcd.print(" PM");
    
    btnstate1=digitalRead(pushbtn1);
    btnstate2=digitalRead(pushbtn2);
    btnstate3=digitalRead(pushbtn3);
    btnstate4=digitalRead(pushbtn4);
    if(btnstate1==1){
      mode=0;
      delay(200);
      break;
    }
    if(btnstate3==1){
      if(currentChangeIn==3)currentChangeIn=0;
      currentChangeIn+=1;
      delay(200);
    }
    if(currentChangeIn==1){
      main_var=&hourAlarm;
      X = 0;
      Y = 1;
     
      if(digitalRead(pushbtn2)==1){
        hourAlarm+=1;
        if(hourAlarm==13)hourAlarm=1;
        else if(hourAlarm==12 & flagAlarm==0)hourAlarm=0;
        delay(200);
      }
    }
    if(currentChangeIn==2){
      main_var=&minuteAlarm;
      X = 3;
      Y = 1;
     
      if(digitalRead(pushbtn2)==1){
        minuteAlarm+=1;
        if(minuteAlarm==60)minuteAlarm=0;
        delay(200);
      }
    }
    if(currentChangeIn==3){
      main_var=&secondAlarm;
      X = 6;
      Y = 1;
     
      if(digitalRead(pushbtn2)==1){
        secondAlarm+=1;
        if(secondAlarm==60)secondAlarm=0;
        delay(200);
      }
    }
    if(btnstate4==1){
      flagAlarm=flagAlarm==0?1:0;
      if(flagAlarm==0 & hourAlarm==12)hourAlarm=0;
      else if(flagAlarm==1 & hourAlarm==0)hourAlarm=12;
      delay(200);
    }
    blink(*main_var,X,Y);
  }
  isAlarm=true;
  runningClock(&hour, &minute, &second, &now, &lastTime, &mls, &flag);
lcd.clear();
}


//////////******** Button 3 ********TIMER***************//////////
void setTimer() {
  int minuteTimer=0, secondTimer=0, mlsTimer=0, lastTimeTimer;
  int *main_var;
  int X, Y;
  lcd.clear();
  int brFlag=0; //blinking stopper
  int currentChangeIn = 2; //1 is hour 2 is minute and so on
  while(true) 
  {
    lcd.setCursor(0,0);
    lcd.print("Set Run Time");
    lcd.setCursor(0,1);
    if(minuteTimer<10)lcd.print("0");
    lcd.print(minuteTimer);
    lcd.print(":");
    if(secondTimer<10)lcd.print("0");
    lcd.print(secondTimer);
    
    btnstate1=digitalRead(pushbtn1);
    btnstate2=digitalRead(pushbtn2);
    btnstate3=digitalRead(pushbtn3);
    btnstate4=digitalRead(pushbtn4);
    if(btnstate1==1){
      mode=0;
      delay(200);
      break;
    }
    if(btnstate3==1){
      currentChangeIn+=1;
      if(currentChangeIn==4)currentChangeIn=2;
      delay(200);
    }
    if(currentChangeIn==2){
      main_var=&minuteTimer;
      X = 0;
      Y = 1;
     
      if(digitalRead(pushbtn2)==1){
        minuteTimer+=1;
        delay(200);
      }
    }
    if(currentChangeIn==3){
      main_var=&secondTimer;
      X = 3;
      Y = 1;
     
      if(digitalRead(pushbtn2)==1){
        secondTimer+=1;
        if(secondTimer==60)secondTimer=0;
        delay(200);
      }
    }
    if(btnstate4==1){
      if(!isTimer)isTimer=true;
      else{
        isTimer=false;
      }
      brFlag=1;
      lastTimeTimer=millis();
      delay(200);
    }
    if(isTimer)runningTimer(&minuteTimer, &secondTimer, &mlsTimer, &lastTimeTimer);
    if(minuteTimer==0&secondTimer==0)brFlag=0;
    if(brFlag==0)blink(*main_var,X,Y);
    delay(200);
    runningClock(&hour, &minute, &second, &now, &lastTime, &mls, &flag);
  }
  lcd.clear();
}

void runningTimer(int* _minute, int* _second, int* _mls, int* _lastTime){
 int _now=millis();
 *_mls+=(_now-*_lastTime);
 *_lastTime=_now;
 if(*_mls >= 1000){
  *_second-=(*_mls/1000); 
  *_mls%=1000;
  }
 if(*_second <= 0){
   if(*_minute == 0 & *_second < 0){
    isTimer=false;
    digitalWrite(led, HIGH);
    delay(3000);
    digitalWrite(led, LOW);
    *_second=0;
  }
   else if(*_minute>0){
    *_minute-=1;
    *_second=59;
   }
 }
}



//////////****************COMMON FUNCTION***************//////////
void blink(int main_var, int X, int Y){
  lcd.setCursor(X, Y);
    if(pis == 0){ 
      if(main_var<10)lcd.print("0");
      lcd.print(main_var);
      delay(200);
    }
    if(pis == 1){
    lcd.print("  ");
    delay(200);
    }
    pis=(pis+1)%2;
}

//////////****************THANK YOU***************//////////


Where you have this:

  if(btnstate1|btnstate2|btnstate3|btnstate4) {
     if(btnstate1==1){
       mode=1;
     }
     if(btnstate2==1){
       mode=2;
     }
     if(btnstate3==1){
       mode=3;
     }
     if(btnstate4==1){
       mode=4;
     }
    }
    else {
       mode=0;
    }

Consider using switch...case - Arduino Reference instead.

You have a function:
void checkState(){

Then in your functions:
void setTime()
void setAlarm()
void setTimer()
You have the same code as in checkState. The idea of creating a function is you don't then need to keep repeating the same thing over and over,you just call the function.

Ideally break all the code down into separate functions so the only thing you have in loop() is calls to the various functions, nothing else. Your first function in loop should be to something that checks and saves the current state of everything, by which I mean the buttons and the switch and probably the time, and then saves them in global variables ready for the following functions to use.

To find out what is wrong insert some Serial.print()s into the code at various points and watch the serial monitor to see where the code goes and what the values are of the various variables at appropriate points in the code.

The use of lcd.clear(); in the loop() function can cause flicker. Overwrite old data with spaces instead of using clear. Or use fixed length strings and only write to the LCD when data changes.

Solved.
The code seems to be working okay.
As pointed out, isn't perfect and really appreciate the advice.

The toggle switch I had install had metallics across the vera board causing the Arduino to become confused. Drilled out the vera board and solved.
Worked as the simulator shows.

Thanks again for your time.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.