Timer after press button

Hi to all

i have test blykn in this day and is very fantastic…i have a simple question

If i press a button in ON status, i need to start relay for 10 sec and stop for 20 sec…start for 10 sec and stop for 20 sec (in loop) if i switch in off…i stop loop

someone I can 'help by showing me the correct syntax about code?

Thanks very much

Got no time to test this myself, this should be added to your code with appropriate libraries
You need libraries and Blynk code but the logic to your request should be covered by the code below.

EDIT, i missed the “off for 20 seconds” part, that requiers another timer calling the same function.

SimpleTimer timer;

int relayPin = 0; // adjust accordingly
int timerNo;   // ID of timer to start stop
int onState = 0; // 1 is On 0 is Off
int onTime = 10; // seconds
int offTime  = 20; // seconds
boolean isOn = false; // toggle flag for relay

void setup()
   timerNo = timer.setInterval(onTime * 1000L, onOffToggler); // Do function every 10 sec, if enabled
   timer.disable(timerNo); // start disabled

   // After blynk configs etc make sure that you start off with "off state"
  Blynk.virtualWrite(V0, onState);

// Set button to switch mode
   onState = param.asInt();
   if (onState == 1)  
       timer.enable(timerNo); // Turn on
       timer.disable(timerNo); // Turn off

// MCU operations
void onOffToggler()
   if (!isOn)
       digitalWrite(relayPin, HIGH);
       timerNo = timer.setInterval(onTime * 1000L, onOffToggler);
       isOn = true;
      digitalWrite(relayPin, LOW);
      timerNo = timer.setInterval(offTime * 1000L, onOffToggler);
      isOn = false;

void loop()
   //Dont forget your Blynk.run() etc....

@Gunner @Lichtsignaal lend me your eyes did I miss anything ^^? Bit tired


Looks quite alright to me. I’m a bit in a personal crisis, so my brain isn’t running at 100% either.

If it’s more serious than that the cat(s) literarly got your tounge, I hope you solve it! Never fun being in a crisis. Good luck! :confused:

1 Like

Thanks very very much…buy i have a error…what is this ?

Boolean isOn = false; // toggle flag for relay

Read here about boolean

Apologies should apparently be a small b, edited code

Thanks! i have a error…

error: ‘isOn’ was not declared in this scope

if (!isOn)


exit status 1
‘Boolean’ does not name a type

Ahh perfect thanks…now i have a last error …

