Sync 4 physical buttons, ARDUINO UNO, ETHERNET

Hi guys, I have a problem with my project. i cant sync my buttons with the blynk app. only two buttons work, if I press the other two buttons,arduino go offline, this is the sketch;

THANK YOU FOR YOUR HELP!

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
char auth[] = "0000000000000000000";
#define W5100_CS  10
#define SDCARD_CS 4
const int ledPin = 7;
const int btnPin = 8;
const int ledPin1 = 6;
const int btnPin1 = 9;
const int ledPin2 = 2;
const int btnPin2 = 13;
const int ledPin3 = 3;
const int btnPin3 = 12;
int ledState = LOW;
int btnState = HIGH;
int ledState1 = LOW;
int btnState1 = HIGH;
int ledState2 = LOW;
int btnState2 = HIGH;
int ledState3 = LOW;
int btnState3 = HIGH;

BLYNK_CONNECTED() {
  
  Blynk.syncVirtual(V2);
Blynk.syncVirtual(V1);
 Blynk.syncVirtual(V8);
Blynk.syncVirtual(V9);
  
}


BLYNK_WRITE(V2) {
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
}
BLYNK_WRITE(V1) {
  ledState1 = param.asInt();
  digitalWrite(ledPin1, ledState1);
}
BLYNK_WRITE(V8) {
  ledState2 = param.asInt();
  digitalWrite(ledPin2, ledState2);
}
BLYNK_WRITE(V9) {
  ledState3 = param.asInt();
  digitalWrite(ledPin3, ledState3);
}

BlynkTimer timer;
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(V1, 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(V8, ledState2);
    }
    btnState2 = LOW;
  } else {
    btnState2 = HIGH;
  }
   if (digitalRead(btnPin3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState3 != LOW) {

      // Toggle LED state
      ledState3 = !ledState3;
      digitalWrite(ledPin3, ledState3);

      // Update Button Widget
      Blynk.virtualWrite(V9, ledState3);
    }
    btnState3 = LOW;
  } else {
    btnState3 = HIGH;
  }
}

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

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);

  Blynk.begin(auth);


  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);
pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(ledPin1, ledState1);
    pinMode(ledPin2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin2, ledState2);
    pinMode(ledPin3, OUTPUT);
  pinMode(btnPin3, INPUT_PULLUP);
  digitalWrite(ledPin3, ledState);
  timer.setInterval(100L, checkPhysicalButton);
}

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

I’ve edited your post so the code formats nice (three backticks ```) before and after the code.

Your timer seems very short. Try setting it to 1000 and see if it still happens. In practice syncing a button every second is fast enough.

I try, thank you!

I try and don’t work…

That’s Arduino weakness.

Which Blynk library version are you using.

Try swapping between 5.0 and 4.10.

What happens when you remove the syncVirtual (or the whole BLYNK_CONNECT for that matter). From time to time I’ve noticed the syncVirtual doesn’t always work well. You could try and add a small delay between the syncs. E.g.

BLYNK_CONNECTED() {
  
  Blynk.syncVirtual(V2);
  delay(50); 
  Blynk.syncVirtual(V1);
  delay(50);
  Blynk.syncVirtual(V8);
  delay(50);
  Blynk.syncVirtual(V9); 
}

i trythe delay ,but, don’t work. Now i change arduno, i try with mega and an other arduino uno! I use the V0.4.1, i’ll try with a 5.0! thank you!

thats works for me on esp8266

#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[] = "xxxxxxxxxx";

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

// Set your LED and physical button pins here
const int ledPin1 = 12;
const int ledPin2 = 13;
const int ledPin3 = 14;
const int ledPin4 = 15;
const int btnPin1 = 5;
const int btnPin2 = 4;
const int btnPin3 = 9;
const int btnPin4 = 1;

BlynkTimer timer;
void checkPhysicalButton();

int led1State = LOW;
int btn1State = HIGH;

int led2State = LOW;
int btn2State = HIGH;

int led3State = LOW;
int btn3State = HIGH;

int led4State = LOW;
int btn4State = HIGH;

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

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V12, led1State);
  //Blynk.virtualWrite(V13, led2State);
  //Blynk.virtualWrite(V14, led3State);
  //Blynk.virtualWrite(V15, led4State);

}

// When App button is pushed - switch the state
BLYNK_WRITE(V12) {
  led1State = param.asInt();
  digitalWrite(ledPin1, led1State);
}
  
 BLYNK_WRITE(V13) {
  led2State = param.asInt();
  digitalWrite(ledPin2, led2State);
 }
BLYNK_WRITE(V14) {
  led3State = param.asInt();
  digitalWrite(ledPin3, led3State);
}
BLYNK_WRITE(V15) {
  led4State = param.asInt();
  digitalWrite(ledPin4, led4State);
}

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

      // Toggle LED state
      led1State = !led1State;
      digitalWrite(ledPin1, led1State);

      // Update Button Widget
      Blynk.virtualWrite(V12, led1State);
    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
  }

  if (digitalRead(btnPin2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      led2State = !led2State;
      digitalWrite(ledPin2, led2State);

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

  if (digitalRead(btnPin3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      led3State = !led3State;
      digitalWrite(ledPin3, led3State);

      // Update Button Widget
      Blynk.virtualWrite(V14, led3State);
    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btnPin4) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn4State != LOW) {

      // Toggle LED state
      led4State = !led4State;
      digitalWrite(ledPin4, led4State);

      // Update Button Widget
      Blynk.virtualWrite(V15, led4State);
    }
    btn4State = LOW;
  } else {
    btn4State = 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", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(ledPin1, led1State);
  

  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin2, led2State);
  

  pinMode(ledPin3, OUTPUT);
  pinMode(btnPin3, INPUT_PULLUP);
  digitalWrite(ledPin3, led3State);
  

  pinMode(ledPin4, OUTPUT);
  pinMode(btnPin4, INPUT_PULLUP);
  digitalWrite(ledPin4, led4State);

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

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