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

Binary file not shown.