My tact switch time set code, which includes debounce (so that erroneous 'bounce' clicks are not detected):
/**********Time Set Routine**********************/
boolean switchread = 1;
switchread = digitalRead(TIME_SET);
if (switchread == LOW) {
while (switchread == LOW) {
switchread = digitalRead(TIME_SET);
}//do nothing while the switch is low
delay(10);
timeset = ((timeset + 1) % 4);
}
//Set hours
if (timeset == 1) {
//mask off everything but the hours
NumberArray[2] = 999;
NumberArray[3] = 999;
NumberArray[4] = 999;
NumberArray[5] = 999;
boolean switchread = 1;
switchread = digitalRead(TIME_UP);
if (switchread == LOW) {
while (switchread == LOW) {
switchread = digitalRead(TIME_UP);
}//do nothing while the switch is low
delay(10);
DateTime adjust (now.unixtime() + 3600); //set hours + 1
RTC.adjust(adjust); // set to new time
}
switchread = digitalRead(TIME_DOWN);
if (switchread == LOW) {
while (switchread == LOW) {
switchread = digitalRead(TIME_DOWN);
}//do nothing while the switch is low
delay(10);
DateTime adjust (now.unixtime() - 3600); //set hours + 1
RTC.adjust(adjust); // set to new time
}
}//end set hours
//Set minutes
if (timeset == 2) {
//mask off everything but the minutes
NumberArray[0] = 999;
NumberArray[1] = 999;
NumberArray[4] = 999;
NumberArray[5] = 999;
boolean switchread = 1;
switchread = digitalRead(TIME_UP);
if (switchread == LOW) {
while (switchread == LOW) {
switchread = digitalRead(TIME_UP);
}//do nothing while the switch is low
delay(10);
DateTime adjust (now.unixtime() + 60); //set mins + 1
RTC.adjust(adjust); // set to new time
}
switchread = digitalRead(TIME_DOWN);
if (switchread == LOW) {
while (switchread == LOW) {
switchread = digitalRead(TIME_DOWN);
}//do nothing while the switch is low
delay(10);
DateTime adjust (now.unixtime() - 60); //set mins - 1
RTC.adjust(adjust); // set to new time
}
}//end set minutes
//Set seconds
if (timeset == 3) {
//mask off everything but the seconds
NumberArray[0] = 999;
NumberArray[1] = 999;
NumberArray[2] = 999;
NumberArray[3] = 999;
boolean switchread = 1;
switchread = digitalRead(TIME_UP);
if (switchread == LOW) {
while (switchread == LOW) {
switchread = digitalRead(TIME_UP);
}//do nothing while the switch is low
delay(10);
DateTime adjust (now.unixtime() + 1); //set seconds + 1
RTC.adjust(adjust); // set to new time
}
switchread = digitalRead(TIME_DOWN);
if (switchread == LOW) {
while (switchread == LOW) {
switchread = digitalRead(TIME_DOWN);
}//do nothing while the switch is low
delay(10);
DateTime adjust (now.unixtime() - 2); //set seconds - 1
RTC.adjust(adjust); // set to new time
}
}//end set hours
I use three switches. The first enters 'time set' mode, and each time it is pressed it goes to set hours, minutes, and seconds, then exits time set mode. The other two buttons increment and decrement. It just lives in the main loop() statement.