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

Please help with timer


#1

Hi everyone, I need to know how do I push an timelapse from app to an arduino pin. My project will work in this way:I’ll have 1 AC outlet and I want to setup to work like on/off sistem, sending from the app a timelapse between 0 seconds to 120 seconds (or 2 minutes) for the ON state and making the same for the OFF state. I want to set this by some kind of timmers I ghess from the app, maybe a slider because the timmer widget does’n allow me to setup the timmer in seconds neither to make this undefitinely.
Can anyone help me?


#2

We can point you to the info needed so you can learn how to do it :wink:

http://docs.blynk.cc/#blynk-firmware-blynktimer

This might help understand the basics of Blynk Timer

And this has some aspects of how you can variably adjust the delay of a timer with a Widget like Slider or Step


#3

Many thanks, I started to understand. I think maybe this can be usefull to me:

  timer.setInterval(500L, blinkMyLEDon);  // This will call the Arduino Function 'void blinkMyLEDon()' every 
  500ms (1/2 second)

  timer.setInterval(1000L, blinkMyLEDoff);  // This will call the Arduino Function 'void blinkMyLEDoff()' 
  every 1000ms (1 second)

In your first answere, but I have some questions. The first one: why put a “L” before the 500? the second one, can I use a variable to read the value from a slider and then include the variable as the timmer interval?

Like this:

TimeLed1On= virtualRead(V2);

if(TimeLed1On>0){
  timer.setInterval(TimeLed1On, blinkMyLEDon);  // This will call the Arduino Function 'void blinkMyLEDon()' 
  every 1000ms (1 second)
}

And then use another variable:

TimeLed1Off=virtualRead(V3);

if(TimeLed1Off>0){
  timer.setInterval(TimeLed1Off, blinkMyLEDoff);  // This will call the Arduino Function 'void blinkMyLEDoff()' 
  every 1000ms (1 second)
}

Can work for me?


#4

You meen after the number… it references the number as a LONG datatype

https://www.arduino.cc/reference/en/language/variables/data-types/long/

Yes… but you need to stop the timer before changing and then restarting it (but it restarts from beginning)… at least that is as far as I have gotten with it.

Study that 2nd link I gave with the slow servo demo… it uses precisely that method.

I don’t know… try it… if not, study the examples and try again :wink:


#5

What happent if I don’t use the “L” after?


#6

I don’t know… possibly nothing if the number is small enough?? Why leave it out?


#7

It just count to 120 and start again.


#8

What does?.. what are we talking about :stuck_out_tongue:


#9

Yes. L is technically only need if the size in ms is a “long” number but it’s quite common to use it at all times. Long is a different size depending on which MCU you are using.


#10

I´m using an arduino uno


#11

Hi @Costas I found this code, I think it’s yours. 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 ...
  timer.setInterval(1000L, runEveryMinute2);  // start the OFF slider timmer ...
}

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.