Creating a pushbutton and linking the state of one or more buttons

Hello.

I would like to create a simple pushbutton, made of two buttons that are related each other, in a way that when one pushes the first button the second is released and viceversa.
What best programming approach to follow in Blynk? Can the app modify the state of a button, possibly resetting it to a default state?
Thanks,
Marco

Assuming Button Widgets here, and not physical. This can be done with code and Blynk commands like Blynk.virtualWrite(vPin, value) to set a button state from code.

But why?? Just set a Button Widget to switch mode if you need it to stay in a set state.

Yes, button widgets associated to virtual pins are also ok for me.
I asked because when you press one of the buttons, in a pushbutton of two for simplicity, the label of the button being released is kept old while it should be reset as well in my goal, at least that was assumed by me. If you confirm that using Blynk.virtualWrite(vPin, value) sets a button state from code and also changes accordingly the value of the label associated I will try then.
Thanks, Marco
,

Lots of “How to” and info here

http://help.blynk.cc

http://docs.blynk.cc

Thank you for you pointers. I managed to run successfully my first Blynk sketch.
In brief, the intended pushbutton has two buttons, when you latch one the other one is released. It should control from D0 to D3 for driving four relays, a loudspeaker switcher.
It is just a working edition, it might be written much better with respect of Blynk semantics, thus any criticism welcome.
Thanks, Marco

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "Myauthtoken.....";

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

int accesoA=0;
int accesoB=0;

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

// prepare GPIO16, GPIO5, GPIO4, GPIO0
  pinMode(16, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);

 // Initialize relay states 
  digitalWrite(16, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(0, HIGH);
  
Blynk.begin(auth, ssid, pass);
}

BLYNK_WRITE(0)
{
int i=param.asInt();
accesoB=0;
Blynk.virtualWrite(V1, 0); // Release button
if (!accesoA) {

digitalWrite(16, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
digitalWrite(0, HIGH);

accesoA=1;
}
Blynk.virtualWrite(V0, 1); // Keep ON if necessary
}

BLYNK_WRITE(1)
{
  int i=param.asInt();  
  accesoA=0;
  Blynk.virtualWrite(V0, 0); // Release button
  if (!accesoB) {
  
digitalWrite(16, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(0, LOW);

  accesoB=1;
  }
  Blynk.virtualWrite(V1, 1); // Keep ON if necessary
}

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

I still don’t understand your logic for this… a single Button, in switch mode and a single Blynk function will do the same thing.

BLYNK_WRITE(V0) {  // Button Widget on V0 set to SWITCH mode
  if (param.asInt() == 1) {  // If button state is 1 (HIGH) then...
    digitalWrite(16, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(0, HIGH);
  } else {  // If button state is 0 (LOW) then...
    digitalWrite(16, LOW);
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    digitalWrite(0, LOW);
  }
}

Yes, you are perfectly right. My logic is a little biased by the fact that I’m interested to switch multiple equipments rather than toggling just between two (loudspeakers), so that using multiple buttons may become necessary, not mentioning wanting to test the Blynk ability to simulate a case like that.
Thank you
Marco

I guess that is my confusion… I just can’t fathom the hows and whys of your particular use case.

But aside from showing how a single widget can have (at least) two states to react from, you can also influence such state from the MCU as previously mentioned with Blynk.virtualWrite(vPin, value); But you can also have the MCU “trigger” the function without needing to actually press a button or move a slider with Blynk.syncVirtual(vPin).

3 posts were split to a new topic: A slider button with multiple states

I see, syncronizing states in case of a communication break, right? I’ll consider to include that important function in my sketch, although not a critical feature here.

Never thought about a pushbutton in terms of a menu but I’ll take that into consideraton as a Blynk exercise and application improvement.

Thank you
Marco

That does seem the more common use, but it can also be used to automate actions that would normally be dependent on App control, and without having to duplicate the control function in the sketch.

For example, having a coded function that uses a slider value to control the intensity of a light bulb, but also using a LDR (light sensor) routine that runs on a timer, say every hour, and as it detects the daylight changing, it can send Blynk.virtualWrite() commands to adjust the slider values, followed by Blynk.syncVirtual() commands that trigger the function that controls the light based on the current slider values :stuck_out_tongue_winking_eye: Thus both manual and automatic control over the same code function.

Blynk with programming makes for very powerful IoT control options.