Time adjust algo changed
+ Added 600ms pause before spin numbers if button pressed * Changed increment delay 500ms -> 200ms * Button bounce compensation code
This commit is contained in:
@@ -41,14 +41,24 @@
|
|||||||
DS1307 clock;
|
DS1307 clock;
|
||||||
SevSeg Display;
|
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 currentMillis;
|
||||||
unsigned long previousMillis = 0;
|
unsigned long previousMillis = 0;
|
||||||
const long interval = 500;
|
const long interval = 500;
|
||||||
const int hrs_btn = A0;
|
|
||||||
const int min_btn = A1;
|
|
||||||
bool dot = true;
|
bool dot = true;
|
||||||
uint8_t minutes;
|
|
||||||
uint8_t hours;
|
|
||||||
/* time inaccuracy compensation flag */
|
/* time inaccuracy compensation flag */
|
||||||
uint8_t compflag = 0;
|
uint8_t compflag = 0;
|
||||||
|
|
||||||
@@ -67,8 +77,8 @@ void setup()
|
|||||||
bool leadingZeros = false;
|
bool leadingZeros = false;
|
||||||
|
|
||||||
Display.begin(hardwareConfig, numDigits, digitPins, segmentPins,
|
Display.begin(hardwareConfig, numDigits, digitPins, segmentPins,
|
||||||
resistorsOnSegments, updateWithDelays, leadingZeros);
|
resistorsOnSegments, updateWithDelays, leadingZeros);
|
||||||
Display.setBrightness(90);
|
Display.setBrightness(100);
|
||||||
|
|
||||||
// setup clock
|
// setup clock
|
||||||
clock.begin();
|
clock.begin();
|
||||||
@@ -90,18 +100,35 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
unsigned long currentMillis = millis();
|
currentMillis = millis();
|
||||||
|
currentPushTimer = currentMillis;
|
||||||
char str[4];
|
char str[4];
|
||||||
float d;
|
float d;
|
||||||
|
|
||||||
if (currentMillis - previousMillis >= interval) {
|
clock.getTime();
|
||||||
previousMillis = currentMillis;
|
|
||||||
|
|
||||||
if (dot == false) {
|
// push button magic
|
||||||
dot = true;
|
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) {
|
if (digitalRead(hrs_btn) == LOW) {
|
||||||
clock.getTime();
|
|
||||||
hours = clock.hour + 1;
|
hours = clock.hour + 1;
|
||||||
(hours > 23) ? hours = 0 : hours;
|
(hours > 23) ? hours = 0 : hours;
|
||||||
clock.fillByHMS(hours, clock.minute, 0);
|
clock.fillByHMS(hours, clock.minute, 0);
|
||||||
@@ -109,18 +136,23 @@ void loop()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (digitalRead(min_btn) == LOW) {
|
if (digitalRead(min_btn) == LOW) {
|
||||||
clock.getTime();
|
|
||||||
minutes = clock.minute + 1;
|
minutes = clock.minute + 1;
|
||||||
(minutes > 59) ? minutes = 0 : minutes;
|
(minutes > 59) ? minutes = 0 : minutes;
|
||||||
clock.fillByHMS(clock.hour, minutes, 0);
|
clock.fillByHMS(clock.hour, minutes, 0);
|
||||||
clock.setTime();
|
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
|
* Let's implement time compenstion for individual rtc module time
|
||||||
* charachteristics. In in my case quartz resonator as inaccurate as
|
* 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
|
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.fillByHMS(clock.hour, clock.minute, 14);
|
||||||
clock.setTime();
|
clock.setTime();
|
||||||
compflag = 1;
|
compflag = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((clock.hour == 21) and (clock.minute == 21) and
|
if ((clock.hour == 21) and (clock.minute == 21) and
|
||||||
(clock.second == 23) and (compflag == 1)) {
|
(clock.second == 23) and (compflag == 1)) {
|
||||||
compflag = 0;
|
compflag = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// make blinking dot
|
// make blinking dot
|
||||||
if (clock.hour > 0) {
|
if (clock.hour > 0) {
|
||||||
if (dot == true) {
|
if (dot) {
|
||||||
Display.setNumber((clock.hour * 100 + clock.minute), 2);
|
Display.setNumber((clock.hour * 100 + clock.minute), 2);
|
||||||
} else {
|
} else {
|
||||||
Display.setNumber((clock.hour * 100 + clock.minute));
|
Display.setNumber((clock.hour * 100 + clock.minute));
|
||||||
@@ -150,7 +182,7 @@ void loop()
|
|||||||
} else {
|
} else {
|
||||||
/* 0 o'clock is an exception, we don't want to see "00.00", we
|
/* 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" */
|
want see "0.00" or "0 00", so we have to use this "hack" */
|
||||||
if (dot == true) {
|
if (dot) {
|
||||||
d = float(clock.minute) / 100;
|
d = float(clock.minute) / 100;
|
||||||
Display.setNumber(d, 2);
|
Display.setNumber(d, 2);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user