diff --git a/twobuttonstimer.fzz b/twobuttonstimer.fzz index d148644..6bf453e 100644 Binary files a/twobuttonstimer.fzz and b/twobuttonstimer.fzz differ diff --git a/twobuttonstimer.ino b/twobuttonstimer.ino index b81b8cd..0f6ad11 100644 --- a/twobuttonstimer.ino +++ b/twobuttonstimer.ino @@ -1,149 +1,137 @@ -// 2 relay-buttons on timer +// 2 relay-buttons on timer with 4-digit display #include #include #include #include -#include // A basic DS1307 library that returns time as a time_t +#include // A basic DS1307 library that returns time as a time_t +#include // Extended TM1637 library https://github.com/bremme/arduino-tm1637 -int stopButton = 8; -int startButton = 9; -int led = 13; -int indicator = 0; +const byte PIN_CLK = 2; // Define CLK pin (for 4-Digit Display) +const byte PIN_DIO = 3; // Define DIO pin (for 4-Digit Display) +const byte redButton = 4; // RedButton: light on, open door +const byte blackButton = 5; // BlackButton: light off, close door +const byte stopButtonRelay = 6; +const byte startButtonRelay = 7; +// const byte led = 13; +const int msStart = 639; // Interval for pressing start button, ms +const int msStop = 637; // Interval for pressing stop button, ms +const int updateTime = 1000; // Interval for digit display update +const int buttonCheck = 200; // Interval for checking button state +const long manualButtonTime = 3600000; // 60 min - actual state of manual pressed button +long buttonMillis = 0; // Previous button press counter +byte indicator = 0; // Indicator of working states: 0 - scheduled off; 1 - scheduled on; + // 2 - manual off, 3 - manual on -int msStart = 639; // Interval for pressing start button, ms -int msStop = 637; // Interval for pressing stop button, ms -long ledPulseNormal = 500; // Interval for normal LED blinking pulse +SevenSegmentExtended display(PIN_CLK, PIN_DIO); -Thread ledThread = Thread(); // Create thread for LED pulse indication +Thread showTimeThread = Thread(); // Create thread for LED pulse indication +Thread pressButtonThread = Thread(); // Create thread for button state checking void setup() { - Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug + Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug serStr("starting setup..."); - pinMode(stopButton, OUTPUT); - pinMode(startButton, OUTPUT); - pinMode(led, OUTPUT); - while (!Serial); // Wait until Arduino Serial Monitor opens - setSyncProvider(RTC.get); // The function to get the time from the RTC - if(timeStatus()!= timeSet) + display.begin(); // Initializes the display + display.setBacklight(100); // Set the brightness to 100 % + display.print("INIT"); // Display INIT on the display + pinMode(redButton, INPUT); + pinMode(blackButton, INPUT); + pinMode(stopButtonRelay, OUTPUT); + pinMode(startButtonRelay, OUTPUT); +// pinMode(led, OUTPUT); + while (!Serial); // Wait until Arduino Serial Monitor opens + 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"); - // LED blinking thread: - ledThread.onRun(ledBlink); - ledThread.setInterval(ledPulseNormal); // Interval for LED blinking + // showTime thread: + showTimeThread.onRun(showTime); + showTimeThread.setInterval(updateTime); // Interval for update time + // pressButtonThread thread: + pressButtonThread.onRun(pressButton); + pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing + delay(1000); + display.off(); + delay(500); + display.on(); serStr("...setup finished"); } void loop() { // Threads init: - if (ledThread.shouldRun()) - ledThread.run(); + if (showTimeThread.shouldRun()) + showTimeThread.run(); + if (pressButtonThread.shouldRun()) + pressButtonThread.run(); if (timeStatus() == timeSet) { checkTime(); - } else { // Fast blinking after boot indicates that time is not set or DS1307RTC is not plugged - int i; - for (i = 1; i <= 100; i++) { - digitalWrite(led, LOW); - delay(50); - digitalWrite(led, HIGH); - delay(50); - } + } else { + display.on(); + display.print("SET TIME"); + delay(5000); } - delay(5000); } -// LED pulse blinking thread -void ledBlink() { - static bool ledStatus = false; // LED status bool - ledStatus = !ledStatus; // Invert LED state - if (indicator==1) { // Fast blinking after long light (planned work indicator) - delay(200); - digitalWrite(led, LOW); - delay(200); - digitalWrite(led, HIGH); - delay(100); - digitalWrite(led, LOW); - delay(100); - digitalWrite(led, HIGH); - delay(200); - digitalWrite(led, LOW); - delay(200); - digitalWrite(led, HIGH); - } - digitalWrite(led, ledStatus); // Activate LED state -} - -// Winter timing 6:00-7:31, 12:00-13:01, 17:00-18:01 -void checkTime() { - // Winter - if (month()==12 || month()==1 || month()==2 ) { - // 6:00 - 7:01 - working - if ((hour()==6) && (indicator==0)) pressStart(); - // 7:01 - stop - if ((hour()==7) && (minute()==1)) pressStop(); - // 12:00 - 13:01 - working - if ((hour()==12) && (indicator==0)) pressStart(); - // 13:01 - stop - if ((hour()==13) && (minute()==1)) pressStop(); - // 17:00 - 18:01 - working - if ((hour()==17) && (indicator==0)) pressStart(); - // 18:01 - stop - if ((hour()==18) && (minute()==1)) pressStop(); - } - // Spring & Autumn - if (month()==3 || month()==4 || month()==5 || month()==9 || month()==10 || month()==11) { - // 6:00 - 7:41 - working - if ((hour()==6) && (indicator==0)) pressStart(); - // 7:41 - stop - if ((hour()==7) && (minute()==41)) pressStop(); - // 12:00 - 13:01 - working - if ((hour()==12) && (indicator==0)) pressStart(); - // 13:01 - stop - if ((hour()==13) && (minute()==1)) pressStop(); - // 17:00 - 18:21 - working - if ((hour()==17) && (indicator==0)) pressStart(); - // 18:21 - stop - if ((hour()==18) && (minute()==21)) pressStop(); - } - // Summer - if (month()==6 || month()==7 || month()==8 ) { - // 6:00 - 8:01 - working - if ((hour()==6 || hour()==7) && (indicator==0)) pressStart(); - // 8:01 - stop - if ((hour()==8) && (minute()==1)) pressStop(); - // 12:00 - 14:01 - working - if ((hour()==12 || hour()==13) && (indicator==0)) pressStart(); - // 14:01 - stop - if ((hour()==14) && (minute()==1)) pressStop(); - // 17:00 - 18:25 - working - if ((hour()==17) && (indicator==0)) pressStart(); - // 18:25 - stop - if ((hour()==18) && (minute()==25)) pressStop(); +// showTime thread +void showTime() { + display.printTime(hour(), minute(), true); + static bool showTimeStatus = false; // showTime status bool + showTimeStatus = !showTimeStatus; // Invert showTime state + if ((indicator==1)||(indicator==3) && showTimeStatus==true) { + display.print("BODA"); } } void pressStart() { serStr("scheduled start button pressed"); + display.print("on__"); delay(5000); indicator = 1; - digitalWrite(startButton, HIGH); + digitalWrite(startButtonRelay, HIGH); delay(msStart); - digitalWrite(startButton, LOW); + digitalWrite(startButtonRelay, LOW); delay(61000); // wait a minute serStr("scheduled start button delay finished"); } void pressStop() { serStr("scheduled stop button pressed"); + display.print("off_"); delay(5000); indicator = 0; - digitalWrite(stopButton, HIGH); + digitalWrite(stopButtonRelay, HIGH); delay(msStop); - digitalWrite(stopButton, LOW); + digitalWrite(stopButtonRelay, LOW); delay(61000); // wait a minute serStr("scheduled stop button delay finished"); } +void pressStartManual() { + serStr("manual start button pressed"); + display.print("on__"); + buttonMillis = millis(); + delay(500); + indicator = 3; + digitalWrite(startButtonRelay, HIGH); + delay(msStart); + digitalWrite(startButtonRelay, LOW); + delay(1500); + serStr("manual start button delay finished"); +} + +void pressStopManual() { + serStr("manual stop button pressed"); + display.print("off_"); + buttonMillis = millis(); + delay(500); + indicator = 2; + digitalWrite(stopButtonRelay, HIGH); + delay(msStop); + digitalWrite(stopButtonRelay, LOW); + delay(1500); + serStr("manual stop button delay finished"); +} + // Send string to serial monitor with millis() counter and date/time void serStr(const char* serString) { long currentTime = millis(); @@ -165,3 +153,101 @@ void serStr(const char* serString) { Serial.print(year()); Serial.println(); } + +// Check button pressing thread +void pressButton() { + unsigned long currentMillis = millis(); + if (buttonMillis>0) { + if (indicator==3) { + if ((currentMillis-buttonMillis) > manualButtonTime) { + serStr("manualButtonTime expired...press stop"); + buttonMillis = 0; + pressStop(); + } + } + if (indicator==2) { + if ((currentMillis-buttonMillis) > manualButtonTime) { + serStr("manualButtonTime expired...indicator state reset"); + buttonMillis = 0; + indicator = 0; + } + } + } + if (digitalRead(redButton) == HIGH || digitalRead(blackButton) == HIGH) { + if (digitalRead(redButton) == HIGH) { + serStr("Manual red button short press"); + pressStartManual(); + } + if (digitalRead(blackButton) == HIGH) { + serStr("Manual black button short press"); + pressStopManual(); + } + } +} + +// Winter timing 6:00-7:31, 12:00-13:01, 17:00-18:01 +void checkTime() { + // Winter + if (month()==12 || month()==1 || month()==2 ) { + // 6:00 - 7:01 - working + if ((hour()==6) && (indicator==0)) pressStart(); + // 7:01 - stop + if ((hour()==7) && (minute()==1)) pressStop(); + // 12:00 - 13:01 - working + if ((hour()==12) && (indicator==0)) pressStart(); + // 13:01 - stop + if ((hour()==13) && (minute()==1)) pressStop(); + // 17:00 - 18:01 - working + if ((hour()==17) && (indicator==0)) pressStart(); + // 18:01 - stop + if ((hour()==18) && (minute()==1)) pressStop(); + } + // Spring & Autumn +/* +// ! TEMPORARY CHANGES ! + if (month()==3 || month()==4 || month()==5 || month()==9 || month()==10 || month()==11) { + // 6:00 - 7:41 - working + if ((hour()==6) && (indicator==0)) pressStart(); + // 7:41 - stop + if ((hour()==7) && (minute()==41)) pressStop(); + // 12:00 - 13:01 - working + if ((hour()==12) && (indicator==0)) pressStart(); + // 13:01 - stop + if ((hour()==13) && (minute()==1)) pressStop(); + // 17:00 - 18:21 - working + if ((hour()==17) && (indicator==0)) pressStart(); + // 18:21 - stop + if ((hour()==18) && (minute()==21)) pressStop(); + } +*/ +// ! TEMPORARY CHANGES ! + if (month()==3 || month()==4 || month()==5 || month()==9 || month()==10 || month()==11) { + // 6:00 - 8:01 - working + if ((hour()==6 || hour()==7) && (indicator==0)) pressStart(); + // 8:01 - stop + if ((hour()==8) && (minute()==1)) pressStop(); + // 12:00 - 13:31 - working + if ((hour()==12) && (indicator==0)) pressStart(); + // 13:31 - stop + if ((hour()==13) && (minute()==31)) pressStop(); + // 17:00 - 18:41 - working + if ((hour()==17) && (indicator==0)) pressStart(); + // 18:41 - stop + if ((hour()==18) && (minute()==41)) pressStop(); + } + // Summer + if (month()==6 || month()==7 || month()==8 ) { + // 6:00 - 8:01 - working + if ((hour()==6 || hour()==7) && (indicator==0)) pressStart(); + // 8:01 - stop + if ((hour()==8) && (minute()==1)) pressStop(); + // 12:00 - 14:01 - working + if ((hour()==12 || hour()==13) && (indicator==0)) pressStart(); + // 14:01 - stop + if ((hour()==14) && (minute()==1)) pressStop(); + // 17:00 - 18:25 - working + if ((hour()==17) && (indicator==0)) pressStart(); + // 18:25 - stop + if ((hour()==18) && (minute()==25)) pressStop(); + } +} \ No newline at end of file diff --git a/twobuttonstimer_bb.png b/twobuttonstimer_bb.png index 39128fd..89890a7 100644 Binary files a/twobuttonstimer_bb.png and b/twobuttonstimer_bb.png differ