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

[SOLVED] Reset interval SimpleTimer


#1

Hello Blynkers,

I have difficulties to reset interval of timers using timer.deleteTimer function. It is partially linked with the this topic
I want to achieve something similar.
Complete code below, involved section is “BRUMISATION”

//**********DEFINE**********//
#define BLYNK_PRINT Serial
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15

//**********LIB**********//
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <TimeLib.h>

BlynkTimer timer;
Adafruit_BME280 bme; // I2C


//**********TOKEN (OK)**********//
char auth[] = "";

//**********WIFI (OK)**********//
char ssid[] = "xx";
char pass[] = "xx";

//**********HEAT BUTTON PIN**********//
const int heatrelayPin = D3;
void HeatButton();
int heatrelayState = HIGH;

//**********WATER BUTTON PIN**********//
const int waterrelayPin = D7;
void WaterButton();
int waterrelayState = HIGH;


//**********LIGHT BUTTON PIN**********//
const int lightrelayPin = D6;
void LightButton();
int lightrelayState = HIGH;


BLYNK_CONNECTED() {
  Blynk.syncAll();
}

//**********BME TO BLYNK (OK)**********//
void sendSensor()
{
  float h = bme.readHumidity();
  float t = bme.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from BME sensor!");
    return;
  }
  Blynk.virtualWrite(V18, h);
  Blynk.virtualWrite(V17, t);
}

//**********BUTTONS**********//
BLYNK_WRITE(V20) {
  heatrelayState = param.asInt();
  digitalWrite( heatrelayPin,heatrelayState );
}
BLYNK_WRITE(V22) {
  waterrelayState = param.asInt();
  digitalWrite( waterrelayPin,waterrelayState );
}
BLYNK_WRITE(V24) {
  lightrelayState = param.asInt();
  digitalWrite( lightrelayPin,lightrelayState );
}

//*********BRUMISATION***********//
int freq;
void brumistart(){
  Blynk.virtualWrite(V22,0);
  //digitalWrite( waterrelayPin,waterrelayState );
}
void brumistop(){
  Blynk.virtualWrite(V22,1);
}

BLYNK_WRITE(V0){
  int freq = param.asInt();
  int timerV0START1;
  int timerV0START2;
  int timerV0STOP1;
  int timerV0STOP2;
  if (freq != 0) {
    timer.deleteTimer(timerV0START1);
    timer.deleteTimer(timerV0STOP1);
    timer.deleteTimer(timerV0START2);
    timer.deleteTimer(timerV0STOP2);
    timerV0START1 = timer.setInterval(freq*60000L, brumistart);
    timerV0STOP1 = timer.setInterval(freq*60000L+4000L, brumistop);
    timerV0START2 = timer.setInterval(freq*60000L+4000L+6000L, brumistart);
    timerV0STOP2 = timer.setInterval(freq*60000L+4000L+6000L+4000L, brumistop);
  } else {
    timer.deleteTimer(timerV0START1);
    timer.deleteTimer(timerV0STOP1);
    timer.deleteTimer(timerV0START2);
    timer.deleteTimer(timerV0STOP2);
  }
}
void setup()
{
  //**********DEBUG CONSOLE**********//
  Serial.begin(115200);

  //**********ESP SETUP (OK)**********//
  Blynk.begin(auth, ssid, pass);
  delay(200);

  //**********BME SENSOR (OK)**********//
  bme.begin();
  delay(100);
  timer.setInterval(1000L, sendSensor);

  //**********SWITCH HEAT ON/OFF**********//
  pinMode(heatrelayPin, OUTPUT);

  //**********SWITCH WATER ON/OFF**********//
  pinMode(waterrelayPin, OUTPUT);
  
  //**********SWITCH LIGHT ON/OFF**********//
  pinMode(lightrelayPin, OUTPUT);
}

void loop()
{
  Blynk.run();
  timer.run();
}

V0 is a numeric input or a step button (I tried both), It works well the first time I trigger V0, but after, the interval stays the same. It looks like the interval is not deleted and recreated like it should (or like I want :slight_smile: )

Any ideas why ?


#2

@kaosss

After doing

timerV0START1 = timer.setInterval(freq*60000L, brumistart);

“timerV0START1” holds the number of the timer that you need to later do this

timer.deleteTimer(timerV0START1);

But like this:

BLYNK_WRITE(V0){
  int freq = param.asInt();
  int timerV0START1;
  int timerV0START2;
  int timerV0STOP1;
  int timerV0STOP2;
  [...]
}

the four variables that should hold timer numbers that you need as a reference are reinitialized.
BLYNK_WRITE(V0) is a function. Vars defined inside only “live” inside.

I.e. the information is gone on each call of BLYNK_WRITE(V0) and

timer.deleteTimer(timerV0START1);

does not work.

I’d suggest to put this at the top of the code to define them globally

//**********GLOBALS**********//
int timerV0START1;
int timerV0START2;
int timerV0STOP1;
int timerV0STOP2;

… and to modify BLYNK_WRITE(V0) like this

BLYNK_WRITE(V0){
  int freq = param.asInt();
  if (freq != 0) {
    timer.deleteTimer(timerV0START1);
    timer.deleteTimer(timerV0STOP1);
    timer.deleteTimer(timerV0START2);
    timer.deleteTimer(timerV0STOP2);
    timerV0START1 = timer.setInterval(freq*60000L, brumistart);
    timerV0STOP1 = timer.setInterval(freq*60000L+4000L, brumistop);
    timerV0START2 = timer.setInterval(freq*60000L+4000L+6000L, brumistart);
    timerV0STOP2 = timer.setInterval(freq*60000L+4000L+6000L+4000L, brumistop);
  } else
    timer.deleteTimer(timerV0START1);
    timer.deleteTimer(timerV0STOP1);
    timer.deleteTimer(timerV0START2);
    timer.deleteTimer(timerV0STOP2);
  }
}

Happy testing :wink:


#3

@IBK Thanks ! Work like a charm ! I changed a bit the code, but define vars outside the function was the solution.


#4

Here is another way…