Function to run only once

If you don’t want it to trip the relay again… then don’t reset the flag.

Or determine the logic that dictates if and when you want to trip the relay again and reset the flag there.

1 Like

The problem is we do not know what you are trying to do.

Ok let me explain.

There is a pump and that has 2 switches. One switch to turn on and other to turn it off !!

Now what am trying to do is, instead of the switches am replacing it with a relay. Basically doing the same stuff as the switch but adding the ability to be controlled by a NODEMCU. And injecting it with :ok_hand:t2:BLYNK !!

Now i want to mimic the press of the button with the relay ! Like turning on the relay for 2sec and turning back off . This will trigger the pump . And when we do the same with the other relay the pump turns off .

This is the functionality i am looking for. This is not a BLYNK related problem at all !!! But I am looking for a way out of this here . Pardon me for this.

As @Gunner said placing a flag and setting it 0 will help and yes it worked. But every time the loop occurs where ever we place the flag, it runs through it sets it zero and again triggers the relay like it was a new command .

I have set 2 function to trigger the 2 different relay’s for 2sec with 2 different flags for each.As Gunner’s states in the earlier post . And yes it works for the turn off relay . But not the turn on relay . It just loops back setting the the flag 0 and triggers the relay back.

Can you please tell me where to place the flag so that this doesn’t occur?

ok very easy code

relaybutton is the vpin that is used on the app

void relayoff() {
  digitalWrite(RELAY_PIN, LOW);             // turn off pin this causes the relay to turn off
  Blynk.virtualWrite(relaybutton, 0);      // display relay off
  flag = 0;
}

void relayon() {
    if (!flag) {
      flag = 1;
      digitalWrite(RELAY_PIN, HIGH);              // send power to relay
      timer.setTimeout(5000L, relayoff);        // turn off after 5 seconds  << change this to whatever delay you want
    }
    Blynk.virtualWrite(relaybutton, 1);          // update the app
}

the flag needs to be a global var

Sorry I forgot to mention. I am modifying an existing Blynk project.

Can you please have a look at this code once please.

This is the part of the code tnat am trying to modiy. There is no dedicated button to turn the relays on.

Once the schedule occurs the relays must work as i said earlier.
Yes i have a void relayon () as global .

You need to properly format code so I can read it

Oops sorry… i am on my phone. And it’s difficult to use here. Sorry for the mess. Now its been edited.

I don’t see it.

Also you can just call the function from the code with the same result.

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.

It will not turn on again until after the time has expired. if you use the functions I supplied.

It is turning on. Because it keeps checking every 30 sec whether to turn on the device or not. then it turns the relay on. If am not resetting the flag to 0, then first time it will work as it should. And the second time it wont. Because the flag will be set to 1.

I have placed the flag2 in place and is working fine. But the flag1 (the turn on relay flag) is the problem.

can you please help me achieve this ? Can you give me some example ?

the relayoff code resets the flag so that it can be turned on again. In the relay on code I have a timer set to call the relay off after 5 seconds If you do not want this action then delete the timer code.

It will then only be able to run the code again if you first call the relay off function.

Bro however as we are in the same loop and calling the relayoff function it will set the relayon flag to 0 and again when the loop happens the relay triggers. IF AM NOT WRONG…because i tried this well before, and failed :tired_face:

why are you calling relay off and on in the same loop.

Because i want the same procedure to be carried out next time the schedule happens…

I am not finding any other way…

You must figure out your logic before you can be helped.

yeah i have understood the logic. I think am not able to explain better here in words.

If there is no flags at all, the ON relay clicks every 10 sec till the scheduled period is ON. and once the Schedule is done the OFF relay clicks every 10 sec till the next schedule comes.

To eliminate this i used @Gunner flag idea, this works partially.

if i reset the device every time the flag idea works flawlessly !! But not when looping because i am not setting and reset to the flag.

Now when i am resetting the flag i need to do it a the loop itself !! Because next time the schedule happens it will reset the flag and work like the 1st time(just like the device was reset).

But as the loop happens every 10 sec the flag will get reset and trigger the relay.

Is there a way to accomplish this out side the loop ? (to reset the flag and keep it ready for the next schedule !!??)

use the time to set a schedule for the reset. Like reset every 24 hours or whatever you like.