Timer and LED routines

Hi Guys,
I am currently working on an LED notification system with ESP8266. I am struggling with the timers : I would like to be able to activate different notification modes :
V1: Led ON for 60sec and off (this is OK)
V2 : Led blinking for 60 sec and OFF (I am strugling with combining blinking and overal duration)
I believe blinking() function run indefinitely.
Additionally to this, I would like to have a V3 where the LED is ON and then blinks only for 60sec and remains ON Is there a way to this without having to unplug the ESP8266 in order to switch the LEDs OFF?

Thanks a lot !

void blinking()
{
  timer.setInterval(2000L, []() {  // 1st Timed Lambda Function ON
    digitalWrite(LED, HIGH); // LED ON
    timer.setTimeout(1000L, []() {  // 2nd Timed Lambda Function OFF
      digitalWrite(LED, LOW); // LED OFF
    });  // END 2nd Timer Function
  });  // END 1st Timer Function
}


//  Switch ON 60seconds and 0FF (1 time)
BLYNK_WRITE(V1)
{
  int buttonState = param.asInt();
  if (buttonState == 1)  {
    digitalWrite(LED, HIGH);  // LED ON
    timer.setTimeout(60000L, []() {
      digitalWrite(LED, LOW); // LED OFF
      Blynk.virtualWrite(V1, LOW);
    } );  // LED OFF
  }
}



//   Blink for 10 seconds and OFF (Not working,blink indefinitely)
BLYNK_WRITE(V2)
{
  int buttonState = param.asInt();
  if (buttonState == 1)  {
    blinking();
    timer.setTimeout(10000L, []() {
      digitalWrite(LED, LOW); // LED OFF
      Blynk.virtualWrite(V2, LOW);
    } );  // LED OFF
  }
}

You could do two functions that call each other. Every time Checking a counter before executing and adding to the counter. The else statement could be another function that resets the counters and turns the led off. The number of cycles would set the length.

Personally, I take the easy way out and rather than wrestling with multiple lambda timers I use the Ticker library to flash an LED with the various frequencies that I’m looking for.
The Ticker library is non-blocking, so won’t impact your other code execution.

In this example I use it to flash an LED at 0.1 second frequency (on for 0.1 seconds, off for 0.1 seconds. This is the setup code which includes the library, declares the LED pin, initialises the Ticker object and sets the LED pin to output.
It also adds the flashblue function which toggles the state of the LED pin whenever it is called (which we will do later with the ticker object we’ve initialised here)…

#include <Ticker.h>       // Install from Arduino Library Manager
#define blue_led 2        // NodeMCU onboard LED attached to GPIO2 (D4) 
Ticker blueticker;        // Initialise the Ticker object called blueticker

void setup()
{
 pinMode(blue_led, OUTPUT); 
}

void flashblue() // Non-blocking ticker for Blue LED
{
  int state = digitalRead(blue_led);  // get the current state of the blue_led pin
  digitalWrite(blue_led, !state);        // set pin to the opposite state
}

To start the LED flashing at the required speed you just add this line of code…

blueticker.attach(0.1, flashblue);  // start blueticker with 0.1 second flash rate

When you want to stop the LED from flashing you just detach the ticker object. At this point the LED might be on or off (50% chance of either), so we want to force it to a particular state - in this case off (HIGH)…

blueticker.detach();  // stop the blueticker
digitalWrite(blue_led, HIGH);  // LED is active LOW, so force it to be off

If you wanted the LED to flash at 0.1 second intervals for 60 seconds then you could use a combination of your existing lambda timer and the ticker code, attaching the tickler at the start of the 60 second timer and detaching it (and setting your LED to the required state) at the end of the timer.

Pete.

1 Like

Hello Pet! me again! As you will realize, I am very new to this, you have already helped me on other occasions! I want to make the internal led of my wemos blink and I can’t make it work with the code you published … what could I be wrong about … ??

#include <FS.h>    
//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> 
#include <ArduinoJson.h>
//for LED status
#include <Ticker.h>
#define blue_led 2        // NodeMCU onboard LED attached to GPIO2 (D4) 
Ticker blueticker;        // Initialise the Ticker object called blueticker
Ticker ticker;

bool alarm_mode = false ;
bool verificador = false;
//int led_placa = 2;
int sensor = 5;
int buzzer = 15;
int alarm_led = 4;
//int alarm_led_off = 0;
int indicador = 13;
int ledPin = 14;
int contconexion = 0;
int conteoReactivacion = 0;
int ledState = LOW;             // ledState used to set the LED
int VirtualPinA = 3;
    
//define your default values here, if there are different values in config.json, they are overwritten.
char mqtt_server[40];
char mqtt_port[6] = "8080";
char blynk_token[34] = "ymzp8hEq9d4UmNguWdvF7PhDj8nVUTlc"; 

//flag for saving data
bool shouldSaveConfig = false;

//callback notifying us of the need to save config

void flashblue() // Non-blocking ticker for Blue LED
{
  int state = digitalRead(blue_led);  // get the current state of the blue_led pin
  digitalWrite(blue_led, !state);        // set pin to the opposite state
  blueticker.attach(0.2, flashblue);  // start blueticker with 0.1 second flash rate
  blueticker.detach();  // stop the blueticker
digitalWrite(blue_led, LOW);  // LED is active LOW, so force it to be off
}
void tick()
{
  //toggle state
  int state = digitalRead(indicador);  // get the current state of GPIO1 pin
  digitalWrite(indicador, !state);     // set pin to the opposite state
}

void ledState1()
{
   if (ledState == LOW) {
      ledState = HIGH;
      Blynk.virtualWrite(VirtualPinA, 0);
     
    } else {
      ledState = LOW;
      Blynk.virtualWrite(VirtualPinA, 1023);
     
    }
  }

//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
  Serial.println("Entered config mode");
  Serial.println(WiFi.softAPIP());
  //if you used auto generated SSID, print it
  Serial.println(myWiFiManager->getConfigPortalSSID());
  //entered config mode, make led toggle faster
  ticker.attach(0.2, tick);
}
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
  }
