How to continuous to execute the subroutine on the arduino

Description of the problem status:
At present, the blynk app has a button that turns ON and executes a cool subroutine.
I want to execute a cool subroutine,the button of the blynk must be off before it executes a cool subroutine.

How can I continue to execute a cool subroutine when the blynk app button is ON?

Many thanks.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>


IRsend irsend(4);  // An IR LED is controlled by GPIO pin 4 (D2)  

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "z56s5XG1JxsNebclzJ6Jkuo-vY44TLyA";   

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "CPH1607";                       
char pass[] = "12345678";

void setup()
{
  // Debug console
  irsend.begin();
  Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY);
 //Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  Serial.println( "Blynk Ready!" );

}

void loop()
{
  Blynk.run();
}

BLYNK_WRITE(V0){                               
  int onoff = param.asInt();
  if(onoff == 1){
  cool(); //call void cool
  }
 }
void cool()
{
  Serial.print( "cool mode" );
    }

Please edit your post to include triple backticks at the start and end of your code.
Until then, unfortunately, we aren’t going to look into your issue. (Well I won’t at least)

```
your code
```
1 Like

@111105, as @JustBertC said, please put triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Also, a better explanation of exactly what it is that you’re trying to achieve is needed before we can give you a clear answer.

Pete.

I appreciate your help very much

when onoff == 1 ,cool subroutine print"cool mode"
But the phone’s button is on, it will be executed once,can’t to continuous print"cool mode"
I want to be able to continue “cool mode” when the button is on

Use a timer.


int coolTimer;

BlynkTimer timer;

BLYNK_WRITE(V0){                               
  int onoff = param.asInt();
  if(onoff == 1){
  coolTimer = timer.setInterval(1000, cool);//call void cool every 1 second
  }
else
{
timer.deleteTimer(coolTimer);
}
 }

void cool()
{
  Serial.print( "cool mode" );
    }

How frequently would you like this message to be sent to your serial monitor?
Once every second, every 10 seconds, every minute?

Pete.

Thank you so much for helping me out today

I used a timer can every second sent to serial monitor

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>

int coolTimer;
BlynkTimer timer; 

IRsend irsend(4);  // An IR LED is controlled by GPIO pin 4 (D2)   

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "z56s5XG1JxsNebclzJ6Jkuo-vY44TLyA";   

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "CPH1607";                       
char pass[] = "12345678";

void setup()
{
  // Debug console
  irsend.begin();
  Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY);
 //Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  Serial.println( "Blynk Ready!" );

}

void loop()
{
  Blynk.run();
  timer.run();
}

BLYNK_WRITE(V0){                               
  int onoff = param.asInt();
  if(onoff == 1){
  coolTimer = timer.setInterval(1000, cool);
  }
  else
  {
  timer.deleteTimer(coolTimer);  
  }
 }
void cool()
{
  Serial.print( "cool mode" );
    }
2 Likes