Need help, tow sliders to an on/off control

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.

Not clear. Do you want the light to be on for 25 secs as per ON slider and then when it goes off, remain off for 35 seconds as per the OFF slider and then turn on? This runs continuously?

If this is the case, the Countdown is decremented only when the ONstatus is true, not when it is false. The OFF timer will not get decremented at all.

There are easier methods like timeout that can be used to do this. Look up examples.

As you understood @mohan_sundaram, that’s what I want my project do. Many thanks @mohan_sundaram I’ll search in the examples!

@AdrianStivalet Let us say on time is x seconds and off time is y seonds. Then you will need to run the on timer every (x+y) seconds. In the call that you are executing to turn the LED on, set a timeout call to invoke the off routine at the end of x seconds. You will be home.

void turn_on()
{
//write to pin to turn the LED on.
timer.setTimeout(turn_off,x);
} 

void turn_off()
{
//write to pin to turn off LED.
}

void setup()
{
.
.
.
.
.
timer.setInterval(turn_on,(x+y));


}

@AdrianStivalet I have a small example here of using a slider to variably adjust the timeout of an action. Perhaps you can extrapolate from it to do what you want.

Hi again @mohan_sundaram I code this, but do not work, I know I’m making somthing wrong. But can understand what it is.

#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 WM1=3;
int EncWV1=0;                // Global variable used in Slider widget and take it for turn_on
int ApagWV1=0;                // Global variable used in Slider widget and take it for turn_off

char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

BLYNK_CONNECTED() {
  Blynk.syncAll();  // Synchronize hardware with App widgets when connected
}

BLYNK_WRITE(V0) {   // read the value from slider 1
  EncWV1 = param.asInt();  // set variable as Slider value
}

BLYNK_WRITE(V1) {   // read the value of slider 2
  ApagWV1 = param.asInt();  // set variable as Slider value   
}

void turn_on()
  {
  digitalWrite(WM1,HIGH);   //write to pin to turn the LED on.
    Serial.print(F("El generador de olas 1 se apagara en:  "));
    Serial.print(EncWV1);
    Serial.println(F(" segundo(s)"));
    timer.setTimeout(turn_off,EncWV1);
  } 

void turn_off()
  {
  digitalWrite(WM1,LOW);    //write to pin to turn off LED.
    Serial.print(F("El Generador de Olas 1 apagado durante:  "));
    Serial.print(ApagWV1);
    Serial.println(F(" segundo(s)"));  
  }


void setup()
{
  Serial.begin(115200); 
  Blynk.begin(auth);  
  timer.setInterval(turn_on,(EncWV1+ApagWV1));  // I'm not so sure if this is right
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer 
} 

Please help!!!

Hi @Gunner I’m new in this so I can get the general idea from your post but I can’t understand it, It’s dificult to me translate it to my project :frowning: can you explain it to me.
Kind regards and thaks in advance

What you are doing is close, but you can’t use the setInterval timer as that one runs constantly. You want to use a different type of timer.

Perhaps look at this post… particularly the Latch and Indicator use of the setTimout timer… and use your slider to supply the time portion as in your current code.

@Gunner Maybe I can use this:

void blinkMyLEDon() {
  digitalWrite(DeviceLED, LOW); // Turn ON built in LED (triggered LOW)
  timer.setTimeout(500L, blinkMyLEDoff);  // Run LED OFF routine once in 1/2 second
}  // END Function

void blinkMyLEDoff() {
  digitalWrite(DeviceLED, HIGH); // Turn OFF built in LED (triggered LOW)
}  // END Function

My question here it’s if I can subtitute the “500L” in “timer.setTimeout” with a variable that takes the slider value for turn the led off? Can I do that?

Yes, that is what happens in my first example with the servo… just use same process in that example but with the different timer setTimout like in the 2nd example with the latch.

In case you want really long timers, and since timers use milliseconds (AKA 1000ms = 1 second), make sure you define the variable as a Long() not an integer.

Hi again @Gunner I can’t made it :frowning_face: this is the code as I use but do nothing. I’m sure I making someting or everyting wrong. Please help me.

Thanks in advance!

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>      // library for SimpleTimer       

BlynkTimer timer;            // define a timer for use by SimpleTimer library
int WM1=3;
long EncWV1=0;                // Global variable used in Slider widget and runEveryMinute()
int x=0;
long ApagWV1=0;                // Global variable used in Slider widget and runEveryMinute()
int y=0;

char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

BLYNK_CONNECTED() {
  Blynk.syncAll();  // Synchronize hardware with App widgets when connected
}

BLYNK_WRITE(V0) {   // add a slider to your project on V0 range 0 to 30 (minutes)
  x = param.asInt();  // set variable as Slider value
  EncWV1 = x*1000;
  timer.setInterval(EncWV1,turn_on);  // start the 60s timer function  
  timer.setTimeout(ApagWV1,turn_off);
}

