actualising code for modern libs

This commit is contained in:
2026-01-17 23:55:04 +03:00
parent 80d8f29c0f
commit 8284916715
2 changed files with 151 additions and 133 deletions

View File

@@ -1,7 +1,7 @@
/* /*
* Hencoop with automatic lamp and door timer, TM1637 display, two buttons and magnetically operated sealed switch * Hencoop with automatic lamp and door timer, TM1637 display, two buttons and magnetically operated sealed switch
*/ */
#include <Wire.h>
#include <Arduino.h> #include <Arduino.h>
#include <TimeLib.h> #include <TimeLib.h>
#include <Thread.h> #include <Thread.h>
@@ -45,11 +45,10 @@ Thread correctionThread = Thread(); // Time bias correction thread
void setup() { 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..."); serStr("starting setup...");
display.begin(); // Initializes the display display.begin(); // Initializes the display
display.setBacklight(100); // Set the brightness to 100 % display.setBacklight(100); // Set the brightness to 100 %
display.print("INIT"); // Display INIT on the display display.print("INIT"); // Display INIT on the display
pinMode(pinRelay3, OUTPUT); pinMode(pinRelay3, OUTPUT);
pinMode(pinRelay4, OUTPUT); pinMode(pinRelay4, OUTPUT);
pinMode(pinDC, OUTPUT); pinMode(pinDC, OUTPUT);
@@ -67,9 +66,13 @@ void setup() {
if(timeStatus()!= timeSet) if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC"); Serial.println("Unable to sync with the RTC");
else else
// Serial.println("RTC has set the system time"); Serial.println("RTC has set the system time");
Serial.println(now()); Serial.println(now());
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC");
// Button state cheking thread: // Button state cheking thread:
pressButtonThread.onRun(pressButton); pressButtonThread.onRun(pressButton);
pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing
@@ -77,9 +80,9 @@ void setup() {
correctionThread.onRun(correctionLoop); correctionThread.onRun(correctionLoop);
correctionThread.setInterval(correctionCheck); correctionThread.setInterval(correctionCheck);
delay (1000); delay (100);
display.off(); display.off();
// serStr("...setup finished"); serStr("...setup finished");
} }
void loop() { void loop() {
@@ -89,7 +92,8 @@ void loop() {
if (correctionThread.shouldRun()) if (correctionThread.shouldRun())
correctionThread.run(); correctionThread.run();
if (timeStatus() == timeSet) { // If RTC works - call the checkTime function tmElements_t tm;
if (RTC.read(tm)) { // If RTC works - call the checkTime function
checkTime(); checkTime();
} else { } else {
display.on(); display.on();
@@ -105,9 +109,11 @@ void pressButton() {
displayWork = 0; displayWork = 0;
} }
if (digitalRead(redButton) == HIGH || digitalRead(blackButton) == HIGH) { if (digitalRead(redButton) == HIGH || digitalRead(blackButton) == HIGH) {
tmElements_t tm;
RTC.read(tm);
display.on(); display.on();
displayWork = 1; displayWork = 1;
display.printTime(hour(), minute(), true); display.printTime(tm.Hour, tm.Minute, true);
buttonPressed = buttonPressed + 200; buttonPressed = buttonPressed + 200;
if (buttonPressed > buttonShortPress) { if (buttonPressed > buttonShortPress) {
@@ -204,211 +210,223 @@ void closeDoor() {
// Send string to serial monitor with millis() counter and date/time // Send string to serial monitor with millis() counter and date/time
void serStr(const char* serString) { void serStr(const char* serString) {
tmElements_t tm;
RTC.read(tm);
long currentTime = millis(); long currentTime = millis();
String space = " "; String space = " ";
String stringToPrint = currentTime + space + serString; String stringToPrint = currentTime + space + serString;
Serial.println(stringToPrint); Serial.println(stringToPrint);
// RTC mark // RTC mark
Serial.print("RTC time = "); Serial.print("RTC time = ");
Serial.print(hour()); Serial.print(tm.Hour);
Serial.write(':'); Serial.write(':');
Serial.print(minute()); Serial.print(tm.Minute);
Serial.write(':'); Serial.write(':');
Serial.print(second()); Serial.print(tm.Second);
Serial.print(", date (D/M/Y) = "); Serial.print(", date (D/M/Y) = ");
Serial.print(day()); Serial.print(tm.Day);
Serial.write('/'); Serial.write('/');
Serial.print(month()); Serial.print(tm.Month);
Serial.write('/'); Serial.write('/');
Serial.print(year()); Serial.print(tmYearToCalendar(tm.Year));
Serial.println(); Serial.println();
} }
void checkTime() { void checkTime() {
// January 08:06 08:50 16:27 17:11 tmElements_t tm;
if (month() == 1) { RTC.read(tm);
if (hour() == 8 && minute() == 15) { // January
if (tm.Month == 1) {
if (tm.Hour == 8 && tm.Minute == 15) {
openDoor(); openDoor();
delay(60000); delay(60000);
} }
if (hour() == 15 && minute() == 55) { if (tm.Hour == 7 && tm.Minute == 55) {
lightOn(); lightOn();
delay(60000); delay(60000);
} }
if (hour() == 18 && minute() == 35) { if (tm.Hour == 17 && tm.Minute == 55) {
lightOff(); lightOff();
delay(60000); delay(60000);
} }
if (hour() == 17 && minute() == 50) { if (tm.Hour == 19 && tm.Minute == 55) {
closeDoor(); closeDoor();
delay(60000); delay(60000);
} }
} }
// February 07:18 07:56 17:31 18:10 // February
if (month() == 2) { if (tm.Month == 2) {
if (hour() == 7 && minute() == 20) { if (tm.Hour == 7 && tm.Minute == 15) {
openDoor(); openDoor();
delay(60000); delay(60000);
} }
if (hour() == 16 && minute() == 55) { if (tm.Hour == 7 && tm.Minute == 30) {
lightOn(); lightOn();
delay(60000); delay(60000);
} }
if (hour() == 19 && minute() == 30) { if (tm.Hour == 19 && tm.Minute == 15) {
lightOff(); lightOff();
delay(60000); delay(60000);
} }
if (hour() == 18 && minute() == 45) { if (tm.Hour == 19 && tm.Minute == 30) {
closeDoor(); closeDoor();
delay(60000); delay(60000);
} }
} }
// March 06:11 06:47 18:30 19:07 // March
if (month() == 3) { if (tm.Month == 3) {
if (hour() == 6 && minute() == 15) { if (tm.Hour == 6 && tm.Minute == 15) {
openDoor(); openDoor();
delay(60000); delay(60000);
} }
if (hour() == 17 && minute() == 55) { if (tm.Hour == 7 && tm.Minute == 30) {
lightOn(); lightOn();
delay(60000); delay(60000);
} }
if (hour() == 20 && minute() == 30) { if (tm.Hour == 19 && tm.Minute == 30) {
lightOff(); lightOff();
delay(60000); delay(60000);
} }
if (hour() == 19 && minute() == 45) { if (tm.Hour == 19 && tm.Minute == 45) {
closeDoor(); closeDoor();
delay(60000); delay(60000);
} }
} }
// April 04:48 05:27 19:33 20:13 // April
if (month() == 4) { if (tm.Month == 4) {
if (hour() == 4 && minute() == 55) { if (tm.Hour == 5 && tm.Minute == 15) {
openDoor(); openDoor();
delay(60000); delay(60000);
} }
if (hour() == 20 && minute() == 50) { if (tm.Hour == 18 && tm.Minute == 55) {
closeDoor();
delay(60000);
}
}
// May 03:31 04:20 20:32 21:21
if (month() == 5) {
if (hour() == 3 && minute() == 50) {
openDoor();
delay(60000);
}
if (hour() == 21 && minute() == 55) {
closeDoor();
delay(60000);
}
}
// June 02:43 03:44 21:15 22:16
if (month() == 6) {
if (hour() == 3 && minute() == 15) {
openDoor();
delay(60000);
}
if (hour() == 23 && minute() == 20) {
closeDoor();
delay(60000);
}
}
// July 03:08 04:04 21:05 22:00
if (month() == 7) {
if (hour() == 3 && minute() == 30) {
openDoor();
delay(60000);
}
if (hour() == 23 && minute() == 15) {
closeDoor();
delay(60000);
}
}
// August 04:15 04:58 20:08 20:51
if (month() == 8) {
if (hour() == 4 && minute() == 15) {
openDoor();
delay(60000);
}
if (hour() == 22 && minute() == 30) {
closeDoor();
delay(60000);
}
}
// September 05:21 05:58 18:50 19:27
if (month() == 9) {
if (hour() == 5 && minute() == 25) {
openDoor();
delay(60000);
}
if (hour() == 18 && minute() == 20) {
lightOn(); lightOn();
delay(60000); delay(60000);
} }
if (hour() == 20 && minute() == 50) { if (tm.Hour == 20 && tm.Minute == 45) {
lightOff(); lightOff();
delay(60000); delay(60000);
} }
if (hour() == 21 && minute() == 35) { if (tm.Hour == 20 && tm.Minute == 50) {
closeDoor(); closeDoor();
delay(60000); delay(60000);
} }
} }
// October 06:20 06:57 17:32 18:09 // May
if (month() == 10) { if (tm.Month == 5) {
if (hour() == 6 && minute() == 25) { if (tm.Hour == 4 && tm.Minute == 30) {
openDoor(); openDoor();
delay(60000); delay(60000);
} }
if (hour() == 16 && minute() == 55) { if (tm.Hour == 21 && tm.Minute == 55) {
lightOn();
delay(60000);
}
if (hour() == 19 && minute() == 30) {
lightOff();
delay(60000);
}
if (hour() == 19 && minute() == 45) {
closeDoor(); closeDoor();
delay(60000); delay(60000);
} }
} }
// November 07:20 08:02 16:24 17:06 // June
if (month() == 11) { if (tm.Month == 6) {
if (hour() == 7 && minute() == 30) { if (tm.Hour == 4 && tm.Minute == 15) {
openDoor(); openDoor();
delay(60000); delay(60000);
} }
if (hour() == 15 && minute() == 55) { if (tm.Hour == 22 && tm.Minute == 30) {
lightOn();
delay(60000);
}
if (hour() == 18 && minute() == 30) {
lightOff();
delay(60000);
}
if (hour() == 17 && minute() == 45) {
closeDoor(); closeDoor();
delay(60000); delay(60000);
} }
} }
// December 08:05 08:51 15:56 16:42 // July
if (month() == 12) { if (tm.Month == 7) {
if (hour() == 8 && minute() == 20) { if (tm.Hour == 4 && tm.Minute == 15) {
openDoor(); openDoor();
delay(60000); delay(60000);
} }
if (hour() == 15 && minute() == 25) { if (tm.Hour == 22 && tm.Minute == 15) {
closeDoor();
delay(60000);
}
}
// August
if (tm.Month == 8) {
if (tm.Hour == 4 && tm.Minute == 45) {
openDoor();
delay(60000);
}
if (tm.Hour == 21 && tm.Minute == 45) {
closeDoor();
delay(60000);
}
}
// September
if (tm.Month == 9) {
if (tm.Hour == 5 && tm.Minute == 55) {
openDoor();
delay(60000);
}
if (tm.Hour == 17 && tm.Minute == 15) {
lightOn(); lightOn();
delay(60000); delay(60000);
} }
if (hour() == 18 && minute() == 5) { if (tm.Hour == 20 && tm.Minute == 15) {
lightOff(); lightOff();
delay(60000); delay(60000);
} }
if (hour() == 17 && minute() == 25) { if (tm.Hour == 20 && tm.Minute == 55) {
closeDoor();
delay(60000);
}
}
// October
if (tm.Month == 10) {
if (tm.Hour == 6 && tm.Minute == 25) {
openDoor();
delay(60000);
}
if (tm.Hour == 7 && tm.Minute == 15) {
lightOn();
delay(60000);
}
if (tm.Hour == 19 && tm.Minute == 15) {
lightOff();
delay(60000);
}
if (tm.Hour == 19 && tm.Minute == 45) {
closeDoor();
delay(60000);
}
}
// November
if (tm.Month == 11) {
if (tm.Hour == 6 && tm.Minute == 45) {
openDoor();
delay(60000);
}
if (tm.Hour == 7 && tm.Minute == 15) {
lightOn();
delay(60000);
}
if (tm.Hour == 19 && tm.Minute == 15) {
lightOff();
delay(60000);
}
if (tm.Hour == 17 && tm.Minute == 15) {
closeDoor();
delay(60000);
}
}
// December
if (tm.Month == 12) {
if (tm.Hour == 8 && tm.Minute == 15) {
openDoor();
delay(60000);
}
if (tm.Hour == 7 && tm.Minute == 45) {
lightOn();
delay(60000);
}
if (tm.Hour == 19 && tm.Minute == 45) {
lightOff();
delay(60000);
}
if (tm.Hour == 16 && tm.Minute == 45) {
closeDoor(); closeDoor();
delay(60000); delay(60000);
} }
@@ -416,20 +434,20 @@ void checkTime() {
} }
void correctionLoop() { void correctionLoop() {
if (hour() == correctionHour) {
if (correctionReady) {
// CORRECTION!
tmElements_t RTCtime; tmElements_t RTCtime;
RTC.read(RTCtime); RTC.read(RTCtime);
if (RTCtime.Hour == correctionHour) {
if (correctionReady) {
// CORRECTION
time_t RTCtimestamp; time_t RTCtimestamp;
RTCtimestamp = makeTime(RTCtime); RTCtimestamp = makeTime(RTCtime);
tmElements_t timeNew; tmElements_t timeNew;
time_t newTimestamp = RTCtimestamp - correctionBias; // -1sec everyday time_t newTimestamp = RTCtimestamp - correctionBias; // -1sec everyday
if ((day() % 5) == 0) newTimestamp = newTimestamp - 2; // -2sec every 5 days (-0.4sec everyday) if ((RTCtime.Day % 5) == 0) newTimestamp = newTimestamp - 2; // -2sec every 5 days (-0.4sec everyday)
breakTime(newTimestamp, timeNew); breakTime(newTimestamp, timeNew);
RTC.write(timeNew); RTC.write(timeNew);
setSyncProvider(RTC.get); setSyncProvider(RTC.get);
// CORRECTION! // CORRECTION
correctionReady = false; correctionReady = false;
} }
} else correctionReady = true; } else correctionReady = true;

Binary file not shown.