BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Need help, tow sliders to an on/off control


#3

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


#4

@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));


}

#5

@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.


#6

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!!!


#7

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


#8

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.


#9

@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?


#10

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.

https://www.arduino.cc/reference/en/language/variables/conversion/longcast/


#11

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 
}

#12

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:


#13

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.


#14

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.


#15

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:


#16

@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.


#17

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?


#18

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…


#19

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?


#20

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.


#21

That is normal for Arduino boards.


#22

:frowning: ok thanks @Gunner