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 & HelpPrograms › start event every second
Page Index Toggle Pages: 1
start event every second (Read 1670 times)
start event every second
Feb 21st, 2010, 10:00pm
 
What is actually the best, most accurate, most reliable way to start an event every second.
I know using frameCount is not good, but i also cant use millis() cause millis%1000 doesnt work cause they only get updated in draw...
any ideas ?
Re: start event every second
Reply #1 - Feb 21st, 2010, 10:26pm
 
Why isn't it acceptable to check millis() during draw()  That, combined achieving the highest framerate possible (by breaking the built-in limit of 60 FPS), is the only way that occurs to me...

edit: there's also Java's CurrentTimeMillis():

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#currentTimeMillis(...

Which, if I recall correctly, measures absolute time rather than time since the applet started, and might update differently ()
Re: start event every second
Reply #2 - Feb 21st, 2010, 10:27pm
 
Quote:
cause they only get updated in draw...

millis() is updated independently of draw(), but you can check it only if draw() is there (either checking in draw or needing draw to activate keyboard/mouse events where you can also check it).
Now, indeed, that's only in draw() that you can check it reliably, since that's the heart beat of your sketch. In general, it is called more often than 1s, so it can work.

But I wouldn't use millis() % 1000 to check one second, indeed, because draw() isn't called that often. You have to use the good old (millis() - timeSinceLastEvent >= 1000) pattern. So you get a precision equals to the current frame rate, which isn't so bad.
Do you really need super precise time beating (ie. below 17ms precision for the default 60FPS)?

Another solution to try is to spawn another thread, making it sleep for 1s, then calling the method of your choice. Although you can get a drift if that method takes time to process... Or the sleep time must be adjusted to clock time.
Re: start event every second
Reply #3 - Feb 21st, 2010, 10:33pm
 
One option, that does use another thread, is a Timer.
This link shows two forms (Swing or java.util)
http://www.rgagnon.com/javadetails/java-0144.html
Re: start event every second
Reply #4 - Feb 21st, 2010, 10:34pm
 
Actually, strictly speaking, one could definitely achieve a 1000-milli countdown between two discrete events within a single function, using delay(1000), right?  It wouldn't work for visual events, because draw() is necessary, but maybe a "tick" sound could be played.  I also am not sure what would happen if you started a separate thread with a delay(1000); in it, and used it to increment a counter...

edit: Oh, I guess that is basically what the class NoahBuddy mentioned does.
Re: start event every second
Reply #5 - Feb 21st, 2010, 10:59pm
 
i believe what PhiLho mentioned seems to be good enough. The rest seems to be more accurate but even more complicated.
so what exactly is the "good old millis() - timeSinceLastEvent >= 1000" ? cause i havent heard of it Smiley

oh and btw, 4 answers this early in the morning within an hour.Big Thx!
Re: start event every second
Reply #6 - Feb 21st, 2010, 11:03pm
 
int lastTime = 0;

void draw(){
if( millis() - lastTime >= 1000){
doEvent();
lastTime = millis();
}
}
Re: start event every second
Reply #7 - Feb 21st, 2010, 11:06pm
 
Quote:
i believe what PhiLho mentioned seems to be good enough.
Which one? Smiley
Quote:
this early in the morning
For you (and me), might be late for some other forum users... Smiley
Re: start event every second
Reply #8 - Feb 21st, 2010, 11:09pm
 
questioned answered i guess...
so what TfGuy44 wrote is better than the

frameRate(60);
if(frameRate%60==0)

i used before. right?
Re: start event every second
Reply #9 - Feb 22nd, 2010, 12:40am
 
What I wrote is the "good old millis() - timeSinceLastEvent >= 1000".
And yes, it is better than that.
Re: start event every second
Reply #10 - Feb 22nd, 2010, 1:36am
 
Quote:
so what TfGuy44 wrote is better than the

frameRate(60);
if(frameRate%60==0)

i used before. right?

Certainly, since you should use frameCount instead!  Wink
Which is OK for rough control but not for precise control (as some frames might be delayed by long computations).
Re: start event every second
Reply #11 - Feb 22nd, 2010, 3:34am
 
ahh yes, sure Smiley frameCount, hehe... ok i use your example then. Thanks!
Page Index Toggle Pages: 1