Clock keeps flashing with delay

I wrote a function to display the time on a TFT touch screen. The problem is that the numeric values keep blinking every second and the buttons are no longer responsive. I’m writing the delay for when the “minute” and “hour” is changed, so why does it blink every second? Similarly, the “hour” is doing the same thing.

int old_hour;
int old_minute;

void CLOCK(){
  
  DateTime now = RTC.now();
     
  tft.setCursor(100, 0);
  tft.setTextColor(WHITE); tft.setTextSize(4);

   tft.print(now.hour(), DEC);
   tft.print(":");
 
 
   if (old_hour != now.hour()){
    tft.setCursor(100, 0);
   tft.fillRect(100, 0, 50, 40, BLACK);
   delay(10);
   tft.print(now.hour(),DEC);
   tft.print(":");
   delay(1000);
 }


  
    
  if (now.minute() < 10) {
    tft.setCursor(170, 0);  
    tft.setTextColor(WHITE); tft.setTextSize(4);
    tft.print("0");
    tft.setCursor(200, 0);  
    tft.setTextColor(WHITE); tft.setTextSize(4);
    tft.print(now.minute(), DEC);
    delay(1000);  
    tft.fillRect(170, 0, 50, 40, BLACK);
    delay(10);
    
          } 
       
      
          
    else if (old_minute != now.minute()){
         tft.fillRect(170, 0, 50, 40, BLACK);
         delay(10);
         tft.setCursor(170, 0);
         tft.print(now.minute(), DEC);
         delay(1000);}
         

}

-JQ

You answer your own question in the title.

Clock keeps flashing with delay.

Yes. you are quite right.

That's why no one in their right mind ever uses delay for anything other than a millisecond or two.

Read the BlinkWithoutDelay example.

Hi, I tried using the blink without delay, but I’m not too sure I’m using it correctly. Because when the hour changed on the RTC, the visual on the touch screen stayed the same. Also, when I assigned a value greater than 1000 for the long interval_min, it wouldn’t show on the screen either. Here is the code I edited.

long oldMillisT = 0;       
long interval_hour = 2000;  
long interval_min = 1000;

int old_hour;
int old_minute;
int old_second;

void CLOCK(){
  unsigned long currentMillisT = millis();
  DateTime now = RTC.now();
  
    
  tft.setCursor(100, 0);
  tft.setTextColor(WHITE); tft.setTextSize(4);
  if(currentMillisT - oldMillisT > interval_hour) {
    oldMillisT = currentMillisT;   
  tft.print(now.hour(), DEC);
  tft.print(":");
  
  if (old_hour != now.hour()){
    tft.fillRect(100, 0, 50, 40, BLACK);
    tft.setCursor(100, 0);
    tft.print(now.hour(), DEC);

    }
  }
   

  if(currentMillisT - oldMillisT > interval_min) {
    oldMillisT = currentMillisT;   
   if (now.minute() < 10) {
          
         tft.setCursor(170, 0);  
         tft.setTextColor(WHITE); tft.setTextSize(4);
         tft.print("0");
         tft.setCursor(200, 0);  
         tft.setTextColor(WHITE); tft.setTextSize(4);
         tft.print(now.minute(), DEC);
         tft.fillRect(170, 0, 50, 40, BLACK);
         
       
   
 }
          
      else if (old_minute != now.minute()){   
         tft.fillRect(170, 0, 50, 40, BLACK);
         delay(10);
         tft.setCursor(170, 0);
         tft.setTextColor(WHITE); tft.setTextSize(4);
         tft.print(now.minute(), DEC);
       
       
      }
         
      
      }
}

-JQ

Why not just update the whole time in one fell swoop every second? That would be so much simpler…

And don’t faff with “if < 10 add a zero” rubbish - just format the time in a string with sprintf:

char timestr[10];
sprintf(timestr, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
tft.print(timestr);

Hi, the code you wrote blew my mind. Thank you, I would never have known to write it like that. But now I'm trying to have it replace the previous value with the new one (because the numbers are just stacking on top) and it says that C++ forbids comparison between pointer and integer. I'm new to programming and have never worked with sprintf.

Heres the code I tried to write

long oldMillish = 0;
long interval_hour = 1000;  

int old_timestr;


void CLOCK(){
   unsigned long currentMillish = millis();
  DateTime now = RTC.now();
  
  char timestr[10];
sprintf(timestr, "%02d:%02d", now.hour(), now.minute());
 //  tft.setCursor(100, 0);
 //  tft.setTextColor(WHITE); tft.setTextSize(4);
 //  tft.print(timestr);
 if(currentMillish - oldMillish > interval_hour) {
    oldMillish = currentMillish;
   if(timestr == old_timestr) {
     tft.fillRect(100, 0, 200, 40, BLACK);
 }
  
else {tft.setTextColor(WHITE); tft.setTextSize(4);
   tft.print(timestr);
}

     
  
    }

-JQ

Today i was going through the millis() function in the arduino forum. I cannot understand why millis() is divided by 1000?? kindly explain the reason

 // print the number of seconds since reset:
  lcd.print(millis()/1000);

Thanks

Hi Nightcrawler, I’m pretty sure its to show the numeric value for 1 second, because there are 1000 milliseconds in one second, so it takes the millis() value and divides it by 1000, to show how much it is in seconds.

-JQ

oops... You are right. It is clearly stated that " // print the number of seconds since reset:" , overlooked that. :cold_sweat:

Thanks for explaing it in the simplest way. cheers... :)

Np, glad I could help :)

But now I'm trying to have it replace the previous value with the new one (because the numbers are just stacking on top) and it says that C++ forbids comparison between pointer and integer.

What is "it" that is going to replace the previous value with the new one? Previous value of what with the new value of what? How is "it" going to do that?

int old_timestr;
  char timestr[10];
   if(timestr == old_timestr) {

Well, what do you know. The compiler is right. What is old_timestr supposed to represent?