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");
}
}
}
@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:
```
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…
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();
}
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.
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.
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.
#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();
}
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