I can't sync a physical button to a widget button

How do I sync a physical button with a widget button. I tried it but I can’t figure out how to do it.

I’m controlling a relay. I want the physical button and the widget button to turn the relay on and off and be in sync.

well, we cant figure it out either!!!

hmmm, maybe if you post your code, and explain your electronics set up, maybe we could help???

I don’t know what to do for the code. I can tell you that the push button switch is connected to analog input pin 0 and the relay is connected to digital output pin 13. I am also connecting the Arduino board to the internet through USB. This project is a relay that controls a lamp, but I also want to control it through a physical button.

http://docs.blynk.cc/#blynk-main-operations-state-syncing

Which code should I use?

have you tried the code in Pavel’s link? that should work…

Yea, but there are 6. Which one should I try?

This is quite simple…

// pins
#define buttonPin 0
#define RelayPin 13

// global vars
int ButtonCurState;
int ButtonLastState;
int RelayState;

void setup() {
  ButtonLastState == LOW; // start low else it doesnt work 
}

// depending on your button type (NO/NC) you may want to flip all 
// the HIGH/LOWs around so that you get your DOWN press of the button first
void physicalButton() {
  ButtonCurState = digitalRead(buttonPin); // read the button state
  if (ButtonCurState == LOW && ButtonLastState == HIGH) { // momentary button was pressed down as its went HIGH to LOW (or vice verca)
    if (RelayState == 0) { // check if blynk hasnt already set it, then turn it OFF if its ON and ON if its OFF. 
      toggleRelay(1);
    } else if (RelayState == 1) {
      toggleRelay(0);
    }
    ButtonLastState = ButtonCurState; // now set the last state so that this only happens on the down press of the button
  }
  if (ButtonCurState == HIGH && ButtonLastState == LOW) { // momentary button was pressed released so just reset the button state
    // you can also perform secondary functions on release of the button here
    ButtonLastState = ButtonCurState;
  }
}

void toggleRelay(int state) {
  if (state == 1) {
    digitalWrite(RelayPin, ON);
    RelayState = 1;
  } else if (state == 0) {
    digitalWrite(RelayPin, OFF);
    RelayState = 0;
  }
  Blynk.virtualWrite(BLYNK_vPIN,RelayState); // this will reflect the relay state to the button so you know if the lamp is on from the app
}

BLYNK_WRITE(BLYNK_vPIN) {
  // set up a SWITCH button on any available Virtual Pin
  // you will be setting a global var that will be checked in the loop by physicalButton()
  toggleRelay(param.asInt());
}

void loop() {
  physicalButton(); // monitor the button in the loop
}

i think you could try the SyncPhysicalButton one

it gave me an error. @Jamin

This one gave me an error too. @Dave1829

Elaborate…

here is the error message.

Arduino: 1.6.7 (Windows 10), Board: “Arduino/Genuino Uno”

sketch_oct03b:42: error: expected constructor, destructor, or type conversion before ‘(’ token

BLYNK_WRITE(BLYNK_vPIN) {

         ^

C:\Users\TYSONB~1\AppData\Local\Temp\arduino_d3e46703b3e967560b28025ff1357c3d\sketch_oct03b.ino: In function ‘void toggleRelay(int)’:

sketch_oct03b:34: error: ‘ON’ was not declared in this scope

 digitalWrite(RelayPin, ON);

                        ^

sketch_oct03b:37: error: ‘OFF’ was not declared in this scope

 digitalWrite(RelayPin, OFF);

                        ^

C:\Users\TYSONB~1\AppData\Local\Temp\arduino_d3e46703b3e967560b28025ff1357c3d\sketch_oct03b.ino: At global scope:

sketch_oct03b:42: error: expected constructor, destructor, or type conversion before ‘(’ token

BLYNK_WRITE(BLYNK_vPIN) {

        ^

exit status 1
expected constructor, destructor, or type conversion before ‘(’ token

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

You didnt just copy my code and expect it to work did you? Its a snippet for you to add to your own code… adding it to something that compiles before hand would be a good idea.

I dont know what librarys and things you need so add all that first and compile it. No errors? then go an add my code snippet.

Obviously you will need to set more global vars like BLYNK_vPIN or just change it to V1 (for example).

Parts of what I’m seeing look a lot like an author token? If that’s the case can I recommend you edit your post?

Tyson - you really really need to start at the basics; get blynk working, then try to add the relay!

1 Like

And update the Arduino IDE. This version is kind of old :slight_smile:

Can you show me how to connect physical switch to relay and arduino?

if you provide schematics would be great :slight_smile:

If i did it for you, how would you learn? haha
Start with this, then once its 100% working, try to merge blynk in to teh code (using the above example) https://www.arduino.cc/en/tutorial/switch
^^ just replace LED with relay and it will function the same