I'm having a piece of code to play a song with a piezo buzzer using Tone library's play function (which includes a bunch of delay calls). Now I want to have a button that can stop the song immediately without having to check the condition to stop after each delay function. Any suggestions? Thanks in advance!
Welcome to the forum
Please post your existing code, using code tags when you do
The obvious one is not to use delay() but to do the timing another way so that a button press can be detected at more or less any time
Search about for things like millis() for timing and take a look at File|Examples|02.Digital|BlinkWithoutDelay.
Thanks for the reply and information.
Thanks. I took a slight look at this example by Arduino and still trying to figure it out how it works. Appreciate the help
Thanks for your detailed examples. Appreciate it
It works exactly the way you would time baking cookies using a wall clock. When you start timingut the cookies in the oven, you look at the clock, and write down the time. Then you come back periodically and look at the clock again, and see how much time has elapsed since the time you put the cookies in. When the elapsed time reaches the baking time for the cookies, you take the cookies out, and turn off the oven. It is that simple.
The beauty of this is that while the cookies are baking, in between your periodic time checks, you can clean the mixing bowl, wipe down the counters, call your mother to check that the baking time really is twenty five minutes and prep and kick off the coffee machine.
If you were using delay, you would just stand motionless waiting for the time to pass.