My lcd keeps repeatedly printing stuff

i have 16x2 lcd and i have DS3231 rtc I'm using to make a clock and whenever i write lcd.print(rtc.hour()); it keeps repeatedly printing it like:17 18 19
the code is not finished yet though
and if i try it with a sketch that initialises the lcd and just prints hello world in void loop it works

#include "Arduino.h"
#include "uRTCLib.h"
#include <LiquidCrystal.h>

// uRTCLib rtc;
uRTCLib rtc(0x68);
int hour;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
bool night = false;
const int buttonPin = 7;
int buttonState = 0;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  delay(3000);// wait for console opening
  URTCLIB_WIRE.begin();
  pinMode(buttonPin, INPUT);


}

void loop() {
  rtc.refresh();
  lcd.setCursor(0,0);
  lcd.print(rtc.second());
  delay(1000);
 
}




Your sketch is printing seconds, not hours

  lcd.print(rtc.second());

i wrote some new code to display the time and the same thing keeps happening

#include "Arduino.h"
#include "uRTCLib.h"
#include <LiquidCrystal.h>

// uRTCLib rtc;
uRTCLib rtc(0x68);
int hour;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
bool night = false;
const int buttonPin = 7;
int buttonState = 0;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  delay(3000);// wait for console opening
  URTCLIB_WIRE.begin();
  pinMode(buttonPin, INPUT);

  // Comment out below line once you set the date & time.
  // Following line sets the RTC with an explicit date & time
  // for example to set January 13 2022 at 12:56 you would call:
   
  // rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
  // set day of week (1=Sunday, 7=Saturday)
}

void loop() {
  rtc.refresh();
  lcd.setCursor(0,0);
  lcd.print(rtc.hour());
  lcd.print(":");
  lcd.print(rtc.minute());
  lcd.print(":");
  lcd.print(rtc.second());
  delay(1000);
 
}




As the name suggests, the loop() function is called repeatedly - in a loop.

The delay at the end of your code means it gets repeated every second.

It looks like the output from previous iterations is remaining on the screen - have you tried clearing the screen at the start of each loop?

Welcome

You use the same pin for the button and for the LCD Register Select (RS) pin. This will cause problems.

Try this code.
I changed the buttonPin pin to 8 and added a line to clear the LCD at the end after the 1 sec delay.

#include "Arduino.h"
#include "uRTCLib.h"
#include <LiquidCrystal.h>

// uRTCLib rtc;
uRTCLib rtc(0x68);
int hour;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
bool night = false;
const int buttonPin = 8;
int buttonState = 0;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  delay(3000);// wait for console opening
  URTCLIB_WIRE.begin();
  pinMode(buttonPin, INPUT);

  // Comment out below line once you set the date & time.
  // Following line sets the RTC with an explicit date & time
  // for example to set January 13 2022 at 12:56 you would call:
   
  // rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
  // set day of week (1=Sunday, 7=Saturday)
}

void loop() {
  rtc.refresh();
  lcd.setCursor(0,0);
  lcd.print(rtc.hour());
  lcd.print(":");
  lcd.print(rtc.minute());
  lcd.print(":");
  lcd.print(rtc.second());
  delay(1000);
  lcd.clear();
}

For a fuller presentation, "zero pad" your single digits

  if lcd.print(rtc_hour()) < 10
    lcd.print(0);
  lcd.print(rtc.hour());
  lcd.print(":");
  if lcd.print(rtc_minute()) < 10
    lcd.print(0);
  lcd.print(rtc.minute());
  lcd.print(":");
  if lcd.print(rtc_second()) < 10
    lcd.print(0);
  lcd.print(rtc.second());

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