Confirmation on certain buttons

No problem.
Once you get your head around the fact that the BLYNK_WRITE(Vx) functions are called each time a button is pressed or released it makes a bit more sense.

One thing that catches most people out is that when you declare a variable within one of these functions (by saying int V10_Button_Value = param.asInt(); for example), that variable can only be referenced within that function, because it’s a local variable.

As I wanted the flag to be available in both functions, I declared it at the top of my code, making it a global variable.

Pete.

NO EFFECT
Video_00364
.
.
.
.
.

press 2 sec to UNLOCK
Video_00365

see my sketch

Well I think I’ve had success using @PeteKnight’s code. Certainly giving me the right response on the serial monitor. The valve that this project is actuating is at our holiday house so won’t be able to upload to the board until I’m next there.
The code I have ended up with is:

 // Two buttons attached to virtual pins: Activator button = V6 and Main Button = V5. Activator button must be set to PUSH
// Press and hold the Activator button for the action of the Main button to have any effect

boolean Active_Flag=false; // Declare a global variable and set it to false by default

BLYNK_WRITE(V6)  // Automatically triggered when Activator button is pressed
{
  int Activatorbtn = param.asInt(); // will be 1 if button pressed, 0 if button released

  if(Activatorbtn == 1)
  {
    Active_Flag=true; // Set the flag to true if the Activator button was pressed
  }
  else
  {
     Active_Flag=false; // Set the flag to false if the Activator button was released
  }
}

BLYNK_WRITE(V5)  // Automatically triggered when Main button is pressed
{
    if (Active_Flag)  // This next piece of code will only run if the Active_Flag == true
    
  {
      int btnpinstate = param.asInt();  //read the state of the main button
      digitalWrite(btnpin, param.asInt()); // Sets btnPin HIGH (valve opens) or LOW (valve closes) depending on state of Button Widget
      Serial.println("Button state written to digital pin");
      Serial.print("Active_Flag:");
      Serial.println(Active_Flag);
      Serial.print("btn pin state:");
      Serial.println(btnpinstate);
      
  }
  else
  {
    //If the Active_Flag = 0, take no action
    Serial.println("No action taken - activator not active");
    Serial.print("Active_Flag:");
      Serial.println(Active_Flag);
  }
}    

Certainly appreciated all the help. Alexis, I think your code will also work and probably has some additional protection but don’t think I needs as much for my purpose. Thanks again.

Cheers
Steve

1 Like

Good, glad I was able to point you in the right direction.
I don’t normally write snippets of code for people, so think yourself lucky!!

Hopefully you’ve learned a few new coding skills that will serve you well in future. It would be worth you playing around with @Blynk_Coeur’s code as it does some nice changes to the button colours/labels which is also a good skill to learn and makes the user interface more intuitive. I left this stuff out of my example as I wanted to keep it straightforward so that you’d be able to understand the basics.
Hopefully in a few months time you’ll have added a few more bells and whistles as your coding knowledge grows :grinning:

Pete.

1 Like

Yeah - I’m pretty rapt. The comments and code from yourself and Alexis have done in 24 hrs what 2 weeks of trawling through the forums and arduino coding tutorials have not been able to :grinning:.
I agree that Alexis’ code does have some nice features - I will try and implement this down the track.

Cheers
Steve

2 Likes

I am happy to help with my code

1 Like

thank you @PeteKnight
it’s a pleasure

1 Like

Hi @Blynk_Coeur,
Just to close this out, I managed to implement your code into my project. Needed a few small mods but it is very neat. I did add a timeout feature so the the system relocks if it is unlocked and the actuator not pressed after 10 secs. My final code below. I’m sure there are inefficiencies in my coding but the exercise has taught me heaps. Thanks againb.

Steve

 //V50 and V51 styled button
WidgetLED redLed(V1);
WidgetLED greenLed(V2);


/**************** LOCK UNLOCK **************/
BLYNK_WRITE(V6) {  // button press 2'' to unlock
  if (param.asInt() and  !LongPress ) {
    ButtonTimer = timer.setTimeout(2000, LongPressDetect);
    ButtonPressed = true;
   btntimeout = timer2.setTimeout(10000, Buttontimeout);
  } else { // Button released
    ButtonPressed = false;        // Reset the button press flag
       if (!LongPress) {
      timer.deleteTimer(ButtonTimer);  
      // Reset LongPress flag and LED
      LongPress = false;
      greenLed.off();
      redLed.on();
    }
  }
}

void LongPressDetect() {
  // button still depressed
  if (ButtonPressed) {
    LongPress = true;
    greenLed.on();
    redLed.off();
    Blynk.setProperty(V6, "offBackColor", BLYNK_RED);
    Blynk.setProperty(V5, "offLabel", "CLOSED");
    Blynk.setProperty(V5, "onLabel", "OPEN");
    Blynk.setProperty(V6, "offLabel", "Actuate"); 
    }
  }

  void Buttontimeout()
  {
    LongPress = false;   //Reset the long press flag
    ButtonPressed = false;   // Reset the button press flag
    greenLed.off();
    redLed.on();
    Blynk.setProperty(V6, "offLabel", "Press 2s");
    Blynk.setProperty(V6, "offBackColor", BLYNK_GREEN);
    Serial.println("Actuator timeout - reset");
  }
 
BLYNK_WRITE(V5) {  // actuator button
      if (LongPress == true) {
      timer2.deleteTimer(btntimeout);
      digitalWrite(btnpin, param.asInt());  // Sets btnPin HIGH (valve opens) or LOW (valve closes) depending on state of Button Widget
      greenLed.off();
      redLed.on();
     int btnpinstate = param.asInt();  //read the state of the main button
      Serial.println("Button state written to digital pin");
      Serial.print("Activator:");
      Serial.println(LongPress);
      Serial.print("btn pin state:");
      Serial.println(btnpinstate);
      LongPress =  false;    //Reset the long press flag
      ButtonPressed = false;   // Reset the button press flag
      Blynk.setProperty(V6, "offLabel", "Press 2s");
      Blynk.setProperty(V6, "offBackColor", BLYNK_GREEN); 
      }
      
      else
  {
    //If activator button has not been pressed for 2 secs, take no action and reset flags
      Blynk.setProperty(V6, "offLabel", "Press 2s"); 
    Serial.println("No action taken - activator not active");
    Serial.print("Activator:");
      Serial.println(LongPress);
  }
  }
3 Likes

good news.
it’s perfect :stuck_out_tongue_winking_eye:
thank you for sharing !

A post was split to a new topic: I need some secure buttons that must be intentional pushed/activated