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

NodeMcu and physical buttons!

Tell me please ! It is necessary to control a single output with physical buttons and applications? if the code is correct or it is necessary to correct?

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "YourAuthToken";
void checkPhysicalButton();
const int butdoorbell = 4;
const int doorbell = 13;
SimpleTimer timer;
int ledState = LOW;
void setup()
{
  Serial.begin(115200);
   Blynk.begin(auth, "ssid", "pass");
  digitalWrite(doorbell, ledState);
   pinMode(doorbell, OUTPUT);
   pinMode(butdoorbell, INPUT);
   timer.setInterval(300L, checkPhysicalButton);
}
BLYNK_CONNECTED()
 {
  Blynk.syncAll();
  }
   BLYNK_WRITE(V10){
     ledState = param.asInt();
   digitalWrite(doorbell, ledState);
   }
   void checkPhysicalButton()
   {
   if (digitalRead(butdoorbell) == HIGH){
    digitalWrite(doorbell, HIGH);
    Blynk.virtualWrite(V10, HIGH);
   }
  if (digitalRead(butdoorbell) == LOW){
     digitalWrite(doorbell, ledState);
     Blynk.virtualWrite(V10, ledState);
  }
  } 
     void loop()
     {
     Blynk.run();
     timer.run();
     }

looks like it could work, what is the problem?

Everything is working ! I’m new to Arduino, I thought to experts looked and said, if something goes wrong!

No man, this looks pretty much ok :slight_smile:

You got the concept figured out. Very nice cat too! Love it!

@AND, In checkPhysicalButton (which is called rather frequently), it would be better to store previous state, and send an updated value only if it changes. It would conserve lot’s of traffic.

Please tell me how to do it!

something like…

   int lastStatus = HIGH;
   void checkPhysicalButton()
   {
     int currentStatus = digitalRead(butdoorbell);
     if (currentStatus != lastStatus) {
       if (currentStatus == HIGH){
         digitalWrite(doorbell, HIGH);
         Blynk.virtualWrite(V10, HIGH);
       } else {
         digitalWrite(doorbell, ledState);
         Blynk.virtualWrite(V10, ledState);
       }
       lastStatus = currentStatus;
     }
  } 
1 Like

Thank you so much for helping me!!

A post was split to a new topic: Problem using physical buttons