diff --git a/simpleclock.ino b/simpleclock.ino index f4a7276..eb1ecbe 100644 --- a/simpleclock.ino +++ b/simpleclock.ino @@ -41,14 +41,24 @@ DS1307 clock; SevSeg Display; +/* push button stuff */ +const long initial_delay = 600; // wait a bit before spin numbers +const long adjust_interval = 200; // delay between consecutive number increnents +const long bounce_delay = 20; // contact bounce compensation +const int hrs_btn = A0; // put hour adjusting button on A0 lane +const int min_btn = A1; // put minute adjusting button on A1 lane +bool btn_pushed = false; // is button pushed? +unsigned long currentPushTimer; +unsigned long previousPushTimer = 0; +uint8_t minutes; +uint8_t hours; + +/* dot blinking stuff */ unsigned long currentMillis; unsigned long previousMillis = 0; const long interval = 500; -const int hrs_btn = A0; -const int min_btn = A1; bool dot = true; -uint8_t minutes; -uint8_t hours; + /* time inaccuracy compensation flag */ uint8_t compflag = 0; @@ -67,8 +77,8 @@ void setup() bool leadingZeros = false; Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, - resistorsOnSegments, updateWithDelays, leadingZeros); - Display.setBrightness(90); + resistorsOnSegments, updateWithDelays, leadingZeros); + Display.setBrightness(100); // setup clock clock.begin(); @@ -90,18 +100,35 @@ void setup() void loop() { - unsigned long currentMillis = millis(); + currentMillis = millis(); + currentPushTimer = currentMillis; char str[4]; float d; - if (currentMillis - previousMillis >= interval) { - previousMillis = currentMillis; + clock.getTime(); - if (dot == false) { - dot = true; + // push button magic + if ((!btn_pushed) and + ((digitalRead(hrs_btn) == LOW) or (digitalRead(min_btn) == LOW))) { + if (currentPushTimer - previousPushTimer >= initial_delay) { + previousPushTimer = currentPushTimer; + btn_pushed = true; + } + } + + if (btn_pushed and + (digitalRead(hrs_btn) != LOW) and (digitalRead(min_btn) != LOW)) { + // compensate possible contact bounce with delay + if (currentPushTimer - previousPushTimer >= bounce_delay) { + btn_pushed = false; + } + } + + if (btn_pushed) { + if (currentPushTimer - previousPushTimer >= adjust_interval) { + previousPushTimer = currentPushTimer; if (digitalRead(hrs_btn) == LOW) { - clock.getTime(); hours = clock.hour + 1; (hours > 23) ? hours = 0 : hours; clock.fillByHMS(hours, clock.minute, 0); @@ -109,18 +136,23 @@ void loop() } if (digitalRead(min_btn) == LOW) { - clock.getTime(); minutes = clock.minute + 1; (minutes > 59) ? minutes = 0 : minutes; clock.fillByHMS(clock.hour, minutes, 0); clock.setTime(); } - } else { - dot = false; } } - clock.getTime(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + + if (dot) + dot = false; + else + dot = true; + } + /* * Let's implement time compenstion for individual rtc module time * charachteristics. In in my case quartz resonator as inaccurate as @@ -129,20 +161,20 @@ void loop() */ if ((clock.hour == 21) and (clock.minute == 21) and - (clock.second == 21) and (compflag == 0)) { + (clock.second == 21) and (compflag == 0)) { clock.fillByHMS(clock.hour, clock.minute, 14); clock.setTime(); compflag = 1; } if ((clock.hour == 21) and (clock.minute == 21) and - (clock.second == 23) and (compflag == 1)) { + (clock.second == 23) and (compflag == 1)) { compflag = 0; } // make blinking dot if (clock.hour > 0) { - if (dot == true) { + if (dot) { Display.setNumber((clock.hour * 100 + clock.minute), 2); } else { Display.setNumber((clock.hour * 100 + clock.minute)); @@ -150,7 +182,7 @@ void loop() } else { /* 0 o'clock is an exception, we don't want to see "00.00", we want see "0.00" or "0 00", so we have to use this "hack" */ - if (dot == true) { + if (dot) { d = float(clock.minute) / 100; Display.setNumber(d, 2); } else {