While working on my LED clock I hit a problem I wasn't able yet to solve I'm running Arduino Mega 2560 and RTC module (battery powered).I have two buttons connected (Set and Increment) with pulldown resistor.It kind of works except when I press Increment button Time stops. Seconds are not updating until I stop pressing Set button. I'm not sure if it's an issue with Time.h library or my debounce time is too long (But shorter debounce times give me erratic button behavior).So for example if I'm incrementing hours, seconds stop at the last value and in the result my clock are behind when setting is over. This adds up when I add Date setting, Alarm, etc. Every press of Inc button stops time for the duration.I removed all extra code, and this is just the basic. It will print time to Serial when Set button is pressed.

void setup() { Serial.begin(9600); setSyncProvider(RTC.get); // the function to get the time from the RTC if(timeStatus()!= timeSet) Serial.println("Unable to sync with the RTC"); else Serial.println("RTC has set the system time");

Because that's the way you've written the code. You really want to detect when the Set switch TRANSITIONS TO pressed, not when the switch IS pressed. To do that, you need to keep track of the previous state. Do whatever needs to be done when the switch transitions to pressed (is at some state now, and was not at that state before, and if the current state is pressed). It's best to use two if statements, not one compound statement.

Because that's the way you've written the code. You really want to detect when the Set switch TRANSITIONS TO pressed, not when the switch IS pressed. To do that, you need to keep track of the previous state. Do whatever needs to be done when the switch transitions to pressed (is at some state now, and was not at that state before, and if the current state is pressed). It's best to use two if statements, not one compound statement.

Thank you Paul! I rewrote code as per your suggestion. Issue remains As long as I keep pressing Increment button faster than each 1 second, time is stopped.I think problem is setTime function. If I don't call it, time is not stopping, but as soon as it's called faster than each second it's not updating

void setup() { Serial.begin(9600); setSyncProvider(RTC.get); // the function to get the time from the RTC if(timeStatus()!= timeSet) Serial.println("Unable to sync with the RTC"); else Serial.println("RTC has set the system time"); Serial.print (month());Serial.print(", "); Serial.println (day()); Serial.print (hour());Serial.print (":");Serial.print (minute());Serial.print (":");Serial.println (second());}

Anyway I figured it out! Thank you PaulS for your input, good info! I now learned how to control Press/Release states of the button. Unfortunate it has nothing to do the problems I had. Issue was basically that I'm getting current second and storing it in variable, then changing time and writing this variable back to RTC chip. If whole process happens faster than second, seconds value never changes!So I created a way to keep track of seconds with millis() function. Now if it's been more than 1 second, I "artificially" increment "seconds" var It's still not 100% precise, as I will increment seconds if I wait some time between presses of the "Set" and "Incr" buttons, but it should not be more than 5 seconds (because of timeout) in most cases. I can live with that!

#include <Wire.h>#include <Time.h> #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t#define BOUNCE_TIME_BUTTON 500 // bounce time in ms for the menu button;boolean isSettingTime = false;boolean isSettingHours = false;boolean isSettingMinutes = false;byte SET_BUTTON_PIN=A3;// "set time" button byte INC_BUTTON_PIN=A2;// "inc time" button byte hours;byte minutes;byte seconds;int pSeconds;volatile unsigned long lastButtonTime = 0;// last time a button was pushed; used for debouncing;volatile unsigned long last_ms = 0;// last time a button was pushed; used for debouncing;int cStateSet = LOW; // Current state of Set Buttonint pStateSet = LOW; // Previous state of Set Buttonint cStateInc = LOW; // Current state of inc Buttonint pStateInc = LOW; // Previous state of inc Button

void setup() { Serial.begin(9600); setSyncProvider(RTC.get); // the function to get the time from the RTC if(timeStatus()!= timeSet) Serial.println("Unable to sync with the RTC"); else Serial.println("RTC has set the system time"); Serial.print (month());Serial.print(", "); Serial.println (day()); Serial.print (hour());Serial.print (":");Serial.print (minute());Serial.print (":");Serial.println (second());}