Infinite loop when button is ON. Break when button is OFF

Good afternoon. How i can organize the execution of an infinite loop when pressing the button and exit it when the button is turned off?


BLYNK_WRITE(V1)
 { 
int pinValue = param.asInt(); 
 if(param.asInt()==1)
{ 
  Serial.print("V1 IS ON");
// Want infinite loop here, but break it when V1 button is off
}
  else
{
  Serial.print("V1 IS OFF ");
}

}


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  }

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

How run infinite loop when button is on, and break this loop when button is off?
Thanks and sorry for bad english.

you have to enable a timer to call your function, and disable the timer when press OFF

//in setup 
StartDoTimer = timer.setInterval(1000L, StartDo);
  timer.disable(StartDoTimer ); // Turn StartDoTimer off

BLYNK_WRITE(V1)
 { 
int pinValue = param.asInt(); 
 if(param.asInt()==1)
{ 
timer.enable(StartDoTimer ); // Turn StartDoTimer on
  Serial.print("V1 IS ON");
// Want infinite loop here, but break it when V1 button is off
}
  else
{
  Serial.print("V1 IS OFF ");
timer.disable(StartDoTimer ); // Turn StartDoTimer off
}

}

void StartDoTimer (){
//do something
}

Thank you for quick reply!
I’ll try now…

1 Like

maybe you need more time to execute your “todo something”
so you can change 1000L to an other value

it’s not hard for you to fix my code?
I’m trying to follow your recommendations, but I get errors when compiling
Thank you very much!

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "xxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxx";

BLYNK_WRITE(V1)
 { 
int pinValue = param.asInt(); 
 if(param.asInt()==1)
{ 
  Serial.print("V1 IS ON");
// Want infinite loop here, but break it when V1 button is off
}
  else
{
  Serial.print("V1 IS OFF ");
}

}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  }

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

show me the compile error

oh sorry , I made a mistake

void StartDo (){ /// not StartDoTimer !!!
//do something
}

This is my code:

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "xxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxx";

BLYNK_WRITE(V1)
 { 
int pinValue = param.asInt(); 
 if(param.asInt()==1)
{ 
timer.enable(StartDoTimer ); // Turn StartDoTimer on
  Serial.print("V1 IS ON");
// Want infinite loop here, but break it when V1 button is off
}
  else
{
  Serial.print("V1 IS OFF ");
timer.disable(StartDoTimer ); // Turn StartDoTimer off
}

}

void StartDo (){
//do something
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  StartDoTimer = timer.setInterval(1000L, StartDo);
  timer.disable(StartDoTimer ); // Turn StartDoTimer off
  }

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

Compile error:

sketch_oct05b:15:1: error: ‘timer’ was not declared in this scope
timer.enable(StartDoTimer ); // Turn StartDoTimer on
^
sketch_oct05b:15:14: error: ‘StartDoTimer’ was not declared in this scope
timer.enable(StartDoTimer ); // Turn StartDoTimer on
^
sketch_oct05b:22:1: error: ‘timer’ was not declared in this scope
timer.disable(StartDoTimer ); // Turn StartDoTimer off
^
sketch_oct05b:22:15: error: ‘StartDoTimer’ was not declared in this scope
timer.disable(StartDoTimer ); // Turn StartDoTimer off
^
/tmp/arduino_modified_sketch_600126/sketch_oct05b.ino: In function ‘void setup()’:
sketch_oct05b:35:3: error: ‘StartDoTimer’ was not declared in this scope
StartDoTimer = timer.setInterval(1000L, StartDo);
^
sketch_oct05b:35:18: error: ‘timer’ was not declared in this scope
StartDoTimer = timer.setInterval(1000L, StartDo);
^

exit status 1
‘timer’ was not declared in this scope

you need to declare that

/***************** Library for Blynk real-time clock *****************/

#include <WidgetRTC.h>

BlynkTimer timer;
WidgetRTC rtc;

I am very sorry for wasting your time, but I am confused

I am now reading the documentation on timers, but for now I can not figure out what exactly causes an error.

This is my fixed code:

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <WidgetRTC.h>

char auth[] = "xxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxx";

BlynkTimer timer;
WidgetRTC rtc;

BLYNK_WRITE(V1)
 { 
int pinValue = param.asInt(); 
 if(param.asInt()==1)
{ 
timer.enable(StartDoTimer ); // Turn StartDoTimer on
  Serial.print("V1 IS ON");
// Want infinite loop here, but break it when V1 button is off
}
  else
{
  Serial.print("V1 IS OFF ");
timer.disable(StartDoTimer ); // Turn StartDoTimer off
}

}

void StartDo (){
//do something
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  StartDoTimer = timer.setInterval(1000L, StartDo);
  timer.disable(StartDoTimer ); // Turn StartDoTimer off
  }

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

Compile errors:

/tmp/arduino_modified_sketch_566374/sketch_oct05b.ino: In function ‘void BlynkWidgetWrite1(BlynkReq&, const BlynkParam&)’:
sketch_oct05b:19:14: error: ‘StartDoTimer’ was not declared in this scope
timer.enable(StartDoTimer ); // Turn StartDoTimer on
^
sketch_oct05b:26:15: error: ‘StartDoTimer’ was not declared in this scope
timer.disable(StartDoTimer ); // Turn StartDoTimer off
^
/tmp/arduino_modified_sketch_566374/sketch_oct05b.ino: In function ‘void setup()’:
sketch_oct05b:39:3: error: ‘StartDoTimer’ was not declared in this scope
StartDoTimer = timer.setInterval(1000L, StartDo);
^
exit status 1
‘StartDoTimer’ was not declared in this scope

you need to declare :wink:

int StartDoTimer;

thank you so much and sorry again for your time!

1 Like

don’t worry

Сompilation passes without errors, but when i loaded this to esp32 apparently the timer does not work…

Final code:

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <WidgetRTC.h>

char auth[] = "xxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";
int StartDoTimer;
BlynkTimer timer;
WidgetRTC rtc;

BLYNK_WRITE(V1)
 { 
int pinValue = param.asInt(); 
 if(param.asInt()==1)
{ 
timer.enable(StartDoTimer); // Turn StartDoTimer on
  Serial.print("V1 IS ON");
}
  else
{
  Serial.print("V1 IS OFF ");
timer.disable(StartDoTimer ); // Turn StartDoTimer off
}

}

void StartDo (){
Serial.print("Print This Line One Time Per second");
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  StartDoTimer = timer.setInterval(1000L, StartDo);
  timer.disable(StartDoTimer); // Turn StartDoTimer off
  }

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

When i pressed button V1, in console i’m getting: V1 IS ON or V1 if OFF, but not the “Print This Line One Time Per second”

Where could i mess up again?

1 Like

Found the mistake.

void loop()
{
  Blynk.run();
  timer.run();  // <----- Add this to void loop()
}

This is the right way?

not sure, but it seems you have to add real time clock widget

05-024060

yes it is the mistake :wink:

Now everything seems to work.

1 Like

I saw that the code works without

#include <WidgetRTC.h>
/WidgetRTC rtc;

i’m commented this lines and everything seems to work.