Timer for 30 minutes

Hi Everyone I still learning Blynk and coding skill. I try searching the similar issue thread through Blynk Forum and Google, I did not find the solution yet, I had use many method but it nothing happen, please kindly help to identify the issue.

About My Project:
My project is about Development of Automatic Shoe Rack with Ultraviolet (UV) and Dryer Apparatus. For open and close the rack just press the button from blynk and it open and close the rack.After that next choose button for Auto or Manual. For example i choose Auto the choose what type of choose. When press fabric then heater will on 30 minutes with time had been set and turn off go for the next UV Light for 10 minutes. After that All will set off.

Problem:

The problem is when i set delay below than 1 minutes it work well then if i set more than 1 minutes ,it always repeat (after finish dry go to UV light then after UV light it always go to heater again).What i want is to make it not repeat.
I had set button for (fabric) in blynk push not switch.But it not turn off when i look at blynk it in low condition.

What i had done:

i had use time interval but it not go to my direction that had been set

void Autol()
{
    if(button2==1&&selectmode==1)
    {
    digitalWrite(4,LOW);//Heater On  
    digitalWrite(2,HIGH);//UV Light  Off  
    timer.setInterval(60000, reconnectBlynk);
   
    }
 else
 {
  digitalWrite(2,HIGH);//UV Light OFF
  digitalWrite(4,HIGH);//Heater OFF
  }
}

void reconnectBlynk()
{
  digitalWrite(2,LOW);//UV Light On  
  digitalWrite(4,HIGH);//Heater off Off
  timer.setInterval(30000, Auto);
}

Hardware:

  1. NodeMcu Amica
  2. Heater
  3. UV Light
  4. 5V Relay

Blynk Library: 0.6.1
Blynk App Version: V2.27.3
** NODEMCU Lua IoT I2C ESP8266 Wifi Controller Board ESP-12 CP2102**

Blynk Widget:

  1. Button

Wire Connection:
D0: 5V Relay(open linear actual motor)
D1: 5V Relay(close linear actual moto)
D2: 5V Relay(Heater)
D4: 5V Relay(UV Light)

Skecth


#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>                                         
#include <SPI.h>
#include <DHT.h>

char auth[] = "0d2e2960f823481f8f2940a30b1a4bdc";
char ssid[] = "nikzhafran";
char pass[] = "678250nik";


int timer2; //hidup motor Blynk
int timer3; //mati motor Blynk
int timer4; //hidup motor Blynk
int timer5; //mati motor Blynk
int timer6; //hidup motor Blynk
int timer7; //mati motor Blynk
int button2;

int selectmode; //Select Mode for Auto or Manual
int openmotor; //open the motor
int closemotor; //close the motor

SimpleTimer timer;

void setup()
{
  Serial.begin(115200); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);
  pinMode(16,OUTPUT);  //output for open motor
  pinMode(5,OUTPUT);   //output for close motor
  pinMode(4,OUTPUT);   //output for heater
  pinMode(2,OUTPUT);   //output UV LIGHT
  digitalWrite(16, HIGH );
  digitalWrite(5, HIGH );
  digitalWrite(4, HIGH );
  digitalWrite(2, HIGH );
  }

void openmotor1() //open linear actual motor
{
   if(openmotor==1)
  {
    digitalWrite(16, LOW );//Motor 16 open
}
}

void openmotor2()//open linear actual motor
{
   if(openmotor==0)
  {
    digitalWrite(16, HIGH );//Motor 16 close
}
}
void closemotor1() //close linear actual motor
{
   if(closemotor==1)
  {
    digitalWrite(5, LOW ); //Motor 5 open
}
}

void closemotor2() //close linear actual motor
{
   if(closemotor==0)
  {
    digitalWrite(5, HIGH );//Motor 5 close
}
}
void Auto()
{
    if(button2==1&&selectmode==1)
    {
    digitalWrite(4,LOW);//Heater On
    digitalWrite(2,HIGH);//UV Light OFF
    delay(200);  //i had put 60000 but it always repeat
    digitalWrite(2,LOW);//UV Light ON 
    digitalWrite(4,HIGH);//Heater OFF
    delay(200); //i had put 60000 but it always repeat
    }
 else if (button2==0&&selectmode==1)
 {
  digitalWrite(2,HIGH);//UV Light OFF 
  digitalWrite(4,HIGH);//Heater OFF 
  }
}



