Hardware problem to software

Hello,

I have Hardware to software problem.
I want to light a led on the app with a physical button.

I use Arduino Wemos D1 R1. The connection is well established with my phone but it is my code that must not be good I think.

My code :

> #define BLYNK_PRINT Serial
> #include <ESP8266WiFi.h>
> #include <BlynkSimpleEsp8266.h>
> 
> char auth[] = "**********************";
> char ssid[] = "************";
> char pass[] = "*******************";
> 
> const int btnPin = 2;
> 
> WidgetLED led3(V0);
> 
> BlynkTimer timer;
> 
> // V3 LED Widget represents the physical button state
> boolean btnState = false;
> void buttonLedWidget()
> {
>   // Read button
>   boolean isPressed = (digitalRead(btnPin) == LOW);
> 
>   // If state has changed...
>   if (isPressed != btnState) {
>     if (isPressed) {
>       led3.on();
>     } else {
>       led3.off();
>     }
>     btnState = isPressed;
>   }
> }
> 
> void setup()
> {
>   Blynk.begin(auth, ssid, pass);
>    // Setup physical button pin (active low)
>   pinMode(btnPin, INPUT_PULLUP);
> 
>   timer.setInterval(500L, buttonLedWidget);
> }
> 
> 
> void loop()
> {
>   Blynk.run();
>   timer.run();
> }

I hope you can help me :slight_smile:

// Read button
>   boolean isPressed = (digitalRead(btnPin) == LOW);

This is a little strange to me. Maybe try something like this:

int btnState = 2; //not 1 or 0 so the function will always run once at boot


// Read button
>   int isPressed = digitalRead(btnPin) ;

Thanks for your answer, I tested but it does not change any reaction on the side of the app. If you need more details I can give you what you want :slight_smile:

Code after modification :

> #define BLYNK_PRINT Serial
> #include <ESP8266WiFi.h>
> #include <BlynkSimpleEsp8266.h>
> 
> char auth[] = "*****************";
> char ssid[] = "*****************";
> char pass[] = "*****************";
> 
> const int btnPin = 2;
> 
> WidgetLED led3(V0);
> 
> BlynkTimer timer;
> 
> // V3 LED Widget represents the physical button state
> boolean btnState = false;
> void buttonLedWidget()
> {
> int btnState = 2;
> int isPressed = digitalRead(btnPin) ;
> 
>   // If state has changed...
>   if (isPressed != btnState) {
>     if (isPressed) {
>       led3.on();
>     } else {
>       led3.off();
>     }
>     btnState = isPressed;
>   }
> }
> 
> void setup()
> {
>   Blynk.begin(auth, ssid, pass);
>    // Setup physical button pin (active low)
>   pinMode(btnPin, INPUT_PULLUP);
> 
>   timer.setInterval(500L, buttonLedWidget);
> }
> 
> 
> void loop()
> {
>   Blynk.run();
>   timer.run();
> }

it is working for me. I changed to pin to work with my board.

Double check that you are using the correct pin. D2 is NOT GPIO2.

 #define BLYNK_PRINT Serial
 #include <ESP8266WiFi.h>
 #include <BlynkSimpleEsp8266.h> 
 
 char auth[] = "";
 char ssid[] = "";
 char pass[] = "";
 
  const int btnPin = 14; 
  
  WidgetLED led3(V0);
  
  BlynkTimer timer;

 int btnState = 2;
  
  // V3 LED Widget represents the physical button state
  
  void buttonLedWidget()
  {
  
  int isPressed = digitalRead(btnPin) ;
  
    // If state has changed...
    if (isPressed != btnState) {
      if (isPressed) {
        led3.on();
      } else {
        led3.off();
      }
      btnState = isPressed;
    }
  }
  
  void setup()
  {

     // Debug console
    Serial.begin(115200);
    Blynk.begin(auth, ssid, pass);
     // Setup physical button pin (active low)
    pinMode(btnPin, INPUT_PULLUP);
  
    timer.setInterval(500L, buttonLedWidget);
  }
  
  
  void loop()
  {
    Blynk.run();
    timer.run();
  }

Ok, Thanks a lot iā€™m just using wrong output :smile:

I have one more question, if I want through this button increment a variable on the application is this possible and how can i do ?

You are the best :slight_smile:

Here is an example:

  #define BLYNK_PRINT Serial
 #include <ESP8266WiFi.h>
 #include <BlynkSimpleEsp8266.h> 
 
 char auth[] = "";
 char ssid[] = "";
 char pass[] = "";
 
  const int btnPin = 14;
  
  WidgetLED led3(V0);
  
  BlynkTimer timer;

  int btnState = 2;
  int counter = 0;
  
  // V3 LED Widget represents the physical button state
  
  void buttonLedWidget()
  {

  int isPressed = digitalRead(btnPin) ;
  
    // If state has changed...
    if (isPressed != btnState) {
      if (isPressed) {
        led3.on();
        Serial.println("ON");
      } else {
        led3.off();
        counter++;
        Blynk.virtualWrite(V2, counter);
        Serial.println(counter);
        Serial.println("OFF");
      }
      btnState = isPressed;
    }
  }
  
  void setup()
  {

     // Debug console
    Serial.begin(115200);
    Blynk.begin(auth, ssid, pass);
     // Setup physical button pin (active low)
    pinMode(btnPin, INPUT_PULLUP);
  
    timer.setInterval(500L, buttonLedWidget);
  }
  
  
  void loop()
  {
    Blynk.run();
    timer.run();
  }

Thanks a lot ! I just want to do one last thing is add a button on the app that can reset the counter if you have this you are the best ! :slight_smile:

BLYNK_WRITE(V3) \\ virtual blynk button in  push mode
{
counter=0;
}
1 Like