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.
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.
@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 and enjoy the beach (it is winter here) .
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
No a true geek jumps back to help before going to the beach from home
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.