BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Add one button in the project with Reley from this forum

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

I’ve just taken a fairly quick look at this and I think I can see the problem, although I’ve not done any testing to confirm this.

Your original code used 4 variables to track the current state of the relays. These are somewhat confusingly called:

led1State
led2State
led3State
led4State

presumably this is because LEDs were used in the original circuit for testing and later replaced with relays.

These 4 variables keep track of the state of the relays and are used to toggle the state by using the exclamation mark ! NOT operator.

The problem is that your new code does not update the status of these variables, so when you press the ‘master’ button the part of the code that tracks the sate of each relay doesn’t know that the relays have changed state. So, when you press a individual relay button the next time, it changes it to it’s opposite state, but because the relay was already in this state nothing happens.

The solution is to add some additional lines of code into you new section that update the status of the ledxState variable to their correct values.

led1State = led5State;
led2State = led5State;
led3State = led5State;
led4State = led5State;

is the simple way to do this, although the led5State variable is slightly confusing in itself as it isn’t really being used to track the value of one single relay.

Pete.

Thanks for the quick and helpful answer of Pete. It works, but still it’s not as easy as I thought. I still need to think about what I want. :smile:
And here is the code that I changed a little from the topic Turn on/off 4 lights using 4 physical buttons and blynk :


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define STASSID "******"
#define STAPSK  "******"

#define ledPin 2 //fade led
int brightness = 0;//fade led
int fadeAmount = 1;//fade led

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "******";

// Your WiFi credentials.
// Set password to "" for open networks.

const char* ssid = STASSID;
const char* pass = STAPSK;


// Set your LED and physical button pins here
const int ledPin1 = 12;
const int ledPin2 = 13;
const int ledPin3 = 14;
const int ledPin4 = 15;
const int btnPin1 = 5;
const int btnPin2 = 4;
const int btnPin3 = 16;
const int btnPin4 = 0;
const int btnPin5 = 10; //////////////// ADDED

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;

int led5State = LOW; //////////////// ADDED
int btn5State = HIGH; //////////////// ADDED

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V0);
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V12, led1State);
  //Blynk.virtualWrite(V13, led2State);
  //Blynk.virtualWrite(V14, led3State);
  //Blynk.virtualWrite(V15, led4State);

}

// When App button is pushed - switch the state
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) {
    // btn1State is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      led1State = !led1State;
      digitalWrite(ledPin1, led1State);

      // Update Button Widget
      Blynk.virtualWrite(V0, led1State);
    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
  }

  if (digitalRead(btnPin2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      led2State = !led2State;
      digitalWrite(ledPin2, led2State);

      // Update Button Widget
      Blynk.virtualWrite(V1, led2State);
    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;
  }

  if (digitalRead(btnPin3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      led3State = !led3State;
      digitalWrite(ledPin3, led3State);

      // Update Button Widget
      Blynk.virtualWrite(V2, led3State);
    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btnPin4) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn4State != LOW) {

      // Toggle LED state
      led4State = !led4State;
      digitalWrite(ledPin4, led4State);

      // Update Button Widget
      Blynk.virtualWrite(V3, led4State);
    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
  if (digitalRead(btnPin5) == LOW) { //////////////// ADDED
    // btnState is used to avoid sequential toggles //////////////// ADDED
    if (btn5State != LOW) { //////////////// ADDED

      // Toggle LED state //////////////// ADDED
            led5State = !led5State; //////////////// ADDED
      led1State = led5State; //////////////// ADDED
      led2State = led5State; //////////////// ADDED
      led3State = led5State; //////////////// ADDED
      led4State = led5State; //////////////// ADDED
      digitalWrite(ledPin1, led5State); //////////////// ADDED
      digitalWrite(ledPin2, led5State); //////////////// ADDED
      digitalWrite(ledPin3, led5State); //////////////// ADDED
      digitalWrite(ledPin4, led5State); //////////////// ADDED

      // Update Button Widget //////////////// ADDED
      Blynk.virtualWrite(V0, led5State); //////////////// ADDED
      Blynk.virtualWrite(V1, led5State); //////////////// ADDED
      Blynk.virtualWrite(V2, led5State); //////////////// ADDED
      Blynk.virtualWrite(V3, led5State); //////////////// ADDED
    } //////////////// ADDED
    btn5State = LOW; //////////////// ADDED
  } else { //////////////// ADDED
    btn5State = HIGH; //////////////// ADDED
  } //////////////// ADDED
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  Serial.println();

  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

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


  pinMode(btnPin5, INPUT_PULLUP); //////////////// ADDED


  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
}

void loop()
{
  Blynk.run();
  timer.run();
  analogWrite(ledPin, brightness);//fade led
    brightness = brightness + fadeAmount;//fade led
    if (brightness <= 0 || brightness >= 1023) //fade led
   {///fade led
     fadeAmount = -fadeAmount;//fade led
   }  //fade led
    delay(1);//fade led
}