[SOLVED] Virtual button pressed for X seconds

Hey all. I need help with something. I want to press a virtual button and, after a certain amount of time, execute an action. For some reason, the action is executed immediately and not after the specified time. Here is my current code:

BLYNK_WRITE(V9) {
  mashButton = param.asInt();
  while (mashButton = 1) {
    unsigned long mashButton_time = millis();
    if ( millis() > (mashButton_time + mashButton_delay)) {
      tone(alarm, 2000);
    }
  }
}

The variables mashButton and mashButton_delay are global ones of the int type. The only thing in my loop function is Blynk.run(); Any help is greatly appreciated.

1 Like

This should be while (mashButton == 1) { with two == signs

https://www.arduino.cc/en/Reference/If

1 Like

Thanks, Gunner. Good catch. Itā€™s still not working though. And, if I hold the button for long enough, I get disconnected from the server.

I donā€™t know what this isā€¦ a void or command?

Sounds like it is staying in the while loop regardless of your mills comparisonā€¦ but even if it reaches the time limit (you may need to put some debug prints in there to see where the looping starts and ends), what breaks it out of the while loop? The aforementioned tone()?

I guess nothing breaks it out of that loop. I ultimately want the tone to be a separate function with its own process. I just happen to have a piezo buzzer on my circuit so I was using it to test the code. Iā€™ll add something to break out of the while loop once I get the button delay part working.

EDIT: Sorry, didnā€™t see the first part of your comment. The tone(alarm, 2000) is just a command that tells the buzzer (alarm) to buzz at the frequency 2000.

Actually exiting the loop is the answer to the delay part. Make a void called Alarm or something and use that to exit.

BLYNK_WRITE(V9) {
  mashButton == param.asInt();
  while (mashButton = 1) {
    unsigned long mashButton_time = millis();
    if ( millis() > (mashButton_time + mashButton_delay)) {
      Alarm();
    }
  }
}

void Alarm() {
tone(alarm, 2000);
}

I have 3 min before I am leaving work for the long weekendā€¦

Here it goes:

BLYNK_WRITE(PIN){
  if(param.asInt()){
    // button pressed
    newTimer = timer.setTimeout(1000,newTimerCallback)
  } else {
    timer.disable(newTimer);
  }
}


void newTimerCallback(){
  // perform your "held" button action. 
}

There may be another way to do it but this works for me.
You can set the timeout longer/shorterā€¦ i just use 1s.

2 Likes

But how will we ever get that terminal debug working if you are off enjoying the real world? :cry:

Iā€™ll wait for you to post your working code while iā€™m at the beach :sunglasses: :sunglasses: :sunglasses:

2 Likes

Iā€™ll let you know as soon as I figure out why your code snippet isnā€™t compiling. Have fun out there.

Prob because I missed a closing parentheses }

Try it again

@Jamin am Doesnā€™t your snippit ā€œdo somethingā€™ā€ for the timer duration? I think he wants it to wait for x time, while holding button, before committing to doing something.

Either wayā€¦ go home :smile: and enjoy the beach (it is winter here) .

I donā€™t think you did. Itā€™s giving me:

ā€˜newTimerā€™ was not declared in this scope

And then when I fix that, it gives me an error pertaining to the function newTimerCallback.

@Jamin go enjoy the beach. Iā€™ll figure this out (hopefully with some help from @Gunner and @Costas).

He only gave you a snippetā€¦ there is probably more supporting code required to get SimpleTimer working.

EDITED - I think this is the correct reference Arduino Playground - HomePage

Stuck in trafficā€¦ .so a chance to reply.

the snippet just starts a timeout of 1sā€¦ if you let go of the button before the timeout is complete, then the timer is disabled and never completes.

If you do hold it for longer than 1 s (before feeding a FALSE/LOW/0 back to the button) then the callback is actionā€™dā€¦ which will do the code he requires to run after a certain duration

Ahh, a true geek :wink:

Got it on your snippet :+1:ā€¦ I have to compile and play around with code to determine its flowā€¦ canā€™t rely on just looking at it :confounded:

Thanks for the help yā€™all. I have no clue how SimpleTimer works so Ima do some research now.

1 Like

@Gunner Do you think itā€™ll help if I update my arduino IDE? Iā€™m a couple versions back.

No a true geek jumps back to help before going to the beach from home :sweat_smile:

Either 1.6.9 or 1.6.12 are golden versions. I use 1.6.9 personally.

Easiest way to learn is use the PushData example provided by Blynk in the IDE.

Its has the basic usage which will update a value widget every 1000ms to show the uptime of the device.

You can then go and manipulate the timers by assigning it a global var (in my example you would need to declare int newTimer; at the top of the scope).

Then you can throw around commands like timer.disable(newTimer), timer.enable(newTimer), timer.toggle(newTimer) etc

Also study the differences between setTimeout() and setInterval().

setTimeout() is VERY VERY handy for performing 1 off tasks via a timer.