// Keep this flag not to re-sync on every reconnection
bool isFirstConnect = true;
// This function will run every time Blynk connection is established
BLYNK_CONNECTED() 
{
  if (isFirstConnect) 
  {
    // Request Blynk server to re-send latest values for all pins
    Blynk.syncAll();
    isFirstConnect = false;
  }
}

  BlynkTimer timer;
void myTimerEvent() {
//Serial.println("Tiempo des en seg: " + String(conteoReactivacion));
  if (alarm_mode == false) {
    conteoReactivacion++;
    if (conteoReactivacion >= 1800) { 
      Blynk.virtualWrite(V1, 1);
      digitalWrite(alarm_led, HIGH);
      //digitalWrite(alarm_led_off, LOW);
      alarm_mode = true;
    }
  } else {
    conteoReactivacion = 0;
  }
  if (digitalRead(sensor) == 0) {
    Blynk.virtualWrite(V0, "NORMAL");
    delay(3000);   // Tiempo de demora al recibir señal de disparo
  }
  if (digitalRead(sensor) == 1)

  {
    Blynk.virtualWrite(V0, "ALERTA");
    
    if (alarm_mode == true)
      //AUTOR ALEXIS MORA
    {
      Blynk.email("clianaloss01@gmail.com", "Historial de Alarmas", "Alarma Disparo");
      Blynk.notify("Alarma Disparo");
      digitalWrite(buzzer, HIGH);
      delay(500);
      digitalWrite(buzzer, LOW);
    }                                                                                  
  }
}
BLYNK_WRITE(V1) {
  Serial.println(param.asInt());
  switch (param.asInt()) {
    case 1: {
        alarm_mode = true;
        digitalWrite(alarm_led, HIGH);
        //digitalWrite(alarm_led_off, LOW);
        break;
      }
    case 2: {
        alarm_mode = false;
        digitalWrite(buzzer, LOW);
        digitalWrite(alarm_led, LOW);
        //digitalWrite(alarm_led_off, HIGH);
        break;
    }    
  }
}

