This is what i have got this far
void relayON() {
if (flag1 == 0) {
flag1 = 1;
digitalWrite(TestLED2, LOW);
delay(2000);
digitalWrite(TestLED2, HIGH);
}
}
void relayOFF() {
if (flag2 == 0) {
flag2 = 1;
digitalWrite(TestLED3, LOW);
delay(2000);
digitalWrite(TestLED3, HIGH);
}
}
And this is bit of the code of the scheduler that i have modified calling the function.
BLYNK_WRITE(V4)//Monday-Friday
{
if (mondayfriday==1) {
sprintf(Date, "%02d/%02d/%04d", day(), month(), year());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
TimeInputParam t(param);
terminal.print("M-F Checked schedule at: ");
terminal.println(Time);
terminal.flush();
int dayadjustment = -1;
if(weekday() == 1){
dayadjustment = 6; // needed for Sunday, Time library is day 1 and Blynk is day 7
}
if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday
terminal.println("Monday-Friday ACTIVE today");
terminal.flush();
if (t.hasStartTime()) // Process start time
{
terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
terminal.flush();
}
if (t.hasStopTime()) // Process stop time
{
terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
terminal.flush();
}
// Display timezone details, for information purposes only
terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time
// terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
terminal.flush();
for (int i = 1; i <= 7; i++) { // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
if (t.isWeekdaySelected(i)) {
terminal.println(String("Day ") + i + " is selected");
terminal.flush();
}
}
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
//Serial.println(startsecondswd); // used for debugging
if(nowseconds >= startsecondswd){
terminal.print("Monday-Friday STARTED at");
terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
terminal.flush();
if(nowseconds <= startsecondswd + 90){ // 90s on 60s timer ensures 1 trigger command is sent
digitalWrite(TestLED1, HIGH); // set LED ON
Blynk.virtualWrite(V2, 1);
relayON();
// code here to switch the relay ON
}
}
else{
terminal.println("Monday-Friday Device NOT STARTED today");
terminal.flush();
}
stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
//Serial.println(stopsecondswd); // used for debugging
if(nowseconds >= stopsecondswd){
digitalWrite(TestLED1, LOW); // set LED OFF
Blynk.virtualWrite(V2, 0);
terminal.print("Monday-Friday STOPPED at");
terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
terminal.flush();
if(nowseconds <= stopsecondswd + 90){ // 90s on 60s timer ensures 1 trigger command is sent
digitalWrite(TestLED1, LOW); // set LED OFF
Blynk.virtualWrite(V2, 0);
relayOFF();
// code here to switch the relay OFF
}
}
else{
if(nowseconds >= startsecondswd){
digitalWrite(TestLED1, HIGH); // set LED ON test
Blynk.virtualWrite(V2, 1);
terminal.println("Monday-Friday is ON");
terminal.flush();
flag2 = 0;
flag1 = 0;
}
}
}
else{
terminal.println("Monday-Friday INACTIVE today");
terminal.flush();
// nothing to do today, check again in 30 SECONDS time
}
terminal.println();
}
}
i have called for the function
when i need the relay to be triggered. And again set the flag to 0 for future trigger. This idea is working fine for the OFF relay not for the ON relay. Because the code i have mention above loops every 10 sec when running and loops every 30 sec when idle, then the relays will keep clicking, which i dont want.