There is a project with 4 buttons and a relay, everything works, but I want to add another physical button that will turn on and off all the relays at once. I tried to do it myself, and it works, but with a mistake, I lack experience or knowledge … please help me.
The button works on and off, but after the remaining buttons work from the second press.
Project on Esp12F and Arduino IDE
And another question is why if I add pin 9 to the code (ledpin5) esp constantly reboots?
#define BLYNK_PRINT Serial
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define STASSID "******"
#define STAPSK "******"
#define ledPin 2
const char* host = "esp8266-webupdate";
const char* ssid = STASSID;
const char* pass = STAPSK;
char auth[] = "*********************************";
int brightness = 150;
int fadeAmount = 2;
ESP8266WebServer server(80);
const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
const int ledPin1 = 12;
const int ledPin2 = 13;
const int ledPin3 = 14;
const int ledPin4 = 15;
//const int ledPin5 = 9;
const int btnPin1 = 5;
const int btnPin2 = 4;
const int btnPin3 = 16;
const int btnPin4 = 0;
// I add this for new button
const int btnPin5 = 10;
BlynkTimer timer;
void checkPhysicalButton();
int led1State = LOW;
int btn1State = HIGH;
int led2State = LOW;
int btn2State = HIGH;
int led3State = LOW;
int btn3State = HIGH;
int led4State = LOW;
int btn4State = HIGH;
// and this
int led5State = LOW;
int btn5State = HIGH;
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
}
BLYNK_WRITE(V0) {
led1State = param.asInt();
digitalWrite(ledPin1, led1State);
}
BLYNK_WRITE(V1) {
led2State = param.asInt();
digitalWrite(ledPin2, led2State);
}
BLYNK_WRITE(V2) {
led3State = param.asInt();
digitalWrite(ledPin3, led3State);
}
BLYNK_WRITE(V3) {
led4State = param.asInt();
digitalWrite(ledPin4, led4State);
}
void checkPhysicalButton()
{
if (digitalRead(btnPin1) == LOW) {
if (btn1State != LOW) {
led1State = !led1State;
digitalWrite(ledPin1, led1State);
Blynk.virtualWrite(V0, led1State);
}
btn1State = LOW;
} else {
btn1State = HIGH;
}
if (digitalRead(btnPin2) == LOW) {
if (btn2State != LOW) {
led2State = !led2State;
digitalWrite(ledPin2, led2State);
Blynk.virtualWrite(V1, led2State);
}
btn2State = LOW;
} else {
btn2State = HIGH;
}
if (digitalRead(btnPin3) == LOW) {
if (btn3State != LOW) {
led3State = !led3State;
digitalWrite(ledPin3, led3State);
Blynk.virtualWrite(V2, led3State);
}
btn3State = LOW;
} else {
btn3State = HIGH;
}
if (digitalRead(btnPin4) == LOW) {
if (btn4State != LOW) {
led4State = !led4State;
digitalWrite(ledPin4, led4State);
Blynk.virtualWrite(V3, led4State);
}
btn4State = LOW;
} else {
btn4State = HIGH;
}
// and this ///////////////////////
if (digitalRead(btnPin5) == LOW) {
if (btn5State != LOW) {
led5State = !led5State;
digitalWrite(ledPin1, led5State);;
digitalWrite(ledPin2, led5State);
digitalWrite(ledPin3, led5State);
digitalWrite(ledPin4, led5State);
Blynk.virtualWrite(V0, led5State);
Blynk.virtualWrite(V1, led5State);
Blynk.virtualWrite(V2, led5State);
Blynk.virtualWrite(V3, led5State);
}
btn5State = LOW;
} else {
btn5State = HIGH;
}
/////////////////////
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
Serial.println();
Serial.println("Booting Sketch...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() == WL_CONNECTED) {
MDNS.begin(host);
server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.setDebugOutput(true);
WiFiUDP::stopAll();
Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
yield();
});
server.begin();
MDNS.addService("http", "tcp", 80);
Serial.printf("Ready! Open http://%s.local in your browser or ip: \n", host);
Serial.println(WiFi.localIP());
} else {
Serial.println("WiFi Failed");
}
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(btnPin1, INPUT_PULLUP);
digitalWrite(ledPin1, led1State);
pinMode(ledPin2, OUTPUT);
pinMode(btnPin2, INPUT_PULLUP);
digitalWrite(ledPin2, led2State);
pinMode(ledPin3, OUTPUT);
pinMode(btnPin3, INPUT_PULLUP);
digitalWrite(ledPin3, led3State);
pinMode(ledPin4, OUTPUT);
pinMode(btnPin4, INPUT_PULLUP);
digitalWrite(ledPin4, led4State);
//// and this
// pinMode(ledPin5, OUTPUT);
pinMode(btnPin5, INPUT_PULLUP);
// digitalWrite(ledPin5, led5State);
/////////////////
timer.setInterval(100L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
server.handleClient();
MDNS.update();
timer.run();
analogWrite(ledPin, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 1023)
{
fadeAmount = -fadeAmount;
}
delay(1);
}