void Manual()
{
digitalWrite(4,HIGH);//not complete yet
}
 //..........................................................................................................................................
  



void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}
BLYNK_WRITE(V4)
{
 openmotor = param.asInt(); // Get the state of the Button for opeen motor
 if(openmotor ==1){
timer.disable(timer3); // Disables the timer3
timer2=timer.setInterval(1000L, openmotor1);   // Setup a function to be called every 1000 ms
 }
 else
{
timer.disable(timer2); //Disables the timer2
  timer3=timer.setInterval(1000L, openmotor2); // Setup a function to be called every 1000 ms
 }
}
BLYNK_WRITE(V5)
{
 closemotor= param.asInt(); // Get the state of the Button for close motor
 if(closemotor ==1){
timer.disable(timer5); // Disables the timer5
timer4=timer.setInterval(1000L, closemotor1);   // Setup a function to be called every 1000 ms
 }
 else
{
timer.disable(timer4); //Disables the timer4
  timer5=timer.setInterval(1000L, closemotor2); // Setup a function to be called every 1000 ms
 }
}
BLYNK_WRITE(V6) //Select Mode
{
 selectmode = param.asInt(); // Get the state of the Button for selectmode
 if(selectmode ==1){
timer.disable(timer7); // Disables the timer7
timer6=timer.setInterval(1000L, Auto);   // Setup a function to be called every 1000 ms
 }
 else
{
timer.disable(timer6); //Disables the timer6
  timer7=timer.setInterval(1000L, Manual); // Setup a function to be called every 1000 ms
 }
}
BLYNK_WRITE(V7) // Case 1 ON OFF fabric 
{
 button2 = param.asInt(); // Get the state of the VButton for Case 1 ONOFF
}
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V4); //Open Motor
  Blynk.syncVirtual(V5); //Close Motor
  Blynk.syncVirtual(V6); //Select Mode
  Blynk.syncVirtual(V7); //Fabric button
}

Instead of only setInterval timers, which basically loop forever or until disabled, and thus can be tricky to manage for non-constantly-repeating tasks, look into setTimeout timers which do something once after x time passes, or setTimer timers which do something once after x time passes, but then repeat the process y amount of times .

https://playground.arduino.cc/Code/SimpleTimer/

The right timer for the job.

2 Likes

To build on @Gunner’s point …

With the one minute timer, you’re capturing the timer IDs (e.g., timer2, timer3, …) and you’re disabling one timer (timer3) before defining (and enabling) a new timer (timer2). That’s why the one minute timers don’t repeat.

BLYNK_WRITE(V4) {
   openmotor = param.asInt(); // Get the state of the Button for opeen motor
   if (openmotor == 1) {
      timer.disable(timer3); // Disables the timer3
      timer2 = timer.setInterval(1000L, openmotor1);
    } else {
      timer.disable(timer2); //Disables the timer2
      timer3 = timer.setInterval(1000L, openmotor2);
   }
}

However, note … when you disable a “setInterval” timer, the timer is not deleted. In other words, each time you invoke,

timer2 = timer.setInterval(1000L, openmotor1);
...

you’re “orphaning” the old timer2 and you’re creating a new timer2. You essentially have a “timer leak”. You’ll eventually run out of timer slots.

Take @Gunner’s advice. Look at the “setTimeout” and “setTimer” timers. These timers are automatically deleted once they expire.

1 Like

sorry i late because i just think what you mean and i understand
thank you very much

    timer.setTimeout(1000L, closemotor);  // Deactivare Relay after 2 seconds

After closemotor has been called, the interval is deleted and value timerId is no longer valid.

    timer.setTimer(60000L, reconnectBlynk,1);  // Heater  (ON/OFF) 1 times

This same case as set timeout but we can make how many time we want right

then can i just put 1 800 000L for 30 minutes
or make

timer.setInterval(30x(1000*60), reconnectBlynk);

Thank you very much i understand and when i test it work well
i use too many timer “setInterval”

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>                                         
#include <SPI.h>
#include <DHT.h>

char auth[] = "0d2e2960f823481f8f2940a30b1a4bdc";
char ssid[] = "nikzhafran";
char pass[] = "678250nik";


