it is a little strange that inside the delta type timer ( timeout ) unconditionally set flag =1 I think that this is unwonted but I am at work now with 0 free time to think deeper about…
the flag is after timeout, not inside
you must add this to detect release before 1 sec
// Button V6 has been released
else {
flag= 0; // Reset the button press flag
timer.deleteTimer(T1); // end timer
}
…better now
I am at work, so I can not try if my code is correct
Hi all,
Many thanks for the thoughts on this.
@PeteKnight, the logic you described is exactly what I have in my head. My challenge has been converting this to code
@Blynk_Coeur I really appreciate you taking the time to put some code together. From what I can understand, the activator button has to be pressed for 1 sec to make it active and then then pressing the actuator button (while still holding the activator button down) will result in an action. Is this correct? Could this be simplified? For me, the chances that both buttons are pressed simultaneously are pretty low. The logic I was thinking of was (setting the activator button to push mode in Blynk) was that, if the activator button state = 1 (ie pressed) then write the state of the actuator button to the digital pin, otherwise the activator button state must be zero and do nothing. Would this work and how would it be coded?
I’ll give your code a try and see how I go.
Cheers
Steve
Here’s a bit of pseudo code that may help get you started:
// Two buttons attached to virtual pins: Activator button = V10 and Main Button = V20. 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(V10) // Automatically triggered when Activator button is pressed
{
int V10_Button_Value = param.asInt(); // will be 1 if button pressed, 0 if button released
if(V10_Button_Value ==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(V20) // Automatically triggered when Main button is pressed
{
int V20_Button_Value = param.asInt(); // will be 1 if button pressed, 0 if button released
if (Active_Flag) // This next piece of code will only run if the Active_Flag == true
{
if(V20_Button_Value ==1)
{
// Do the stuff in here that you want to run when the main button is pressed (On)
}
else
{
// Do the stuff in here that you want to run when the main button is released (Off)
}
}
}
I’ve not compiled and tested this, so it may be full of syntax errors!
Pete.
HI @PeteKnight ,
Many thanks for the code. Between yours and @Blynk_Coeur, I think I am starting to understand how this works. I’ll have a play on the weekend and see how it goes.
Cheers
Steve
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
.
.
.
.
.
press 2 sec to UNLOCK
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
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
Pete.
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 .
I agree that Alexis’ code does have some nice features - I will try and implement this down the track.
Cheers
Steve
I am happy to help with my code
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);
}
}
good news.
it’s perfect
thank you for sharing !
A post was split to a new topic: I need some secure buttons that must be intentional pushed/activated