expected unqualified-id before ‘{’ token

Probably a misplaced bracket or missing clsoing bracket somewhere. Those are the worst errors to debug.


These are simple C++ compile errors.
You need to check over your code for extra brackets.

1 Like

Ok now work all very good!
thanks very very very much!!

Not to steal the Lufkin’s tread - but is it possible to use several timers (SimpleTimer) objects on a Nano board?
I tried declaring multiple timers, but using more than one crashes the board continuously.

Used a method aprox. like this:

#include <SimpleTimer.h>
SimpleTimer timer;
int t1  = 1;
int t2  = 2;
void setup(){
t1   =  timer.setInterval(5000L, tf1);
t2   =  timer.setTimeout(12000L, tf2);
void tf1(){
//do something..
void tf2(){
//do something..
void loop(){

Haven’t been able to finde an NANO example with multiple timer-objects. Has anyone tried something similar?


Can you further define “crashes”? A Nano is going to run similar to an UNO, so it is more likely your code is incorrect or something else is the issue, not just because it is a nano.

Can you paste your actual code instead of your aprox example?

I think your issue might be in how you have set yours up: e.g. tx = timer…

Here is how I have mine, running fine on an Mega, Uno and Pro Micro

#include <SimpleTimer.h>
SimpleTimer timer;

void setup(); {
  timer.setInterval(500L, AnalogSensors);  // Misc analog sensors on CodeShield
  timer.setInterval(500L, whiteLED);  // White LED on CodeShield
  timer.setInterval(1000L, thermTemp);  // Thermistor  on CodeShield
  timer.setInterval(1000L, clockDisplay);  // Time and Date Widget
  timer.setInterval(2000L, sendUptime);  // Up Time Widget
  timer.setInterval(5000L, sendTempHum);  // DHT11 sensor read

void loop()
  Blynk.run();  // Run internal Blynk functionality.
  timer.run();  // Run timers

Yes by crashes I mean reboots. Starts up, goes into setup-loop, runs the code for some seconds and reboots - over and over.
The App tell me the device was disconnected every time. Led’s and println tells me that it restarts.
Thourght it might be a matter of resources on the board.
Will paste code.

Code not complete since I never got that version with timers working.
Difference to your example is that I have several timer-objects (instances of Timer) not just one Timer (timer)…

PIN 3  = DHT
PIN 6  = Power Realy
PIN 7  = "Connection Lost"-warrning LED
PIN 13 = Work in pprogress - onboard LED
PIN A0 = Light sensor
VPIN V0 = Light
VPIN V1 = Humidity
VPIN V2 = Temeparature

Your basic bathroom ventilation controller - MMonitor humidity and light.
By light-change to over 100, trig relay after 5 min., after light-change to below 100 reset relay after 5.
If humidity is abowe 60%, trig relay until humidity is 50% - set timer for 1h - if relay stille active send pushnotification.
Send monitoring data to blynk and allow for remote manually override.

#include "DHT.h" //DHT22 sensor library
#include <SimpleTimer.h> // here is the SimpleTimer library
#include <UIPEthernet.h> //Serial Ethernet module library
#include <BlynkSimpleUIPEthernet.h> //Blynk library

SimpleTimer timer; // Create a Timer object called "timer"!
//SimpleTimer t2Light; // Create a Timer object for relay hold after light off
//SimpleTimer t1h; // Create a Timer object for 1 hour warrning

//#define BLYNK_PRINT Serial

#define DHTPIN 3     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //Delade DHT instance

const byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };    //DE:ED:BA:FE:FE:ED
//IPAddress server_ip   (192, 168,   2,  35);
//IPAddress arduino_ip  (192, 168,   2, 179);
//IPAddress dns_ip      (192, 168,   2,   1);
//IPAddress gateway_ip  (192, 168,   2,   1);
//IPAddress subnet_mask (255, 255, 255,   0);

const char auth[] = "####";  //Token from Blynk app
int hum;  //Stores humidity value
int temp; //Stores temperature value

int newLux = 0; //latest read lux
int lux = 0; //Light sensor read

int tLoop   = 1;
int t2Light = 2;
int t1h      = 3;

//const int ledPin = 13;
const byte warrningPin = 7;
const byte relayPin = 6;

void setup() //startup configurations
    //pinMode(ledPin, OUTPUT); 
    pinMode(warrningPin, OUTPUT);
    pinMode(relayPin, OUTPUT);
    dht.begin(); //start DHT22 sensor
    Serial.begin(115200); //start serial communication used for Ethernet module
    Blynk.begin(auth, IPAddress(192, 168,   2,  35), 8442, IPAddress(192, 168,   2, 179), IPAddress(192, 168,   2,   1), IPAddress(192, 168,   2,   1), IPAddress(255, 255, 255,   0), arduino_mac); //Start blynk with Token, server IP, port 
   // while(Blynk.connected() == false){
       //Serial.println("connecting to blynk");
      tLoop   =  timer.setInterval(5000L, tf);  //  Here you set interval and which function to call (time, function)
      t2Light = timer.setTimeout(120000L, relayOff); // 2-min lights timeout to fan-start
      t1h      = timer.setTimeout(3600000L, relayOff); // Timer set to 1 hour
void hum1Hour(){
  digitalWrite(relayPin, LOW);
  Blynk.notify("FAN OFF - RUNNING FOR MORE THAN 1 HOUR!!");
void relayOff(){
  digitalWrite(relayPin, LOW);
  Blynk.notify("FAN OFF - LIGHTS OUT FOR 2 MINUTS.");  

digitalWrite(warrningPin, HIGH); //LED turns off when Bllynk is conneted once again
Blynk.tweet("NANO Ventilation Connected");
void tf() //Timed functions goes here
      // digitalWrite(ledPin, LOW);
        hum = int(10.0*dht.readHumidity()); //Read humidity
        temp = int(10.0*dht.readTemperature()); //Read temperature
        newLux = analogRead(A0);
           Blynk.virtualWrite(V2, (float(temp/10.0))); //Write temperature to virtual Blynk-pin
           Blynk.virtualWrite(V1, (float(hum/10.0))); //Write humidity to virtual Blynk-pin
           Blynk.virtualWrite(V0, newLux); //Read analog pin A0 and write value to virtual blynk-pin 0
        //  digitalWrite(ledPin, HIGH);
           digitalWrite(warrningPin, LOW);
        if(lux != newLux){
          if(((100 - newLux) + lux) < 0){ // Light changed to on
          }else if(((100-newLux) + lux) > 0){ // Lights changed to off
          lux = newLux;

        if(hum/10 > 60){
          if(digitalRead(relayPin) == LOW){
             digitalWrite(relayPin, HIGH);
             Blynk.notify("VENT ON from Humidity.");
       else {
         if(digitalRead(relayPin) == HIGH){
          //Serial.println("if relay high");
            digitalWrite(relayPin, LOW);
            Blynk.notify("FAN OFF - Humidity below 50%");

void loop() //main loop
    Blynk.run(); //All the Blynk Magic starts here...
    timer.run(); //Run timer

Try this instead…

timer.setInterval(5000L, tf);  //  Here you set interval and which function to call (time, function)
timer.setTimeout(120000L, relayOff); // 2-min lights timeout to fan-start
timer.setTimeout(3600000L, relayOff); // Timer set to 1 hour

Yes I can do that and that works - however I want to be able to restart the timers and suspend them independently.

Like this?

int t1;

t1 = timer.setTimeout(120000L, relayOff);


Full doco: Arduino Playground - HomePage