I’m running a Led animation on my InternetButton with a while loop.
Is there a way to end it with a button press?
The only thing that works for me right now is to check in the loop but that means I need to have the button pressed at that certain moment when it’s checked during the animation.
How i can I check for a button press while running the loop?
Thanks for the quick replies.
Its’s just a simple pulsing animation of the leds. But it takes around 6 seconds.
So right now I have to press the button in the exact moment (when the loop ends and starts again) to be able to stop it.
I’ll look into the interrupt thing and see if that’s what I’m looking for.
@HansWurst90, there are ways to do LED animation that are non-blocking. Even with an interrupt, you will still need to sample the flag that the interrupt service routine will set within you animation code. If you can share you code, we can probably advise you on how to make it non-blockiing
@HansWurst90, if I may, I suggest that in doAnimation() you simply set a mode variable that you sample in loop(). So when you get a “breathe” command you can set the “mode” variable to say 3. Then in loop, you can read the mode value, do the action and reset the value so it waits for the next doAnimation command. Add to that a non-blocking version of “breath” that, when activated does the up and down allLedsOn() stuff in steps every 13ms until completed. I’ll pull some code together for you later when I’m at home.
Changed my Code according to your suggestion. You’re right, it’s a little cleaner now.
But I’m still not really getting behind the interrupt thing.
What I forgot to say is, that I want to intercept and break the loop via the button, but also with a function that I call via the cloud API.
Is that possible with an attached interrupt?
The key for that is not to use long running loops (e.g. with delays).
Instead of entering into the loop and staying there till it is finished, you first check if it’s time for a change in state
if (millis() - msLastChange > timeToChange)
doChange() // this should keep track of the states and
// just do stuff without any delay
msLastChange = millis();
and if not you just fall through, do other stuff and come back later.
That would be the basic (“soft delay”) way. To save you some typing, you can use the ElapsedMillis library.
More advanced techniques would involve interrupts which can be handled rather comfortably by using SparkTimerInterval library.
You’ll find some topics on these subjects in this forum.