Function to run only once

like I said above, change the timer interval to 60 seconds.

this may turn on or off a min later. Because it checks only after the 60sec interval. If the time passes by that time then it has to wait till the next cycle occurs.

If i need to turn on the pump for only one min then this will not be possible. \

Need some other ideaā€¦:face_with_monocle:

then use my flag example above. Donā€™t use delay(); or add code to the void loop()

Be sure to add the realyOFF() to the void resetManual()

void resetManual()
{
Blynk.virtualWrite(V1, 0);   //Turn OFF Manual Mode Widget
Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget Device
digitalWrite(TestLED, LOW); // set LED OFF
realyOFF();  //turn off pump until next check
}

this does not work. Because the relayOn() will keep triggering the the relay every 10 sec.

My idea now is to
read the state of the TestLED1 because this is constantly high during the device is also ON. So if the TestLED1 is high then the flag1 must be set to 1. Thus eliminating the repeated triggering.
And once the TestLED1 goes low the flag1 will be set back to 0, preparing for it to trigger for the future.

I dont know this theory will work or not. But i am working on this. If anyone feels this doesnt work out. Please stop meā€¦ Wasting time on something that doesnt return anything is BAD :pensive:

Try this. Maybe it will work (maybe not).

int flag1 = 1;

void relayON() {  
 if (flag1 == 0) { 
 flag1 = 1;  
        digitalWrite(TestLED2, LOW);
         timer.setTimeout(2000L, []() {  // Run once after 2 seconds
       digitalWrite(TestLED2, HIGH);
         });  // END Timer Function
        
 }
}


void relayOFF() {  
 if (flag1 == 1) {
 flag1 = 0;  
         digitalWrite(TestLED3, LOW);
        timer.setTimeout(2000L, []() {  // Run once after 2 seconds
       digitalWrite(TestLED3, HIGH);
         });  // END Timer Function
 }
}

void resetManual()
{
Blynk.virtualWrite(V1, 0);   //Turn OFF Manual Mode Widget
Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget Device
digitalWrite(TestLED, LOW); // set LED OFF
realyOFF();  //turn off pump until next check
}

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();
      relayON();  
    }
    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(TestLED, LOW); // set LED OFF
      //Blynk.virtualWrite(V2, 0);
      terminal.print("Monday-Friday STOPPED at");
      terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
     relayOFF();
    }
    else{
      if(nowseconds >= startsecondswd){  
        //digitalWrite(TestLED, HIGH); // set LED ON    test
        //Blynk.virtualWrite(V2, 1);
          relayON(); //may not be needed
        terminal.println("Monday-Friday is ON");
        terminal.flush();
      
      }          
    }
  }
  else{
    terminal.println("Monday-Friday INACTIVE today");
    terminal.flush();
    // nothing to do today, check again in 10 SECONDS time    
  }
  terminal.println();
}
}

Also must add relayOFF(); to the void setup() right after rtc.begin(); so that when it boots it turns the pump off, runs the schedule check 10 seconds later, and then turns the pump on if scheduled to.

Yes ! Done !!

void relayON() {  
 if (flag1 == 0) { 
 flag1 = 1;  
        digitalWrite(TestLED2, LOW);
         timer.setTimeout(2000L, []() {  // Run once after 2 seconds
       digitalWrite(TestLED2, HIGH);
         });  // END Timer Function
        
 }
}


void relayOFF() {  
 if (flag1 == 1) {
 flag1 = 0;  
         digitalWrite(TestLED3, LOW);
        timer.setTimeout(2000L, []() {  // Run once after 2 seconds
       digitalWrite(TestLED3, HIGH);
         });  // END Timer Function
 }
}

This works partially ! > When the device is ON the relay triggers once as it should to turn ON the device and later remains LOW until next trigger is enabled (the 10 sec trigger is now eliminated)ā€¦

but once the device is stopped and the code loops here

terminal.print("Monday-Friday STOPPED at");
      terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
     relayOFF();

The On relay triggers along with Off relay and then the On relay turns off and then the Off relay turns off !! This madness will happen like 3 to 4 times and then calms down.
This is due to the flags being set opposing to one another.

this will turn on the device twice or thrice unnecessarily that to just for 2 sec. This will damage the device on a long run. And when both the relays come on at a time this will damage the pump controller. Its like pressing the ON and OFF switch simultaneously !!!

Lets try rearranging some thing.

everything else is same as before, except:

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);
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    //Serial.println(stopsecondswd);  // used for debugging

    if(nowseconds >= stopsecondswd){
      //digitalWrite(TestLED, LOW); // set LED OFF
      //Blynk.virtualWrite(V2, 0);
      terminal.print("Monday-Friday STOPPED at");
      terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
     relayOFF();
    }
    else if(nowseconds >= startsecondswd){  
      terminal.println("Monday-Friday is ON");  
      terminal.print("Monday-Friday STARTED at");
      terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
      relayON();  
    }
    else{
      terminal.println("Monday-Friday Device NOT STARTED today");
      terminal.flush();
   
    }
 }
  else{
    terminal.println("Monday-Friday INACTIVE today");
    terminal.flush();
    // nothing to do today, check again in 10 SECONDS time    
  }
  terminal.println();
}
}

