My code run but physical button not run

hello, I can open it from the phone, the physical button only works button1. other relays are not working. can you help with this?

    #include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "7b2f0486955b41b2aecfb2551f5f2975";

char ssid[] = "okhan";
char pass[] = "atakan2009";

const int btnPin = D5;  // pin for physical push button switch.
const int btnPin1 = D6; 
const int btnPin2 = D7; 
const int btnPin3 = D8; 
const int RelayPin = D1;         // pin for relay output.
const int RelayPin1 = D2;
const int RelayPin2 = D3;
const int RelayPin3 = D4;

BlynkTimer timer;
void checkPhysicalButton();
void checkPhysicalButton1();
void checkPhysicalButton2();
void checkPhysicalButton3();

int btnPinState = LOW;           // ON
int RelayPinState = HIGH;        // OFF
//
int btnPin1State = LOW;           // ON
int RelayPin1State = HIGH;        // OFF
//
int btnPin2State = LOW;           // ON
int RelayPin2State = HIGH;        // OFF
//
int btnPin3State = LOW;           // ON
int RelayPin3State = HIGH;        // OFF

//*******Sets Relays to Off Position*****************
#define TURN_ON 0                 // TURN_ON and TURN_OFF are defined to account for Active High relays
#define TURN_OFF 1                // Used to switch relay states

