Run loop continuously by pressing Switch Button

The Button in my sketch (V0) is acting as SWITCH. So, on pressing, its state become “1”. My slider value is

always set manually less than sensor value. So, naturally, StartProcess() loop should run continuously on

pressing button. But, when i press button, StartProcess() loop runs only once.

Q: I know that this on pressing switch, this loop StartProcess() should run for indefinite time until I switch

the button back. How can i achieve this?

Note: Sensor is not a temperature sensor. I have studied timer function but i do not know whether its use

can help or not. Thanks.

#include <BlynkSimpleStream.h>
#define BLYNK_PRINT Serial1
char auth[] = "6466cdabf65c4b9dafae7d606991767a";

int SliderValue, SensorValue, FanPin = 7, led = 13;

void setup() {
  pinMode(FanPin, OUTPUT);
  pinMode(led, OUTPUT);
  Serial1.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

void loop() {
  Blynk.run();
  Sensor(); // to display sensor value continously, i put this loop here
}

BLYNK_WRITE(V0) { // Control Button
  int pinValue = param.asInt();
  if (pinValue == 1) {                              // if switch is pressed
    digitalWrite(led, HIGH);                  // turn led at pin 13 on
    StartProcess();                               // Run this loop continuously until switch pressed again.
  } else
    StopProcess();                           // if switch is not pressed, run stop loop.
  digitalWrite(led, LOW);
}

void Sensor() {
  SensorValue = analogRead(A0); // Get value from sensor (This is not temp sensor)
  Blynk.virtualWrite(V1, SensorValue); //display sensor value
}

BLYNK_WRITE(V2) { // Slider
  SliderValue = param.asInt(); // already declared SliderValue as int globaly
}

void StartProcess() {
  if (SliderValue < SensorValue) {
    digitalWrite(FanPin, HIGH);  // Turn Fan ON
    delay (1000);
    digitalWrite(FanPin, LOW);  // Turn Fan OFF
  }
  else {
    digitalWrite(FanPin, LOW);  // Turn Fan OFF
  }
}

void StopProcess() {
  digitalWrite(FanPin, LOW); // Turn Fan OFF
}

The code executes exactly what you told it to do. You need to enable/disable the Timer functions to achieve what you want.

Study the Push Data example. It tells you how to make a timed loop. After that, study the SimpleTimer enable/disable commands on the Arduino.cc website :slight_smile:

By putting this command directly in the void loop() it is running that function hundreds of times a second… even with the latest changes in the Blynk library, that might prevent flooding and disconnection, this is BAD Blynk style programming, and just may be contributing to other issues… Yes, use a timer instead.

You also want to avoid using delays lest they cause disconnections… again, a timeout style timer function will work instead of a delay.

Pseudo code (meaning: it’s just logic and will not work if you copy/paste it):

if V0 = HIGH
{
timer.enable(1000, DoTImedStuff();
}
else
{
timer.disable(timerID);
}

@Lichtsignaal, Studying SimpleTimer helped to understand Blynk Timer. I didn’t get the pseudo code and unable to make it work.
@Gunner, removed command from Void loop. Also removed delay().

Now the code is working fine. Please comment on the code to make it more robust according to Blynk style programming.

#include <BlynkSimpleStream.h>
#define BLYNK_PRINT Serial1
BlynkTimer timer;
char auth[] = "6466cdabf65c4b9dafae7d606991767a";

int pinValue = 0, SliderValue = 0, SensorValue, FanPin = 7;

void setup() {
  pinMode(FanPin, OUTPUT);
  Serial1.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  Blynk.virtualWrite(V2, 0);
  timer.setInterval(100L, Sensor);
  timer.setInterval(100L, checkLogic);
}

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

BLYNK_WRITE(V0) { // Control Button
  pinValue = param.asInt();
  Blynk.virtualWrite(V3, pinValue);
}

void Sensor() {
  SensorValue = analogRead(A0);
  Blynk.virtualWrite(V1, SensorValue);
}

BLYNK_WRITE(V2) { // Slider
  SliderValue = param.asInt();
}

void StartProcess() {
  if (SliderValue < SensorValue) {
    digitalWrite(FanPin, HIGH);
  }
  else {
    digitalWrite(FanPin, LOW);
  }
}

void StopProcess() {
  digitalWrite(FanPin, LOW);
}

void checkLogic() {
  if (pinValue == 1) {
    StartProcess();
  } else
    StopProcess();
}

I would recommend much longer timers… 100ms is rather quick, unless you somehow need the precision of 10 reads a second :wink:

Also, either run one timer for both (combined) function actions, or stagger the two timer’s times… otherwise both are trying to run at the exact same time every 100ms.

Hi @Gunner, I would be grateful if you explain it run one timer for both (combined) function actions, or stagger the two timer’s times with a simple example.

Sensor() and checkLogic() are two separate functions… if their contents can run together, then move the contents from one into the other and just call that one function.

But if for some reason they need to run separately then just space out the timers…

timer.setInterval(100L, Sensor);  // Runs every 100 milliseconds
timer.setInterval(125L, checkLogic);  // Runs every 125 milliseconds

This way Sensor() runs first, then 25ms later checkLogic() runs.

I still say both are probably repeating way to fast anyhow :wink:

I got it. Thanks for prompt reply :slight_smile: