We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › delays separate from main loop
Page Index Toggle Pages: 1
delays separate from main loop? (Read 350 times)
delays separate from main loop?
Mar 1st, 2008, 4:53am
 
Hey there,

I am wondering if there's a way to have two loops running in Processing with independent timing. Here are the specifics of my application:

- the program is continuously reading data from serial port (works fine)

- while data matches certain parameters, trigger a sound that repeats every few seconds

- for different values of incoming data, trigger sound at different rates. (I can do this with switch/case pretty easily).

I'm using the minim library. I have the trigger set up fine; it's getting the sound to repeat properly that isn't working. If I put in a delay(1000) then the whole program pauses for one second, which isn't acceptable. Is there a way that I can sort of have one function running on its own loop while the whole program keeps going?

Here's what I have now, pretty simple -- all the math and stuff leading up to getting datapoint works fine
Code:

// if datapoint is a larger number than (average of last samples plus 5% = 1.05 f) then trigger the sound

if ( datapoint > 1.05 * f) {
print("EDGE DETECT at ");
print(datapoint);
print(" : ");
println(f);
beep.trigger();
delay(1000);
}

this pauses the sketch for 1 second, obviously. I need to keep the rest of it running and just pause the audio loop bit.

I tried to make it a function by replacing the last two lines with playBeep(); and making this simple function
Code:

void playBeep() {
beep.trigger();
delay(1000);
}

That does the same thing.

So, any way I can keep this delay separate from the execution of the main sketch? I also can't write the delay into the sound file because of the nature of the trigger() function in minim.

Thanks all.

CUID
Re: delays separate from main loop?
Reply #1 - Mar 1st, 2008, 12:40pm
 
To handle your sounds separatly from the main app, you need a Thread. Check this good tutorial :
http://www.shiffman.net/teaching/a2z/threads/
Page Index Toggle Pages: 1