Why does the button not work?

Why does the button not work?
I used the code in this page. Added a button and an LED in APP.
https://examples.blynk.cc/?board=NodeMCU&shield=ESP8266%20WiFi&example=More%2FSync%2FButtonPoll

Code

 *************************************************************

  This example shows how to monitor a button state
  using polling mechanism.

  App project setup:
    LED widget on V1
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

int prevState = -1;
int currState = -1;
long lastChangeTime = 0;

void checkPin()
{
  // Invert state, since button is "Active LOW"
  int state = !digitalRead(2);

  // Debounce mechanism
  long t = millis();
  if (state != prevState) {
    lastChangeTime = t;
  }
  if (t - lastChangeTime > 50) {
    if (state != currState) {
      currState = state;
      Blynk.virtualWrite(V1, state);
    }
  }
  prevState = state;
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // 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);

  // Make pin 2 default HIGH, and attach INT to our handler
  pinMode(2, INPUT_PULLUP);
}

void loop()
{
  Blynk.run();
  checkPin();
}

Button OUTPUT: V2 MODE: PUSH
LED INPUT: V1

20170914174852

I can directly let the button control D1 on the LED connected to the light and off. But I use the program to control the LED in the LED light and off, I do not know where the reason.
Are there enthusiastic friends who can answer this question for me?
If the question I have provided is not enough, you can leave a message and I will add it.
Thank you for taking the time to read and answer my questions!

try to set the button to switch instead of push. if that not helps,

post your actual sketch (properly formatted) and the schematic of your hw.

I have updated and added the question

Doesn’t the LED widget work in 0 for off and 255 for on?

Pete.

1 Like
Blynk.virtualWrite(V1, state);

if state == 1… then you have turned on your LED to 1 brightness… in a range of 0-255.

so its on but super super dim.

You need to map state to 0-255.

Blynk.virtualWrite(V1, map(state, 0, 1, 0, 255));

Yes, it is not well documented.

No, I tried your source code, APP on the LED is still not bright, I really can not find the reason

void checkPin()
{
  // Invert state, since button is "Active LOW"
  int state = !digitalRead(2);

  // Debounce mechanism
  long t = millis();
  if (state != prevState) {
    lastChangeTime = t;
  }
  if (t - lastChangeTime > 50) {
    if (state != currState) {
      currState = state;
      Blynk.virtualWrite(V1, map(state, 0, 1, 0, 255));
    }
  }
  prevState = state;
}

This is the code.

@mtrucc I’m guessing you misunderstood what the original sketch was doing.
It relates to a physical button on digital pin 2 not a Blynk virtual button on V2.
From your screenshots that is what I think your error is.

If you have actually connected a physical button to digital pin 2 let us know and we will debug further.

I once again seriously look at the code, I think you are right, I connected the APP on the virtual button, rather than the physical button. Then pin 2 in Nodemcu which is D2?

20170915144112

pinMode function I did not find in the document ~ so i did not quite understand this function.

It’s a basic Arduino function to define input / output state of a physical pin.
Are you wanting to use virtual pins / real pins i.e. a physical button or a Blynk virtual button?

Well, I’ll look at this function.
If so, I expect both physical and virtual buttons to be used.

Let’s get the easy part working first i.e. virtual button.
Confirm this works:

BLYNK_WRITE(V2){                 // Blynk virtual button
  if(param.asInt() == 1){
    Blynk.virtualWrite(V1, 255); // turn ON virtual LED
  }
  else{
    Blynk.virtualWrite(V1, 0);   // turn OFF virtual LED
  }
}
1 Like
BLYNK_WRITE(V2){                 // Blynk virtual button
  if(param.asInt() == 1){
    Blynk.virtualWrite(V1, 255); // turn ON virtual LED
  }
  else{
    Blynk.virtualWrite(V1, 0);   // turn OFF virtual LED
  }
}

The experiment is successful and thank you very much! But you may need to fix it, you forget to add “()”:grin::grin:

Edited the code extract :slight_smile:

You can now move on to physical buttons with the code already provided by others.

1 Like

Thank you very much, I also learned the pinMode function.
I’ll sort out the code and link later as a summary.

1 Like
*************************************************************

  This example shows how to monitor a button state
  using polling mechanism.

  App project setup:
    LED widget on V1
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // 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);
}

void loop()
{
  Blynk.run();
}

BLYNK_WRITE(V2){                 // Blynk virtual button
  if(param.asInt() == 1){
    Blynk.virtualWrite(V1, 255); // turn ON virtual LED
  }
  else{
    Blynk.virtualWrite(V1, 0);   // turn OFF virtual LED
  }
}

This is a simple work
Use the virtual buttons on the APP to control the on and off of the LEDs on the APP