I found this code in another post. I’m thinking that this is what I need it for my project. This help me to ON a lamp by some seconds using a slider in the app, writing to V0 port.
// thirtysecondtimer.ino by Costas 12 Dec 2016
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h> // library for SimpleTimer
SimpleTimer timer; // define a timer for use by SimpleTimer library
int Countdown; // Global variable used in Slider widget and runEveryMinute()
bool ONstatus = false; // variable to switch device ON and OFF
char auth[] = "token";
BLYNK_WRITE(V0) { // add a slider to your project on V0 range 0 to 30 (minutes)
Countdown = param.asInt(); // set variable as Slider value
}
void runEveryMinute(){ // runs every 60s, will do noting when Slider is at zero
if((Countdown > 0)&& (ONstatus == true)){
Countdown--; // reduce Countdown by 1 minute every 60s
Serial.print(F("Device will switch OFF in "));
Serial.print(Countdown);
Serial.println(F(" minute(s)"));
}
if((Countdown > 0) && (ONstatus == false)){
Serial.println(F("Device was switched ON"));
ONstatus = true; // to ensure device is only turned ON once
// code here to turn your device ON
}
if((Countdown == 0) && (ONstatus == true)){
Serial.println(F("Device is now OFF"));
ONstatus = false; // to ensure device is only turned OFF once
// code here to turn your device OFF
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth); // this is now a blocking function - more on this later
timer.setInterval(1000L, runEveryMinute); // start the 60s timer function
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer to ensure the 60s timer keeps running
}
In my project I use 2 sliders, one for ON the lamp an another for OFF the lamp. I try to use 2 rutines “void runEveryMinute()” one for ON the lamp and other for OFF the lamp but when I test it do not work like I need because work the 2 rutines at the same time, so got readings like:
The lamp switch OFF in 25 seconds
The lamps switch ON in 35 seconds
I think because the code it’s telling to work in that way
void setup()
{
Serial.begin(115200);
Blynk.begin(auth); // this is now a blocking function - more on this later
timer.setInterval(1000L, runEveryMinute1); // start the ON slider timmer. runEveryMinute1 write to V0
timer.setInterval(1000L, runEveryMinute2); // start the OFF slider timmer. runEveryMinute2 write to V1
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer to ensure the 60s timer keeps running
}
But what I want to do is that immediately that the ON slider run out starts to work with de OFF slider and when this slider run out come back to the ON slider and so on forever.