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
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.
@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.
I would recommend much longer timers… 100ms is rather quick, unless you somehow need the precision of 10 reads a second
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