We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I am in need of some assistance. I'd like to write code that will execute when you click the mouse, and I want the action to run for a maximum of four seconds. If the mouse is still pressed for longer than four seconds, the action will stop. However, I want the action to stop immediately once the mouse is released if the mouse is released after say for example two or three seconds. Also, I'd want this to be repeatable, so that you can do the action as many times as you want. Thank you.
Answers
You can set a timer the moment mouse is pressed :
startTime = millis();
runAction = true;
When the timer is up:
if(millis() - startTime)>= 4000) {
runAction=false;
}
Then, on mouseReleased, reset all your timer variable(s).
Doing this yourself is not hard, but if you want to try using a library instead (more complex, but usually more robust), then consider the timer libraries linked from the Libraries page:
That works, thank you. However, I forgot to mention in the question that I am using the Minim library, and the parameters for audio output require a specified duration. The part that confuses me is how to specify what the duration is going to be when the duration is not predefined but rather defined by the user's inputs.
Oh, ok I think I figured out my problem, I was following the example in the library how they created a new tone instrument in the out.playNote function instead of creating an object with the actual name. However, I can't seem to find the stop function for that. I'm following the example from the reference for creating an instrument, where it shows out.playNote.
out.pause();
Unfortunately that did not work for me for some reason, but I did find an alternative solution. I chose to create an oscilator object, and then patched it when the key was pressed and then unpatched it when the key was released.