BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to exit scheduler when button pressed?

First of all, the time input scheduler works fine for my case, :sweat_smile:my problem is if I pressed button to stop relay during scheduler start & stop time (let’s say started at 10:00, stop at 10:10), it stopped right away, but after few seconds, relay turned on again because of stop time is not reached yet, I could not figure out how to exit scheduler and not allow relay turn on again, I did search the forum, maybe my key words and English issues, I did not find the similar topic to illustrate that, hope someone kindly give me a hint, thank you for reading my request. :grinning:

Best regards

BLYNK_WRITE(V1) {
  int Btn = param.asInt();
  if (Btn == 1 && flag == 0) {
    flag = 1;
    relayON();
  }
  if (Btn == 0 && flag == 1) {
    flag = 0;
    relayOFF();
  }
}

void activetoday() {
  if (year() != 1970) {
    Blynk.syncVirtual(V2);
  }
}

BLYNK_WRITE(V2) {
  sprintf(Date, " % 02d / % 02d / % 04d",  day(), month(), year());
  sprintf(Time, " % 02d: % 02d: % 02d", hour(), minute(), second());

  TimeInputParam t(param);
  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))) {
    if (t.hasStartTime()) {
    }
    if (t.hasStopTime()) {
    }
    for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
      if (t.isWeekdaySelected(i)) {
      }
    }
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    if (nowseconds >= startsecondswd) {
      if (nowseconds <= startsecondswd + 90) {
        ontime = 1;
        if (ontime == 1 && flag2 == 0) {
          flag2 = 1;
          relayON();
        }
      }
    }
    else {
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    if (nowseconds >= stopsecondswd) {
      if (nowseconds <= stopsecondswd + 90) { // 90s on 60s timer ensures 1 trigger command is sent
        ontime = 0;
        if (ontime == 0 && flag2 == 1) {
          flag2 = 0;
          relayOFF();
        }
      }
    }
    else {
      if (nowseconds >= startsecondswd) {
      }
    }
  }
  else {
  }
}

Hello~
Anyone can provide hint? Any response will be highly appreciated. ^.^

In your “if” check you could also put a flag have that flag set false/true by your virtual pin. If time is right && sprinklerFlag == false…

1 Like

Thank you so much, I try understanding your hint, allow me to think about it and then do some experiment.

Zephyr

Try a virtual button that changes this flag2 to 0.

BLYNK_WRITE(V1)
{
 
  if  (param.asInt())      //this means if the app is 1 or (default on) it will trigger.
  {
      flag2 = 0;
  }

}

You might also want to add a

Blynk.syncVirtual(V1);

in your Blynk_Connected function. I didn’t understand this one for a long time… If you have a Blynk_Connected function, it calls anything inside it once as soon as it connects. If you have a virtual sync it will take the app state and use that. In case the device has been restarted or something.

2 Likes

Thanks a lot, I will try flag2 = 0; ^.^

Have a nice day ^.^

@daveblynk,

It works, big thanks, I’d really appreciate it. :grinning:

Now I am working on how to get rid of blynk notify when pressing button to stop scheduler. I put Blynk notify inside ontime = 0, no matter stop scheduler or not, Blynk.notify still send a notification to me.

Have a nice day

Use another flag and if check. I usually set a timer that turns it back true after 1hrs so if the problem still exists in an hour it will notify me again.

1 Like

Thanks, I will use another flag and if check tomorrow, I will get this back to you soon. ^.^

Zephyr

Hi @daveblynk,

It works, bravo! Thank you so much. :grinning:

But here is another problem, let’s say scheduler started at 08:00, stopped at 08:01, stopped scheduler by button at 08:00:30, and then setup new scheduler time from 08:01 to 08:02, scheduler won’t be started at 08:01. I am thinking about solution now. :sweat_smile:

:wink: if I understand what you mean… in the time input code put something that resets the flag

1 Like

If put reset flag in the time input, will ontime = 1 and relay on again right after scheduler reach stop time? Sometimes scheduler reaches stop time and relay turn off and then turn on relay again in very short time, seems like 100ms, then turn off finally.

Big thanks to @daveblynk. Since original issue had been resolved, I will keep studying the rest of coding, I will close this case and mark Solved.

Have a nice day ^.^

Zephyr