Good morning all,
I’m trying to operate a start relay that needs to stay engaged for around 10 seconds.
I’m using NodeMCU board with Blynk.
I’m using V20 to start.(which controls D3) and V21 to stop (which controls D4).
In Blynk I have V20 as push, not switch; so that when I get the timer delay it will only activate the start relay for the 10 second delay.
My problem is if I put in a delay, V20 doesn’t work at all. I read that I should use a blynk.timer command: but I’m not understanding the syntax and where to put it…
Could some one please explain the syntax of the command and tell me where to put it in my sketch?
Thank you in advance.
#define BLYNK_PRINT Serial
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// #include <LiquidCrystal.h>
// #include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define N_DIMMERS 3
#define led_pin D0
int dimmer_pin[] = {14, 5, 15};
char auth[] = "**";
char ssid[] = "**";
char pass[] = "**";
char host[] = "**";
// Select your pin with physical button
const int btnPin = D3;
const int Stop = D4;
WidgetLED led3(V3);
WidgetLED led4(V4);
BlynkTimer timer;
BLYNK_CONNECTED() {
Blynk.syncAll();
}
void LedWidget() {
if (digitalRead(btnPin) == HIGH) { // Sets LED widget ON or OFF depending on state of btnpin
led3.on();
} else {
led3.off ();
}
if (digitalRead(Stop) == HIGH) { // Sets LED widget ON or OFF depending on state of Stop
led4.on();
} else {
led4.off ();
}
}
void ButtonWidget() {
if (digitalRead(btnPin) == HIGH) { // Sets LED widget ON or OFF depending on state of btnpin
Blynk.virtualWrite(V20, HIGH);
} else {
Blynk.virtualWrite(V20, LOW);
}
if (digitalRead(Stop) == HIGH) { // Sets LED widget ON or OFF depending on state of Stop
Blynk.virtualWrite(V21, HIGH);
} else {
Blynk.virtualWrite(V21, LOW);
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW);
pinMode(btnPin, OUTPUT);
pinMode(Stop, OUTPUT);
timer.setInterval(100L, LedWidget);
timer.setInterval(100L, ButtonWidget);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
Blynk.begin(auth, ssid, pass);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Blynk.begin(auth, ssid, pass);
Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
Serial.println("Retrying connection...");
}
/* switch off led */
digitalWrite(led_pin, HIGH);
/* configure dimmers, and OTA server events */
analogWriteRange(1000);
analogWrite(led_pin, 990);
for (int i = 0; i < N_DIMMERS; i++) {
pinMode(dimmer_pin[i], OUTPUT);
analogWrite(dimmer_pin[i], 50);
}
ArduinoOTA.setHostname(host);
ArduinoOTA.onStart([]() { // switch off all the PWMs during upgrade
for (int i = 0; i < N_DIMMERS; i++) {
analogWrite(dimmer_pin[i], 0);
}
analogWrite(led_pin, 0);
});
ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end
for (int i = 0; i < 30; i++) {
analogWrite(led_pin, (i * 100) % 1001);
delay(50);
}
});
ArduinoOTA.onError([](ota_error_t error) {
(void)error;
ESP.restart();
});
/* setup the OTA server */
ArduinoOTA.begin();
Serial.println("Ready");
}
BLYNK_WRITE(V20) {
digitalWrite(btnPin, param.asInt()); // Sets btnPin HIGH or LOW depending on state of Button Widget.
}
BLYNK_WRITE(V21) {
digitalWrite(Stop, param.asInt()); // Sets Stop HIGH or LOW depending on state of Button Widget.
}
BLYNK_WRITE(V22) {
digitalWrite(btnPin, param.asInt()); // Sets btnPin HIGH or LOW depending on state of Button Widget.
}
BLYNK_WRITE(V23) {
digitalWrite(btnPin, param.asInt()); // Sets btnPin HIGH or LOW depending on state of Button Widget.
}
BLYNK_WRITE(V24) {
digitalWrite(btnPin, param.asInt()); // Sets btnPin HIGH or LOW depending on state of Button Widget.
}
void loop()
{
Blynk.run();
ArduinoOTA.handle();
timer.run();
}