Struggling with Blynk, millis and loops

Hello Blynk gurus,

I have been trying hard to understand how to integrate Blynk to make it run a program for a defined amount of time using the millis function, when I send a “1” on a virtual pin.

The V0 pin is a push button widget and when depressed it sends the value “1”, otherwise “0”.

The program is actually for running a DC motor but for sake of simplifying the issue, I just want to have the LED turn on and stay on for 5 seconds, then turn off. At the end of this, I want it to return to waiting for the V0 pin to send another “1”.

At the moment, when the ESP connects to the wifi, the LED is off until I send the “1” after which it turns on and stays on indefinitely. Please, if you have a minute, point me to the stupidity that I am making here.

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "mywifi";

char pass[] = "password";

#define ON true

#define OFF false

byte EN = 0; //defining the enable pin on Huzzah

byte ledState = OFF; //setting the starting state

unsigned long interval = 5000; // the time we need to run

unsigned long ledOnTimer = 0; // millis() returns an unsigned long.

void turnOnLED() {     //this is the turn-on routine

  ledState = ON;

  ledOnTimer = millis();

  digitalWrite(EN, HIGH);

}

void checkLEDState() {    //this routine is the timer routine. It keeps the EN pin HIGH UNLESS the interval time has been reached.

  if (ledState == OFF) {

    digitalWrite(EN, LOW);

  } else {

    if (millis() - ledOnTimer >= interval) {

      ledState = OFF;

      digitalWrite(EN, LOW);

    }

  }

}

void setup()

{ 

  pinMode(EN, OUTPUT);  

  turnOnLED();

  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

}

BLYNK_WRITE(V0) {

int button = param.asInt(); // read button

if (button == 1) {

  checkLEDState();  //this starts the timer routine above

  }
else {
        ledState = OFF;
      digitalWrite(EN, LOW);
    }
}

void loop()

{

  Blynk.run();  

}

This was very helpful, thanks. Iended up using your latch timer example as that is exactly what i was needing. For anybody too lazy to follow the link, get this code:


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
//char server[] = "xxx.xxx.xxx.xxx";  // IP for your Local Server
char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 8080;
int Latch;
int Flag;
#define DeviceLED 2 // Built in LED for Wemos or NodeMCU v3, Or 16 for NodeMCU v1
#define LatchRelay 12 //  GPIO12 or D6

BlynkTimer timer;



void setup() {
  pinMode(DeviceLED, OUTPUT);
  pinMode(LatchRelay, OUTPUT);
  WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  Blynk.connect();
}



//===== LATCH(Relay) & LED PULSE - BLYNK Functions =====
BLYNK_WRITE(V0)  // Virtual button on V0 to activate Relay_LED pulses
{
  //Serial.println("Latch LED");
  Latch = param.asInt();
  if (Latch == 1 && Flag == 0) {
    Flag = 1;  // Keeps from allowing button press more then once while relay activated
    digitalWrite(LatchRelay, HIGH); // Activate Relay
    timer.setTimer(1000L, blinkMyLEDon, 10);  // Pulse LED routine (LedON/LedOFF) 10 times
    timer.setTimeout(10000L, RelayOFF);  // Deactivare Relay after 10 seconds
  }  // END if
}  // END Blynk Function

void RelayOFF() {
  digitalWrite(LatchRelay, LOW);
  Flag = 0;  // reset flag after relay disables
}  // END Function

void blinkMyLEDon() {
  digitalWrite(DeviceLED, LOW); // Turn ON built in LED (triggered LOW)
  timer.setTimeout(500L, blinkMyLEDoff);  // Run LED OFF routine once in 1/2 second
}  // END Function

void blinkMyLEDoff() {
  digitalWrite(DeviceLED, HIGH); // Turn OFF built in LED (triggered LOW)
}  // END Function



void loop() {
  Blynk.run();
  timer.run(); // This keeps checking the timers to see if then need to be processed
}