Control Lights with Blynk and Physical Buttons

What about the ledStates and btnStates for your additional LEDs and physical buttons?

Pete.

Thank you so much Pete!!! It is finally working exactly as it should. I know you spoonfed quite a bit. This is how I learn the best. I can’t just sit and read the info, make it stick and understand it. I have to make it do something practical. Here is the final code I ended up with. I’d love to hear any suggestions to make it better even though it does function perfectly.

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

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

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

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

// Set your LED and physical button pins here
const int ledPin = 7;
const int ledPin1 = 8;
const int ledPin2 = 9;
const int btnPin = 2;
const int btnPin1 = 3;
const int btnPin2 = 4;

BlynkTimer timer;
void checkPhysicalButton();

int ledState = LOW;
int ledState1 = LOW;
int ledState2 = LOW;
int btnState = HIGH;
int btnState1 = HIGH;
int btnState2 = HIGH;

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

  // 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);
}

BLYNK_WRITE(V3) {
  ledState = param.asInt();
  digitalWrite(ledPin1, ledState1);
}
BLYNK_WRITE(V4) {
  ledState = param.asInt();
  digitalWrite(ledPin2, ledState2);
}

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;
  }
   if (digitalRead(btnPin1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState1 != LOW) {

      // Toggle LED state
      ledState1 = !ledState1;
      digitalWrite(ledPin1, ledState1);
      
      // Update Button Widget
      Blynk.virtualWrite(V3, ledState1);
          }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
     if (digitalRead(btnPin2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState2 != LOW) {

      // Toggle LED state
      ledState2 = !ledState2;
      digitalWrite(ledPin2, ledState2);
      
      // Update Button Widget
      Blynk.virtualWrite(V4, ledState2);
          }
    btnState2 = LOW;
  } else {
    btnState2 = HIGH;
  }
}

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

  Blynk.begin(auth, ssid, pass);

  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);
  pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(ledPin1, ledState);
  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin2, ledState);

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

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

Celebrated a bit soon I guess. It works flawlessly when being powered by they USB port, but once I power it with a 12v power supply it gets a little crazy. Maybe just a cheap power supply sending bad power to the board? I used this same power supply with a Nano close for years for a strip of addressable LED’s without issue.

You’re still using the wrong variables in places.

Your other issue sounds like a power supply problem.

Pete.

Hello, I made an automation center using an Arduino Mega and a W5500 ethernet shield, below is the code exemplifying just one physical button synchronized with Blynk’s virtual button.
Everything works perfectly until the moment that there is a failure in the internet connection, the program crashes and the physical buttons stop working…
The Ethernet.linkstatus command only works to test the cable connection, and it works very well, when you remove the internet cable the code keeps working.
Could you help me how do I make the code continue running even without internet?
Thanks

#define BLYNK_PRINT Serial
#define INTERVAL_RECONNECT 60000L
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SerialRelay.h>
SerialRelay relays(3, 2, 10); // (data, clock, número de módulos)



char auth[] = "xxxxxxxxxxxxx";

// Set your physical button pins here
int btn5Pin = 5;
int relay5State = LOW;
int btn5State = HIGH;

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

// When App button is pushed - switch the state

BLYNK_WRITE(V5) {
  relay5State = param.asInt();
  if(relay5State==HIGH){relays.SetRelay(1, SERIAL_RELAY_ON, 1);}
       if(relay5State==LOW){relays.SetRelay(1, SERIAL_RELAY_OFF, 1);}
}



void checkPhysicalButton()

{
    //botão 5
   if (digitalRead(btn5Pin) == LOW) {
    if (btn5State != LOW) {
      relay5State = !relay5State;
      if(relay5State==HIGH){relays.SetRelay(1, SERIAL_RELAY_ON, 1);}
       if(relay5State==LOW){relays.SetRelay(1, SERIAL_RELAY_OFF, 1);}

      // Update Button Widget
      Blynk.virtualWrite(V5, relay5State);
    }
    btn5State = LOW;
  } else {
    btn5State = HIGH;
  }
 

void setup()
{
  
  Serial.begin(9600);  

  if (Ethernet.linkStatus() == LinkON) {
    Blynk.begin(auth);}
  

 //pinMode(ledPin, OUTPUT);
  pinMode(btn5Pin, INPUT_PULLUP);
 
  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
  timer.setInterval(INTERVAL_RECONNECT, timerReconnect);
  
}

void loop()
{
  
  if (Blynk.connected()){Blynk.run();}
  timer.run();
  }
  
void timerReconnect()
{
  if (Ethernet.linkStatus() == LinkON){
  if(!Blynk.connected())
  {
    Blynk.connect();}
    if(!Blynk.connected())
  {
    Blynk.begin(auth);}
}```

@Rafaorme 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:
```

Also, I’ve deleted your duplicate post in the other topic.
Please don’t post the same question in multiple topics, it gets very messy when people star responding to both questions.

Pete.

@PeteKnight
I edited the post, and sorry for the duplicity I remembered that I had already asked about it in another post and put it there with the code.

Thanks