so im trying to create an arduino alarm clock that displays current time and when you press one of 4 buttons it displayes 3 variables that will be altered by plus and minus buttons that i will add later. The issue is that no matter if i press the button or not the variables appear on the screen the moment that minute on the clock changes. Note that variables appear and disappear when i press and unpress normally before the minute number on the clock changes.
at the start of the program
after pressing a button
after the minute number changes. doesnt matter if i press the button before it or after, it wont disappear
and heres the code-
#include <LiquidCrystal_I2C.h>
#include <virtuabotixRTC.h>
#include <Arduino.h>
// Creation of the Real Time Clock Object
LiquidCrystal_I2C lcd(0x27, 16, 2);
virtuabotixRTC myRTC(6, 7, 8);
//constant variables
const int BP1=1;
const int BP2=2;
const int BP3=3;
const int BP4=4;
//variables
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int alarmHour=0;
int alarmMinute=0;
int alarmSecond=0;
void setup(){
Serial.begin(9600);
pinMode(BP1, INPUT);
pinMode(BP2, INPUT);
pinMode(BP3, INPUT);
pinMode(BP4, INPUT);
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(40, 59, 15, 1, 24, 6, 2024);
}
void loop() {
// This allows for the update of variables for time or accessing the individual elements.
myRTC.updateTime();
// Start printing elements as individuals
Serial.print("Current Date / Time: ");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
// buttons
buttonState1 = digitalRead(BP1);
buttonState2 = digitalRead(BP2);
buttonState3 = digitalRead(BP3);
buttonState4 = digitalRead(BP4);
//lcd screen - alarm menu
if(buttonState1 == HIGH){
lcd.setCursor(5,1);
lcd.print(alarmHour);
lcd.print(":");
lcd.print(alarmMinute);
lcd.print(":");
lcd.print(alarmSecond);
}
//lcd screen - clock
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(myRTC.hours);
lcd.print(":");
lcd.print(myRTC.minutes);
lcd.print(":");
lcd.print(myRTC.seconds);
}