int timer2; //hidup motor Blynk
int timer3; //mati motor Blynk
int timer4; //hidup motor Blynk
int timer5; //mati motor Blynk
int timer6; //hidup motor Blynk
int timer7; //mati motor Blynk
int T2; //hidup motor Blynk
int T1; //hidup motor Blynk
int button2;

int selectmode; //Select Mode for Auto or Manual
int openmotor; //open the motor
int closemotor; //close the motor

SimpleTimer timer;

void setup()
{
  Serial.begin(115200); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);
  pinMode(16,OUTPUT);  //output for open motor
  pinMode(5,OUTPUT);   //output for close motor
  pinMode(4,OUTPUT);   //output for heater
  pinMode(2,OUTPUT);   //output UV LIGHT
  digitalWrite(16, HIGH );
  digitalWrite(5, HIGH );
  digitalWrite(4, HIGH );
  digitalWrite(2, HIGH );
  }

void openmotor1() //open linear actual motor
{
   if(openmotor==1)
  {
    digitalWrite(16, LOW );//Motor 16 open
}
}

void openmotor2()//open linear actual motor
{
   if(openmotor==0)
  {
    digitalWrite(16, HIGH );//Motor 16 close
}
}
void closemotor1() //close linear actual motor
{
   if(closemotor==1)
  {
    digitalWrite(5, LOW ); //Motor 5 open
}
}

void closemotor2() //close linear actual motor
{
   if(closemotor==0)
  {
    digitalWrite(5, HIGH );//Motor 5 close
}
}
void Auto()
{
    if(button2==1&&selectmode==1)
    {
    digitalWrite(4,LOW);//Heater On
    digitalWrite(2,HIGH);//UV Light OFF
    timer.setTimer(60000L, reconnectBlynk,1);
    }
 else if (button2==0&&selectmode==1)
 {
  digitalWrite(2,HIGH);//UV Light OFF 
  digitalWrite(4,HIGH);//Heater OFF 
  }
}

void reconnectBlynk()
{
  digitalWrite(2,LOW);//UV Light ON 
  digitalWrite(4,HIGH);//Heater OFF
   timer.setTimer(30000L, tutup,1);
}
void tutup()
{
  digitalWrite(2,HIGH);//UV Light ON 
  digitalWrite(4,HIGH);//Heater OFF
}


void Manual()
{
digitalWrite(4,HIGH);//not complete yet
}
 //..........................................................................................................................................
  



void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}
BLYNK_WRITE(V4)
{
 openmotor = param.asInt(); // Get the state of the Button for opeen motor
 if(openmotor ==1){
timer.disable(timer3); // Disables the timer3
timer2=timer.setTimeout(1000L, openmotor1);   // Setup a function to be called every 1000 ms
 }
 else
{
timer.disable(timer2); //Disables the timer2
  timer3=timer.setTimeout(1000L, openmotor2); // Setup a function to be called every 1000 ms
 }
}
BLYNK_WRITE(V5)
{
 closemotor= param.asInt(); // Get the state of the Button for close motor
 if(closemotor ==1){
timer.disable(timer5); // Disables the timer5
timer4=timer.setTimeout(1000L, closemotor1);   // Setup a function to be called every 1000 ms
 }
 else
{
timer.disable(timer4); //Disables the timer4
  timer5=timer.setTimeout(1000L, closemotor2); // Setup a function to be called every 1000 ms
 }
}
BLYNK_WRITE(V6) //Select Mode
{
 selectmode = param.asInt(); // Get the state of the Button for selectmode
 if(selectmode ==1){
timer.disable(timer7); // Disables the timer7
timer6=timer.setTimeout(1000L, Auto);   // Setup a function to be called every 1000 ms
 }
 else
{
timer.disable(timer6); //Disables the timer6
  timer7=timer.setTimeout(1000L, Manual); // Setup a function to be called every 1000 ms
 }
}
BLYNK_WRITE(V7) // Case 1 ON OFF fabric 
{
 button2 = param.asInt(); // Get the state of the VButton for Case 1 ONOFF
}
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V4); //Open Motor
  Blynk.syncVirtual(V5); //Close Motor
  Blynk.syncVirtual(V6); //Select Mode
  Blynk.syncVirtual(V7); //Fabric button
}