Faking a glitch on LCD clock

Hello,

I've built a simple clock with Arduino Uno, LCD 16x2 and RTC DS1302 for artistic project. It displays time for one minute, and text with just seconds timer for the other one. To visualize that:
HH:MM:SS for one minute
(letter letter):(letter letter):SS for the other.
I would like to code something that pretends to be a glitch, so one second before the timing with text it would display:
(letter)H:M(letter):SS
So just second digit from hours and first digit from letters. I would be really grateful for help or maybe some suggestions how to work this out, if it's possible. Here is the code:

#include "virtuabotixRTC.h"//Library used
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
virtuabotixRTC myRTC(2, 3, 4); //The order here is DAT,CLK,RST. You can change this depending on the wiring

char timeChar [8]; //number of digits for the format
uint8_t prevSeconds = 0; //

void setup() 
{
  
  Serial.begin(9600);
  
  lcd.init();
  lcd.backlight();
  
  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year
 //myRTC.setDS1302Time(55, 37, 9, 5, 10, 12, 2021); //Here you write your actual time/date as shown above
  //but remember to "comment/remove" this function once you're done
  //The setup is done only one time and the module will continue counting it automatically
}
void loop() 
{
  
  myRTC.updateTime();
  
  if(myRTC.seconds != prevSeconds)  // Only update display if seconds have changed
  {
    prevSeconds = myRTC.seconds;
    
    // Start printing elements as individuals
    lcd.setCursor(6,0);
    lcd.print("");
    lcd.setCursor(4,1);
    
    if(myRTC.minutes %2)
    {
      sprintf(timeChar, "%02d:%02d:%02d",myRTC.hours, myRTC.minutes, myRTC.seconds);
    }
    else
    {
      sprintf(timeChar, "D0:0M:%02d", myRTC.seconds);
    }
    
    lcd.print(timeChar);
  }
}

Best regards
Lena

What have you tried ?

Try this...

#include "virtuabotixRTC.h"//Library used
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
virtuabotixRTC myRTC(2, 3, 4); //The order here is DAT,CLK,RST. You can change this depending on the wiring

char timeChar [8]; //number of digits for the format
uint8_t prevSeconds = 0; //

void setup() 
{
  
  Serial.begin(9600);
  
  lcd.init();
  lcd.backlight();
  
  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year
  //myRTC.setDS1302Time(55, 37, 9, 5, 10, 12, 2021); //Here you write your actual time/date as shown above
  //but remember to "comment/remove" this function once you're done
  //The setup is done only one time and the module will continue counting it automatically
}
void loop() 
{
  
  myRTC.updateTime();
  
  if(myRTC.seconds != prevSeconds)  // Only update display if seconds have changed
  {
    prevSeconds = myRTC.seconds;
    
    // Start printing elements as individuals
    lcd.setCursor(6,0);
    lcd.print("");
    lcd.setCursor(4,1);
    
    if(myRTC.minutes %2)
    {
      sprintf(timeChar, "%02d:%02d:%02d",myRTC.hours, myRTC.minutes, myRTC.seconds);
      if(myRTC.seconds == 59)
      {
        // timeChar[0] = 'Z';
        // timeChar[4] = 'X';
        timeChar[0] = random(32, 128);
        timeChar[4] = random(32, 128);
      }
    }
    else
    {
      sprintf(timeChar, "D0:0M:%02d", myRTC.seconds);
    }
    
    lcd.print(timeChar);
  }
}
1 Like