Add threads and season schedules, some other fixes

This commit is contained in:
ivan 2018-03-08 22:24:15 +03:00
parent a7d3a293f8
commit c2d4d99d28
1 changed files with 118 additions and 31 deletions

View File

@ -1,35 +1,46 @@
/* 2 relay-buttons on timer scetch
* [WINTER short time version]
* based on TimeRTC.pde
* Use SetTime scetch (from DS1307RTC library) for time-sync
*/
#include <TimeLib.h>
// 2 relay-buttons on timer
#include <Arduino.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <TimeLib.h>
#include <Thread.h>
#include <DS1307RTC.h> // A basic DS1307 library that returns time as a time_t
int stopButton = 8;
int startButton = 9;
int led = 13;
int indicator = 0;
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
Thread ledThread = Thread(); // Create thread for LED pulse indication
void setup() {
Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug
serStr("starting setup...");
pinMode(stopButton, OUTPUT);
pinMode(startButton, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
while (!Serial); // wait until Arduino Serial Monitor opens
setSyncProvider(RTC.get); // the function to get the time from the RTC
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");
Serial.println("RTC has set the system time");
// LED blinking thread:
ledThread.onRun(ledBlink);
ledThread.setInterval(ledPulseNormal); // Interval for LED blinking
serStr("...setup finished");
}
void loop() {
// Threads init:
if (ledThread.shouldRun())
ledThread.run();
if (timeStatus() == timeSet) {
checkTime();
} else { // fast blinking after boot indicates that time is not set or DS1307RTC is not plugged
} 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);
@ -41,40 +52,116 @@ void loop() {
delay(5000);
}
// Winter timing 6:00-8:01, 12:00-13:01, 17:00-18:01
// 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() {
// 6:00 - 8:01 - working
if ((hour()==6) && (indicator==0)) pressStart();
if ((hour()==7) && (indicator==0)) pressStart();
// 8:01 - stop
if ((hour()==8) && (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();
// 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();
}
}
void pressStart() {
serStr("scheduled start button pressed");
delay(5000);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
indicator = 1;
digitalWrite(startButton, HIGH);
delay(889);
delay(msStart);
digitalWrite(startButton, LOW);
delay(61000); // wait a minute
serStr("scheduled start button delay finished");
}
void pressStop() {
serStr("scheduled stop button pressed");
delay(5000);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
indicator = 0;
digitalWrite(stopButton, HIGH);
delay(887);
delay(msStop);
digitalWrite(stopButton, LOW);
delay(61000); // wait a minute
serStr("scheduled stop button delay finished");
}
// Send string to serial monitor with millis() counter and date/time
void serStr(const char* serString) {
long currentTime = millis();
String space = " ";
String stringToPrint = currentTime + space + serString;
Serial.println(stringToPrint);
// RTC mark
Serial.print("RTC time = ");
Serial.print(hour());
Serial.write(':');
Serial.print(minute());
Serial.write(':');
Serial.print(second());
Serial.print(", date (D/M/Y) = ");
Serial.print(day());
Serial.write('/');
Serial.print(month());
Serial.write('/');
Serial.print(year());
Serial.println();
}