Change button state in app from hardware

Hello:
The project is very easy. I want to turn on a led from blynk and be able to turn it off from a switch.
But I want the blynk button to turn off when I turn it off from the switch.
I was looking in the forum and the only thing I saw was trying Blynk.virtualWrite, but it doesn’t work for me.

#define BLYNK_PRINT Serial

#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>


char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxx";


char ssid[] = "xxxxxxxxxxxx";
char pass[] = "xxxxxxxxxx";



void setup(){ 
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
  if (digitalRead(13)==HIGH){   //led on
    Blynk.notify("Yaaay... button is pressed!");
    Serial.println("led on");
  }
  if (Serial.available()>0){   //here we simuleted a swich by serial
    int tecla=Serial.read();
    if (tecla==49){
      digitalWrite (13,LOW);
      Blynk.virtualWrite(V13, 0);       //put off the button on app blynk        
      Serial.println("led off");
    }    
  }
 
}

Thanks

how i print right my code :sweat_smile: :sweat_smile:???

@rubenvalmo please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

now …thanks :grinning:

It would be useful if you were clearer in your description about whether the LEDs and buttons you are referring to are physical (connected to your Arduino) or widgets in the Blynk app.

I’d also suggest staying clear of the Notify widget until you know what you’re doing. Much better to use the Terminal widget to send these messages.

You also need to keep your void loop clean…

Personally though, I’d suggest that you start with the ‘sync physical button’ example in Sketch Builder…

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FSync%2FSyncPhysicalButton

Pete.

hi.
I just tried the example you put in the link, but it still does not update the status in the app. I change the on / off led but the app button is not modified.
I copy the code here in case I was doing something wrong


#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>


char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";


char ssid[] = "xxxxxxxxxxxxx";
char pass[] = "xxxxxxxxx";



const int ledPin = 13;
const int btnPin = 8;

BlynkTimer timer;
void checkPhysicalButton();

int ledState = LOW;
int btnState = HIGH;

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

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState);

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

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", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);

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

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

thanks very much

Try changing this line…

      Blynk.virtualWrite(V2, ledState);

to this…

      Blynk.virtualWrite(V2, ledState * 255);

Pete.

still no working :unamused:

The app its conected beacause i push the virtual botton and do the on/off
The physical button also works because when pressing it changes on / off

the problem is that the virtual button is not updated with the physical button

Thanks

Okay, if you’d have said that originally my advice would have been different. I assumed you were using a virtual LED, not a virtual button.
Change the code back to what you were using originally.

How is your virtual button configured in the app?

Pete.

Sorry, maybe I didn’t explain it well.
I am using a physical button and a virtual button (blynk button) to turn on or off a led.
I want that when pressing the physical button the state (on or off) in the virtual button of blynk is updated.
For example, if I press the physical button on, in the app the virtual button should change to on.

In the app the virtual button is D13

Thank you so much for help me

If you look at the comments in the sketch builder example I linkled to then you’ll see that it says…

App project setup:
Button widget attached to V2 (Switch mode)

Pete.

oops, i didn’t realize that.
I have changed all V2 to V13 … but it still does not update the status

here is something I don’t understand
In the example it says:
const int ledPin = 7;

and is used the Button widget attached to V2 (Switch mode).

I think that’s my problem, I am using:
ledPin = 13
Button widget attached to V13 (Switch mode)

Doesn’t the same number have to be respected?
I am totally lost

What?
Please post a screenshot of your button widget setup in edit mode.

This is the PHYSICAL pin that the PHYSICAL LED is connected to on your board - GPIO7

Where is this V13 coming from? Have you changed the sketch from the original one to use Virtual Pin 13 in the app for your button widget? If so, please post your updated sketch.

Pete.

This is the way its conectet

https://i.postimg.cc/vmxzKMMt/Screenshot-1.jpg

and this is the code

#define BLYNK_PRINT Serial

#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>


char auth[] = "xxxxxxxxxxxxxxxxxxxxxx";


char ssid[] = "xxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxxxx";



const int ledPin = 13;
const int btnPin = 8;

BlynkTimer timer;
void checkPhysicalButton();

int ledState = LOW;
int btnState = HIGH;

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

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V13) {
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V13, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

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", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);

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

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

in the app its D13 ass a switch

…and thanks again

But your code is written to work with the switch widget attached to virtual pin 13, not digital pin 13…

Pete.

I know the problem its here

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState);

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

Im going to lear how its woks blynk… more reading :slight_smile:

So you’ve now changed the sketch back to V2 instead of V13

The important thing, and the bit that I think you’re missing, is which VIRTUAL pin the switch widget is attached to. It has to match your sketch.

Pete.

hi, i fix the problem.
First of all, I had the push-button pull down resistor set wrong. In the code it is like pull up
The second was put in the Blynk pin Digital app and had to put Virtual.
Thanks for your time