Hello, I have built a seven segment display clock that have the capability to have its minute and hour number increase via button press. However, I am having an issue where if I let go of the minute/ hour button, the minute/hour number will immediately revert back to their original state. Can someone help me figure out how to permanently adjust the time? Thank you. I can not post the code as a file so I will have the entire thing paste down here.
//this version is working, don't change it. Make a copy instead.
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_GenericDevice.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Adafruit_SPIDevice.h>
//renember to include the entire Adafruit BusIO library, the original <Adafruit_I2CRegister.h> library is missing for some reason
#include <RTClib.h>
RTC_DS3231 rtc;
//set up for the Shift registers and numbers
//check the datasheet for the SN74HC595 to see the individual pins
#define Hourbutton 8//button used to adjust the hour
#define Minutebutton 9//button used to adjust the minutes
#define DS_pin 4//blue
#define STCP_pin 5//yellow
#define SHCP_pin 6//green
//color coded so you don't blows it up
int numbers[10] {126, 48, 109, 121, 51, 91, 95, 112, 127, 123}; //use [int digits [10] { 1, 79, 18, 12, 76, 36, 32, 15, 0, 4};] instead if
//your display is the common Annode type.
int digits[4]; //int d1; int d2; int d3; int d4;// has to define all of these, they represents the individual digits and the time number
int hbstate;
int mbstate;
int minute;
int hour;
void setup() {
pinMode(Hourbutton, INPUT);
pinMode(Minutebutton, INPUT);
pinMode(DS_pin, OUTPUT);
pinMode(STCP_pin, OUTPUT);
pinMode(SHCP_pin, OUTPUT);
hbstate = 0;
mbstate = 0;
// initializing the rtc
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
// When time needs to be set on a new device, or after a power loss,
//the following line sets the RTC to the date & time this sketch was compiled
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));// This line sets the RTC with the time from the pc when compiled
//rtc.adjust(DateTime(2026, 4, 21, 11, 55, 30));//uncomment this line once to manually set the time
}
//The entire if section above enable the RTC to read correct time, do not delete. thanks Nuetral, I owe you one.
rtc.disable32K();
}
void loop() {
//minute = now.minute();
//hour = now.hour();
hbstate = digitalRead(Hourbutton);
mbstate = digitalRead(Minutebutton);
DateTime now = rtc.now();
//Formula to display Hours
digits[1]= ((hour/10)%10);//formula for the first digit
digits[2]= (hour%10);//formula for the second digit
//Formua to display Minutes
digits[3]= ((minute/10)%10);//formula for the third digit
digits[4]= (minute%10);//formula for the fourth digit
if (hbstate == HIGH){
if (hour == 25){
hour = 1;
}
else{
hour = now.hour() + 1;
}
}
else{
hour = now.hour();
}
if (mbstate == HIGH){
if (minute == 61){
minute = 1;
}
else{
minute = now.minute() + 1;
}
}
else{
minute = now.minute();
}
digitalWrite(STCP_pin, LOW);
//The lines of code below is the shiftout command
//use least LSBFIRST instead of MSBFIRST
shiftOut(DS_pin, SHCP_pin, LSBFIRST, numbers[digits[4]]); // shift in the digit, last digit.
shiftOut(DS_pin, SHCP_pin, LSBFIRST, numbers[digits[3]]); // shift in the digit, third digit.
shiftOut(DS_pin, SHCP_pin, LSBFIRST, numbers[digits[2]]); // shift in the digit, second digit.
shiftOut(DS_pin, SHCP_pin, LSBFIRST, numbers[digits[1]]); // shift in the digit, first digit.
//The sequenece is important
digitalWrite(STCP_pin, HIGH); // flash the digit in the correct place
delay (100);
}