Thank you for the quick reply. I will try this in the morning. And let you know the result.
Thank you once again.

Youā€™re welcome. What we canā€™t see from the code you have posted is what you have set elsewhere in the code.

The call Blynk.run and any timer settings in your setup routine could be creating the timing trigger you have mentioned. As you are using code for another purpose for what you are looking to achive itā€™s giving you some unintended outcomes.

Can you confirm or deny you are using Blynk to monitor the device?

if you are not, then drop the Blynk routine reference as it is not helping things and is causing most of the challenges

Something like this might help

int loopon = 1;
unsigned long loopdelay;
int timetowait = 60 // 60 second wait time    

void runpumproutine() {
 your code above out of the Bylnk Write routine
}

void setup() {
  loopdelay = millis();  // internal millisecond counter value called and stored

void loop () {
  if (millis() - loopdelay >= (timetowait * 1000)) { //this stops the pump routine from being run for 60 seconds 60 * 1000 milliseconds = 60,000 milliseconds
    loopdelay = millis(); // set start time of the delay loop again
    runpumproutine(); // run/stop the pump
 }
}

This would simplify your control significantly

You would, however, need to reset the device every 48-49 days as the millis() counter resets to 0 in about that time which would screw up the loop timer. Though you could also manage that with other routines testing oldmillis() > millis() making loopdely millis() at that point to keep things running

No there is a terminal window in the Blynk project (app). In there we can see the details about the days selected. active hours of the device, etc etc.

The terminal updates every 10 sec because the code loops back every 10sec, This is to keep a check on when to turn on/off the device. This may give a 10sec delay during the on/off of process. If we increase the 10sec loop to 60sec like @Toro_Blanco saidā€¦ When i want to turn on the device only for a min then it may not turn at the exact moment because of the 60sec delay.

Is there a way i can read the status of the TestLED1 and set the flag accordingly ? If it is high then flag1 = 1; else if the TestLED1 is low then flag1 = 0; < this will ensure to set the flag to 0 for the next trigger.

Is this possible ???

@Toro_Blanco Hoooooo Yeahhhhh !!! This works awesomeā€¦ No repeated triggers !! Working really goodā€¦ All thanks to you. Thank you for helping me out !!!

@jhale716 Thank you for giving your time for giving me various ideas !!

I will learn something new every time i come to this forum From all all you brilliant HEADS !!!

Excited to test the device !!

1 Like

Good to hear!

1 Like

Hello!!

sorry for the trouble again !!

As you know we have four modes

Monday to Friday
Saturday and Sunday
All days
Up to you.

The code what you gave after rearranging works awesome on Monday to Fridayā€¦
So now i am trying to add the same to the rest of the modes, but it is hanging at ā€œmode____Selected is stopped at ____ time.ā€ Neither it is turning on or off the relay.

But monday to friday is working flawlessly !!
here is the code that i have changed for the Saturday and Sunday mode.:point_down:

BLYNK_WRITE(V6) //Saturday-Sunday
 {  
  if (saturdaysunday==1) { 
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
    TimeInputParam t(param);
  
    terminal.print("S-S 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("Saturday-Sunday 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);
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    if(nowseconds >= startsecondswd){
      digitalWrite(TestLED1, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);   
      terminal.print("Saturday-Sunday STOPPED at");
      terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
      relayOFF();
    }
      else if(nowseconds >= startsecondswd){
      digitalWrite(TestLED1, HIGH); // set LED OFF
      Blynk.virtualWrite(V2, 1);
      terminal.print("Saturday-Sunday is ON");
      terminal.print("Saturday-Sunday STARTED at");
      terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush(); 
      relayON();  
     }      
    else{
      terminal.println("Saturday-Sunday NOT STARTED today");
      terminal.flush();
     
    }
  }
  else {
    terminal.println("Saturday-Sunday INACTIVE today");
    terminal.flush();
    }
    terminal.println();
  }  
  }

This is what am getting Saturday-Sunday STOPPED at the terminal. But relays are not triggered !!

Where am i going wrong ??? Can you find the mistake ?

Thank you in advance.

The first if statement is incorrect .it should be stopsecondswd not startsecondswd.

@Toro_Blanco Ufffff !!! I was trying to find the mistake from past 1hr, and you did it in less than a min !! Hats :tophat: off !!!

Thank you so much for the support !! I wish I had a part of your brilliance!!!
I sometimes feel jealous of these BRILLIANT people in this Blynk Forumā€¦:heart_eyes::heart_eyes::heart_eyes::heart_eyes: