Any ideas how to tie sound duration to button press?

I have a scenario where I'd like the sound command duration parameter to be tied to the duration of a button press, so button pressed and held and sound until button released, the button presses will be from a few milliseconds to a couple of seconds, so given that the duration command is in multiples of 10ms then I guess 10ms will be fine for the minimum duration and 2.5 second will be the maximum.

I am not sure if this is possible but if anyone has any ideas then please post an example of how it might be achieved, thanks.
 

goom

Senior Member
One possibility would be to use the initial button press to trigger an interrupt. In the interrupt routine:
Reset the variable used to count the time
Wait for a short time to allow for any switch bounce
Enter a loop, incrementing a variable, pausing for 10ms and checking for button release within this loop. On detecting a button press, exit the loop and the variable will contain the number of 10ms intervals.
Reset the interrupt
On returning to the main program, you can then use the variable to schedule the appropriate sound.
 
Top