BLYNK_WRITE(V1) {   // add a slider to your project on V0 range 0 to 30 (minutes)
  y = param.asInt();  // set variable as Slider value 
  ApagWV1 = y*1000;  
}

void turn_on()
  {
  digitalWrite(WM1,HIGH);   //write to pin to turn the LED on.
  } 

void turn_off()
  {
  digitalWrite(WM1,LOW);    //write to pin to turn off LED. 
  }


void setup()
{
  Serial.begin(115200); 
  Blynk.begin(auth);  // this is now a blocking function - more on this later
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer to ensure the 60s timer keeps running 
}

I have this other code:

#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 WM1=3;
int CountdownEnc;                // Global variable used in Slider widget and runEveryMinute()
int CountdownApag;                // Global variable used in Slider widget and runEveryMinute()
bool ONstatus  = false;       // variable to switch device ON and OFF
bool ONstatus2 = false;
char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

BLYNK_CONNECTED() {
  Blynk.syncAll();  // Synchronize hardware with App widgets when connected
}

BLYNK_WRITE(V0) {   // add a slider to your project on V0 range 0 to 30 (minutes)
  CountdownEnc = param.asInt();  // set variable as Slider value
}

BLYNK_WRITE(V1) {   // add a slider to your project on V0 range 0 to 30 (minutes)
  CountdownApag = param.asInt();  // set variable as Slider value   
}



void SliderControlONOFFWM1(){ // runs every 60s, will do noting when Slider is at zero
  
  if((CountdownEnc > 0) && (ONstatus == true) && (ONstatus2 == false)){  //1
    CountdownEnc--;    //  reduce CountdownEnc by 1 second every 1000 milis
    Serial.print(F("El generador de olas 1 se apagara en:  "));
    Serial.print(CountdownEnc);
    Serial.println(F(" segundo(s)"));    
  }  

  if((CountdownEnc > 0) && (ONstatus == false) && (ONstatus2 == false)){  //2
    Serial.println(F("El generador de olas 1 se encendio"));
    ONstatus = true;   // to ensure device is only turned ON once
    ONstatus2 = false;
    digitalWrite(WM1,HIGH);   // code here to turn your device ON 
  }
  
  if((CountdownEnc == 0) && (ONstatus2 == false)  && (ONstatus == true)){  //3
    Serial.println(F("El generador de olas 1 esta apagado"));
    digitalWrite(WM1,LOW);// code here to turn your device OFF  
    ONstatus = false;      
  }  

  if((CountdownApag > 0) && (ONstatus2 == true) && (ONstatus == false)){  //4
    CountdownApag--;    //  reduce CountdownApag by 1 minute every 60s
    Serial.print(F("El Generador de Olas 1 apagado durante:  "));
    Serial.print(CountdownApag);
    Serial.println(F(" segundo(s)"));    
  }  

  if((CountdownApag > 0) && (ONstatus2 == false) && (ONstatus == false)){  //5
    Serial.println(F("El Generador de Olas 1 esta apagado"));
    ONstatus2 = true;   // to ensure device is only turned ON once
    ONstatus = false;
    digitalWrite(WM1,LOW);   // code here to turn your device ON 
  }

  if((CountdownApag == 0) && (ONstatus2 == true) && (ONstatus == false)){  //6
    Serial.println(F("El Generador de Olas 1 encendio"));
    digitalWrite(WM1,HIGH);// code here to turn your device OFF    
    ONstatus2 = false;   
    ONstatus = false; 
  }  
}


void setup()
{
  Serial.begin(115200); 
  Blynk.begin(auth);  // this is now a blocking function - more on this later
  timer.setInterval(1000L, SliderControlONOFFWM1);  // start the 60s timer function
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer to ensure the 60s timer keeps running 
}

This code works fine but only once :frowning:

When the slide on off routine is executed, for every execution, you should be decrementing the counter value. Else only part of the loop will keep executing all the while seeming like only action is being taken.

Should be timer.setInterval((EncWV1+ApagWV1),turn_on), is it not? You want the light to turn on every EncWV1 seconds and go off after ApagWV1 seconds.

Put these timer.setInterval call into setup routine instead of the BLYNK_WRITE call. Let the timeout call alone be in the BLYNK_WRITE. Let us know what happens.

Not at home. Else would’ve been able to try this sketch and let you know. Maybe later tonight.

Hi @mohan_sundaram thanks for helping me, do you talk about the first code? if yes I wrote this code, taking your advices, but nothing happen, well nothing that I want, the led remains on, never go off, thats way I say nothing happens :slight_smile:

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>      // library for SimpleTimer       

BlynkTimer timer;            // define a timer for use by SimpleTimer library
int WM1=3;
long EncWV1=0;                // Global variable used in Slider widget and runEveryMinute()
int x=0;
long ApagWV1=0;                // Global variable used in Slider widget and runEveryMinute()
int y=0;