void setup()
{
  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(RelayPin, OUTPUT);            //  initialize your pin as output.
   pinMode(RelayPin1, OUTPUT);            //  initialize your pin as output.
    pinMode(RelayPin2, OUTPUT);            //  initialize your pin as output.
     pinMode(RelayPin3, OUTPUT);            //  initialize your pin as output.
  pinMode(btnPin, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
    pinMode(btnPin1, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
      pinMode(btnPin2, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
        pinMode(btnPin3, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
  digitalWrite(RelayPin, RelayPinState);
    digitalWrite(RelayPin1, RelayPin1State);
      digitalWrite(RelayPin2, RelayPin2State);
        digitalWrite(RelayPin3, RelayPin3State);
  digitalWrite(RelayPin, TURN_OFF);     // remain off till command is receive
    digitalWrite(RelayPin1, TURN_OFF);     // remain off till command is receive
      digitalWrite(RelayPin2, TURN_OFF);     // remain off till command is receive
        digitalWrite(RelayPin3, TURN_OFF);     // remain off till command is receive

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

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

// When App button is pushed - switch the state
// Map this Virtual Pin to your Mobile Blynk apps widget.
BLYNK_WRITE(V1)
{                           
  RelayPinState = param.asInt();
  digitalWrite(RelayPin, RelayPinState);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW)
  {
    // btnPinState is used to avoid sequential toggles
    if (btnPinState != LOW)
    {
      // Toggle relay state
      RelayPinState = !RelayPinState;
      digitalWrite(RelayPin, RelayPinState);
      Blynk.virtualWrite(V1, RelayPinState);  //Update button widget
    }

    btnPinState = LOW; 
  }
  else
  {
    btnPinState = HIGH;
  }
}

//dneeme

BLYNK_WRITE(V2)
{                           
  RelayPin1State = param.asInt();
  digitalWrite(RelayPin1, RelayPin1State);
}

void checkPhysicalButton1()
{
  if (digitalRead(btnPin1) == LOW)
  {
    // btnPinState is used to avoid sequential toggles
    if (btnPin1State != LOW)
    {
      // Toggle relay state
      RelayPin1State = !RelayPin1State;
      digitalWrite(RelayPin1, RelayPin1State);
      Blynk.virtualWrite(V2, RelayPin1State);  //Update button widget
    }

    btnPin1State = LOW; 
  }
  else
  {
    btnPin1State = HIGH;
  }
}
////BUTON 3


BLYNK_WRITE(V3)
{                           
  RelayPin2State = param.asInt();
  digitalWrite(RelayPin2, RelayPin2State);
}

void checkPhysicalButton2()
{
  if (digitalRead(btnPin2) == LOW)
  {
    // btnPinState is used to avoid sequential toggles
    if (btnPin2State != LOW)
    {
      // Toggle relay state
      RelayPin2State = !RelayPin2State;
      digitalWrite(RelayPin2, RelayPin2State);
      Blynk.virtualWrite(V3, RelayPin2State);  //Update button widget
    }

    btnPin2State = LOW; 
  }
  else
  {
    btnPin2State = HIGH;
  }
}
////BUTON 4
BLYNK_WRITE(V4)
{                           
  RelayPin3State = param.asInt();
  digitalWrite(RelayPin3, RelayPin3State);
}

void checkPhysicalButton3()
{
  if (digitalRead(btnPin3) == LOW)
  {
    // btnPinState is used to avoid sequential toggles
    if (btnPin3State != LOW)
    {
      // Toggle relay state
      RelayPin3State = !RelayPin3State;
      digitalWrite(RelayPin3, RelayPin3State);
      Blynk.virtualWrite(V4, RelayPin3State);  //Update button widget
    }

    btnPinState = LOW; 
  }
  else
  {
    btnPinState = HIGH;
  }
}
//

void loop()
{
  Blynk.run();                               // Run Blynk
  timer.run();                               // Run SimpleTimer
}

This is your 4th or 5th topic… but you never follow through on the others.

What more can we say here that hasn’t already been said. We don’t fix your code for you, we try to help you learn…

Yes some will help with code, but you have to show efforts and feedback first… not just create another topic with another issue.

1 Like

I understand what they say hello, I’ve edited the code and I can’t run the 3 buttons, I’m aware, I’m trying to learn, I’ve edited this code like this. I want to know where I’ve made a mistake … excuse me!

So is this the same project as all your other topics??

You only have one timer checking one button… and rather too fast as well… For physical button detection, you would be better using hardware interrupt pins instead of timers. Google for using that with ESP8266

1 Like

Hi, I’ve run the code, only D0 Pin and D8 pin are not working. all other relays are working normally, I can turn the phone off and off from the physical button, I just tried both these D0-D8 pins and didn’t get any results either. alternatively which pin can I use.D8 button does not work physically

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "7b2f0486955b41b2aecfb2551f5f2975";

char ssid[] = "okhan";
char pass[] = "atakan2009";

const int btnPin = D5;  // pin for physical push button switch.
const int btnPin1 = D6; 
const int btnPin2 = D7; 
const int btnPin3 = D8; 
const int RelayPin = D1;         // pin for relay output.
const int RelayPin1 = D2;
const int RelayPin2 = D3;
const int RelayPin3 = D4;

BlynkTimer timer;
void checkPhysicalButton();
void checkPhysicalButton1();
void checkPhysicalButton2();
void checkPhysicalButton3();

int btnPinState = LOW;           // ON
int RelayPinState = HIGH;        // OFF
//
int btnPin1State = LOW;           // ON
int RelayPin1State = HIGH;        // OFF
//
int btnPin2State = LOW;           // ON
int RelayPin2State = HIGH;        // OFF
//
int btnPin3State = LOW;           // ON
int RelayPin3State = HIGH;        // OFF

//*******Sets Relays to Off Position*****************
#define TURN_ON 0                 // TURN_ON and TURN_OFF are defined to account for Active High relays
#define TURN_OFF 1                // Used to switch relay states


void setup()
{
  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(RelayPin, OUTPUT);            //  initialize your pin as output.
   pinMode(RelayPin1, OUTPUT);            //  initialize your pin as output.
    pinMode(RelayPin2, OUTPUT);            //  initialize your pin as output.
     pinMode(RelayPin3, OUTPUT);            //  initialize your pin as output.
  pinMode(btnPin, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
    pinMode(btnPin1, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
      pinMode(btnPin2, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
        pinMode(btnPin3, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
  digitalWrite(RelayPin, RelayPinState);
    digitalWrite(RelayPin1, RelayPin1State);
      digitalWrite(RelayPin2, RelayPin2State);
        digitalWrite(RelayPin3, RelayPin3State);
  digitalWrite(RelayPin, TURN_OFF);     // remain off till command is receive
    digitalWrite(RelayPin1, TURN_OFF);     // remain off till command is receive
      digitalWrite(RelayPin2, TURN_OFF);     // remain off till command is receive
        digitalWrite(RelayPin3, TURN_OFF);     // remain off till command is receive

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

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

// When App button is pushed - switch the state
// Map this Virtual Pin to your Mobile Blynk apps widget.
BLYNK_WRITE(V1)
{                           
  RelayPinState = param.asInt();
  digitalWrite(RelayPin, RelayPinState);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW)
  {
    // btnPinState is used to avoid sequential toggles
    if (btnPinState != LOW)
    {
      // Toggle relay state
      RelayPinState = !RelayPinState;
      digitalWrite(RelayPin, RelayPinState);
      Blynk.virtualWrite(V1, RelayPinState);  //Update button widget
    }

    btnPinState = LOW; 
  }
  else
  {
    btnPinState = HIGH;
  }
}

//dneeme

BLYNK_WRITE(V2)
{                           
  RelayPin1State = param.asInt();
  digitalWrite(RelayPin1, RelayPin1State);
}

void checkPhysicalButton1()
{
  if (digitalRead(btnPin1) == LOW)
  {
    // btnPinState is used to avoid sequential toggles
    if (btnPin1State != LOW)
    {
      // Toggle relay state
      RelayPin1State = !RelayPin1State;
      digitalWrite(RelayPin1, RelayPin1State);
      Blynk.virtualWrite(V2, RelayPin1State);  //Update button widget
    }

    btnPin1State = LOW; 
  }
  else
  {
    btnPin1State = HIGH;
  }
}
////BUTON 3


BLYNK_WRITE(V3)
{                           
  RelayPin2State = param.asInt();
  digitalWrite(RelayPin2, RelayPin2State);
}

void checkPhysicalButton2()
{
  if (digitalRead(btnPin2) == LOW)
  {
    // btnPinState is used to avoid sequential toggles
    if (btnPin2State != LOW)
    {
      // Toggle relay state
      RelayPin2State = !RelayPin2State;
      digitalWrite(RelayPin2, RelayPin2State);
      Blynk.virtualWrite(V3, RelayPin2State);  //Update button widget
    }

    btnPin2State = LOW; 
  }
  else
  {
    btnPin2State = HIGH;
  }
}
////BUTON 4
BLYNK_WRITE(V4)
{                           
  RelayPin3State = param.asInt();
  digitalWrite(RelayPin3, RelayPin3State);
}

void checkPhysicalButton3()
{
  if (digitalRead(btnPin3) == LOW)
  {
    // btnPinState is used to avoid sequential toggles
    if (btnPin3State != LOW)
    {
      // Toggle relay state
      RelayPin3State = !RelayPin3State;
      digitalWrite(RelayPin3, RelayPin3State);
      Blynk.virtualWrite(V4, RelayPin3State);  //Update button widget
    }

    btnPin3State = LOW; 
  }
  else
  {
    btnPin3State = HIGH;
  }
}
//

void loop()
{
  Blynk.run();                               // Run Blynk
  timer.run();                               // Run SimpleTimer
}
,,,

I solved my problem, thank you, I used the GPI03 pin instead of the D8 pin.

As mentioned throughout this forum it is always preferred to use the GPIO numbering instead of the silk screen label. Many times (depending on IDE board type) the silk screened value is not recognised properly.

1 Like

I’ve managed to run the D8 pin, I need to use the 10k resistor together.

On ESP8266 GPIO15 (D8) already has an internal pulldown resistor (NodeMCU at least) and needs to stay LOW in order to boot properly.

BTY, that pin GPIO3 (RX / D9) is used with the RX serial interface… having something hooked to it while trying to upload code might cause issues.

I think it wouldn’t be a problem if

What?? I can’t read what you said… I have something else showing lots of text on my screen at the same time… :wink: