Button enaled/disabled status

Hi,

is possible to add enaled/disabled status to Button item?

Now we can set Color, Status ON/OFF, label, etc, but not enable/disable button status (with disabled status button can’t be pressed).

In my Blynk app some enabling/disabling buttons status depend from item selected in Menu Setting, now I have to handle this from Hardware side to ignore button pression, implementing this new feature I could remove lot of trash code.

Thanks
Regards

3 Likes

It would be nice to have such feature

1 Like

@Aro1do

I like the idea of easily enabling and disabling buttons/switches, with visual indication (on app). A nice “lockout” feature that can be easily implemented for whatever reason.

But I don’t see any reason for Blynk to hard code that in… since we would probably still have to write some code to enable or disable the buttons anyhow… code is code, and not much would be needed.

Super simple, even I could do it :wink:… and here is how…

A int line up front (for each item), assigning the variable to enable or disable… and this variable could be adjusted via code on the hardware side, or a “menu” in the app (with more buttons of course :grinning:)

Corresponding lines in setup (for each item) to assign enabled/disabled colours, labels, etc.

And a single if() statement added to each function to allow or disallow action.

I even included my Patent Pending “Flash an LED while the button is pushed, without stalling the rest of the code” routine. WARNING not to be over used lest everything come crashing down in a flood… and you blame me :innocent:

// Two buttons, one to flash a RED LED, one to flash a WHITE LED
// Either button can be enabled or disabled with a 1 or 0 variable

#include <BlynkSimpleStream.h>
char auth[] = "authcodehere";

int v1E = 1; //  V2 Button - Enable(1) Disable(0)
int v2E = 0; //  V2 Button - Enable(1) Disable(0)

void setup()
{
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);

  if (v1E == 0)  { // If disabled black out the V1 button, if enable set to your colour choice.
    Blynk.setProperty(V1, "color", "#000000"); // Black
  } else {
    Blynk.setProperty(V1, "color", "#FF0000"); // Red
  }
  if (v2E == 0)  { // If disabled black out the V2 button, if enable set to your colour choice.
    Blynk.setProperty(V2, "color", "#000000"); // Black
  } else {
    Blynk.setProperty(V2, "color", "#FFFFFF"); // White
  }
}

BLYNK_WRITE(V1) // Run this function when V1 button pressed.
{
  if (v1E == 1) {  // Check if disabled or not.
    int pinValue = param.asInt();  // Get status of V1.
    if (pinValue == 1) {  // If status of V1 is 1 then do stuff in if().
      digitalWrite(3, HIGH); // Turn on LED.
      delay(20); // Wait 20 millis but remember you are holding up the show.
      digitalWrite(3, LOW);  // Turn off LED.
      Blynk.run(); // Run rest of show in-between waiting for this loop to repeat or quit.
      int pinValue = 0;  // Set V1 status to 0 to quit, unless button is still pushed (as per below)
      Blynk.syncVirtual(V1); // ...Then force BLYNK_WRITE(V1) function check of button status to determine if repeating or done.
    }
  }
}

BLYNK_WRITE(V2) // Run this function when V2 button pressed.
{
  if (v2E == 1) {  // Check if disabled or not.
    int pinValue = param.asInt();  // Get status of V2.
    if (pinValue == 1) {  // If status of V2 is 1 then do stuff in if().
      digitalWrite(4, HIGH); // Turn on LED.
      delay(20); // Wait 20 millis but remember you are holding up the show.
      digitalWrite(4, LOW);  // Turn off LED.
      Blynk.run(); // Run rest of show in-between waiting for this loop to repeat or quit.
      int pinValue = 0;  // Set V2 status to 0 to quit, unless button is still pushed (as per below)
      Blynk.syncVirtual(V2); // ...Then force BLYNK_WRITE(V2) function check of button status to determine if repeating or done.
    }
  }
}

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

@Gunner reason to implement this feature in Blynk side is not only to simplify code but also to have a consistent situation between hardware and GUI app. Suppose you have a button in SWITCH mode, you need also code to restore status after button press if it’s disabled.

In your code, with this feature, you could remove two state variable and a lot of statements in BLYNK_WRITEs:

// Two buttons, one to flash a RED LED, one to flash a WHITE LED
// Either button can be enabled or disabled with a 1 or 0 variable

#include <BlynkSimpleStream.h>
char auth[] = "authcodehere";

void setup() {
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

BLYNK_WRITE(V1) { // Run this function when V1 button pressed.
      digitalWrite(3, HIGH); // Turn on LED.
      delay(20); // Wait 20 millis but remember you are holding up the show.
      digitalWrite(3, LOW);  // Turn off LED.
}

BLYNK_WRITE(V2) { // Run this function when V2 button pressed.
      digitalWrite(4, HIGH); // Turn on LED.
      delay(20); // Wait 20 millis but remember you are holding up the show.
      digitalWrite(4, LOW);  // Turn off LED.
}

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

and change button enabled status where you need with something like this:

Blynk.setProperty(buttonVpin, “enabled”, true);
Blynk.setProperty(buttonVpin, “enabled”, false);

@Aro1do Oh, not to worry, I wasn’t shooting down or even really debating whether they should or shouldn’t… I just took advantage of the idea to experiment/learn more about coding Arduino with Blynk functions. As well as provide examples for others.

Besides…

As it is, it seems switches do not “restore status” if lockups or disconnects occur; nor even when stop/play or logging out/in on the app side. So now they (Blynk) would need to implement a default state as well as active state, might as well as add enhanced haptic feedback options while we’re at it… hmm, I actually like that idea!! Press a disabled button more than once and get a shock!! :electric_plug: :smile:

And besides…

…most of what you “cleaned up” in the BLYNK_WRITEs had nothing to do with the buttons enabled status, rather, their removal disabled the actual sustained operation of those functions. So not a real lot of code savings there. :wink:

But in the end, you made some good suggestions, I learned some more coding ideas for my projects and someone may find a benefit from the examples. A WIN WIN for all!! :fireworks:

1 Like