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();
}