As I said in an earlier post I’m a noob.
I’m using a NodeMCU with Blynk on a Android phone.
I’m setting up a virtual button and trying to also use a Virtual LED to show when the button is in a high state.
The problem I’m having is the virtual LED only gets a quarter line on the outside. I does not fill in. It’s like it’s only reading a partial state between low and high. I’m sure it’s in my coding; but I can’t find the issue. Please forgive me if I didn’t format my code right.
#define BLYNK_PRINT Serial
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define N_DIMMERS 3
#define led_pin D0
int dimmer_pin[] = {14, 5, 15};
char auth[] = "**";
char ssid[] = "**";
char pass[] = "**";
char host[] = "ESP-OTA";
// Select your pin with physical button
const int btnPin = D2;
int btnState = LOW;
WidgetLED led3(V20);
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 ();
}
}
void updateButtonWidgetIfNeeded() {
int newBtnState = digitalRead(btnPin);
if (btnState != newBtnState) {
btnState = newBtnState;
Blynk.virtualWrite(V20, btnState);
}
}
BLYNK_WRITE(V21) {
digitalWrite(btnPin, param.asInt());
updateButtonWidgetIfNeeded();
}
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);
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW);
pinMode(btnPin, OUTPUT);
timer.setInterval(1000L, updateButtonWidgetIfNeeded);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
Blynk.begin(auth, ssid, pass);
// WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Blynk.begin(auth, ssid, pass);
// WiFi.begin(ssid, password);
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.
}
void loop()
{
Blynk.run();
ArduinoOTA.handle();
timer.run();
}