char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

BLYNK_CONNECTED() {
  Blynk.syncAll();  // Synchronize hardware with App widgets when connected
}

BLYNK_WRITE(V0) {   // add a slider to your project on V0 range 0 to 120 (seconds)
  x = param.asInt();  // set variable as Slider value
  EncWV1 = x*1000;
  timer.setTimeout(ApagWV1,turn_off);
}

BLYNK_WRITE(V1) {   // add a slider to your project on V1 range 0 to 120 (seconds)
  y = param.asInt();  // set variable as Slider value 
  ApagWV1 = y*1000;  
}

void turn_on()
  {
  digitalWrite(WM1,HIGH);   //write to pin to turn the LED on.
  } 

void turn_off()
  {
  digitalWrite(WM1,LOW);    //write to pin to turn off LED. 
  }


void setup()
{
  Serial.begin(115200); 
  Blynk.begin(auth); 
  timer.setInterval((EncWV1+ApagWV1),turn_on);  // start the timer function  
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer to ensure the timer keeps running 
} 

Maybe I don’t get it yet :frowning:

@AdrianStivalet Can you try this sketch? Works as intended. Turns on for x minutes, goes off for y minutes and the cycle continues.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>

BlynkTimer timer;            // define a timer for use by SimpleTimer library
int WM1=3;
int EncWV1=3;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV1=3;                // Global variable used in Slider widget and runEveryMinute()
int x, y;
int CounterOn = 0;
int CounterOff = 0;
int ledStatus = 0;

char auth[] = "token";

BLYNK_CONNECTED() {
  Blynk.syncAll();  // Synchronize hardware with App widgets when connected
}

BLYNK_WRITE(V0) 
{   // add a slider to your project on V0 range 0 to 120 (seconds)
  x = param.asInt();  // set variable as Slider value
  EncWV1 = x;
  Serial.print("LED on timer set to ");
  Serial.print(EncWV1);
  Serial.println(" minutes");
}

BLYNK_WRITE(V1) 
{   // add a slider to your project on V1 range 0 to 120 (seconds)
  y = param.asInt();  // set variable as Slider value 
  ApagWV1 = y;  
  Serial.print("LED off timer set to ");
  Serial.print(ApagWV1);
  Serial.println(" minutes");
  }

void led_control()
{
  Serial.print("\n");
  Serial.print(minute());
  Serial.println(":: Executing check loop this minute");
  
  if ((CounterOn > 0) && (ledStatus == 0))
  {
      digitalWrite(WM1,HIGH);   //write to pin to turn the LED on.
      Serial.print(minute());
      Serial.print(": LED on\n");
      ledStatus = 1;
      CounterOff = 0;
   }
   
  if ((CounterOn > 0) && (ledStatus == 1))
      {
      CounterOn = (CounterOn - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 1))
    {
      CounterOff = ApagWV1;
    }

  if ((CounterOff > 0) && (ledStatus == 1))
  {
      digitalWrite(WM1,LOW);   //write to pin to turn the LED off.
      Serial.print(minute());
      Serial.print(": LED off\n");
      ledStatus = 0;
      CounterOn = 0;
   }
   
  if ((CounterOff > 0) && (ledStatus == 0))
      {
      CounterOff = (CounterOff - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 0))
    {
      CounterOn = EncWV1+1;
    }

}

void setup()
{
  if (EncWV1== 0)
  {
    EncWV1=1;
  }
  if (ApagWV1== 0)
  {
    ApagWV1=1;
  }
  Serial.begin(115200); 
  Blynk.begin(auth, "Linksys-01750", "sundaram!@#");
  CounterOn = EncWV1+1;
  // run timer every minute to check for led On/Off action
  timer.setInterval(60000L, led_control);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates Timer to ensure the timer keeps running 
} 

Edit: I’ve a wireless ESP8266 and have included files accordingly. Please modify as per your set up and try this sketch.

2 Likes

Wow many thanks @mohan_sundaram for taking time to code this for me, if I want to control the time in seconds have to modify this line: timer.setInterval(60000 L, led control); right?

Make 60000L to 1000L and this timer will call the led_control function every second. I suggest you remove/ comment the first three Serial.print() lines immediately on entry into led_control() function to avoid a flurry of serial output. You may also want to comment out the Serial.print() calls when the LED goes on or off.

Happy Blynking…

1 Like

Indeed happy blynking :slight_smile: many thanks @mohan_sundaram I have one question. The program is running quite fine, the only thing is that when I open the serial monitor the blynk conection restarts and the arduino too. why is that? can I avoid this?

Not sure. I’m using a NodeMCU board and that worked well. Did not have a rebooting issue. I’ve seen like @Costas say that that the board needs to be reset (hard reset) once post flashing. Please try that.

1 Like