void setup()
{
  pinMode(sensor, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  pinMode(alarm_led, OUTPUT);
  pinMode(blue_led, OUTPUT); 
  
  //pinMode(alarm_led_off, OUTPUT);
  pinMode(indicador, OUTPUT);
//pinMode(led_placa, OUTPUT);
//pinMode(BUILTIN_LED, OUTPUT);
//start ticker with 0.5 because we start in AP mode and try to connect
  ticker.attach(0.6, tick);
 
  // Debug console
  Serial.begin(115200);
   Serial.println();
   
  //clean FS, for testing
  //SPIFFS.format();
 // Blynk.begin(auth, ssid, pass);
  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, ledState1); //timer will run every sec 

  // put your setup code here, to run once:
  
  //clean FS, for testing
  //SPIFFS.format();

  //read configuration from FS json
  Serial.println("mounting FS...");

  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");

          //strcpy(mqtt_server, json["mqtt_server"]);
         //strcpy(mqtt_port, json["mqtt_port"]);
          strcpy(blynk_token, json["blynk_token"]);

        } else {
          Serial.println("failed to load json config");
        }
        configFile.close();
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
  //end read

  // The extra parameters to be configured (can be either global or just in the setup)
  // After connecting, parameter.getValue() will get you the configured value
  // id/name placeholder/prompt default length
  //WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  //WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34);

  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  //set static ip
  //wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
  
  //add all your parameters here
  //wifiManager.addParameter(&custom_mqtt_server);
  //wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_blynk_token);

  //reset settings - for testing
  //wifiManager.resetSettings(); //borra usuario y contraseña cuando esta linea esta sin comentar al grabar.
  //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
  wifiManager.setAPCallback(configModeCallback);
  //set minimu quality of signal so it ignores AP's under that quality
   //  defaults to 8%
  wifiManager.setMinimumSignalQuality( 20 );
  
  //sets timeout until configuration portal gets turned off
  //useful to make it all retry or go to sleep
  //in seconds
  wifiManager.setTimeout(120);

  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect("NanoWifi", "12345678")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");

  //read updated parameters
  //strcpy(mqtt_server, custom_mqtt_server.getValue());
  //strcpy(mqtt_port, custom_mqtt_port.getValue());
  strcpy(blynk_token, custom_blynk_token.getValue());

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    //json["mqtt_server"] = mqtt_server;
   // json["mqtt_port"] = mqtt_port;
    json["blynk_token"] = blynk_token;

    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }

    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
    //end save
  }

 while (WiFi.status() != WL_CONNECTED) {
       int mytimeout = 5;
       delay(500);
       Serial.print(".");
       if((millis() / 1000) > mytimeout ){ // try for less than 6 seconds to connect to WiFi router
         Serial.println(" ");
          break;
          }
   }

  if(WiFi.status() == WL_CONNECTED){  
    //if you get here you have connected to the WiFi
   Serial.println(" ");
   Serial.println("connected to WiFi!! yay :)");
   Serial.println("IP address: ");
   Serial.println(WiFi.localIP()); 
  ticker.detach();
  //keep LED on
  digitalWrite(indicador, HIGH);
}
 
  Blynk.config(blynk_token);
  bool result = Blynk.connect();

  if (result != true)
  {
  Serial.println("BLYNK Connection Fail");
  Serial.println(blynk_token);
  wifiManager.resetSettings();  
  ESP.reset();
  delay (1000);
  }
    }
  
void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer  
}

Well, you’ve totally butchered the flashblue() function…

and you’re never actually calling the blueticker.attach(0.2, flashblue); command, except where you’ve added it inside its own function.

Pete.

Yes pet was a mess but I don’t know where I have to locate this line of code in my sketch blueticker.attach(0.2, flashblue);

Maybe try Pete or Peter. Pet generally is a favorite animal :grinning:

So sorry, the translator played me wrong

1 Like

At the point when you want the LED to start flashing.
If that’s immediately after startup then in your void setup.

Pete.

perfect ! It worked!! but sometime you recommended me to leave the loop clean because there may be problems with Blynk … and in this way I am adding blueticker.attach (0.2, flashblue); to the loop

Maybe you need new glasses!

Pete.

I might need new glasses haha ​​but I was saying it for this … http: //help.blynk.cc/en/articles/2091699-keep-your-void-loop-clean

Yes, but if you actually read what I wrote carefully, you’ll see that I did not tell you to put anything in your void loop, only in your void setup.

Pete.

1 Like