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 › incrementing through an array over time.
Page Index Toggle Pages: 1
incrementing through an array over time. (Read 645 times)
incrementing through an array over time.
Oct 4th, 2009, 7:52pm
 
I'm having a bit of a brain fart here and failing at programming 101...

I have an array (say somewhere between 30 and 100 items), and I want to step through it gradually, over the course or a minute or two. That is, I will increment a pointer variable (v) for myArray[v], so that things looking up a value in myArray will see the same values until V is moved.

I want to be able to set a specific time (accurate within a few seconds) for processing to step through the array (increase v). Since v obviously must be an integer (and for my purposes i cannot interpolate, and don't want to), how do I figure out how many frames to wait between increments?

something like
Code:
framesBetweenIncrement = (timeInSeconds*frameRate)/lengthOfArray;  


seems obvious, but it always runs way quicker than desired.

Any hints?
Re: incrementing through an array over time.
Reply #1 - Oct 4th, 2009, 9:43pm
 
Let me also point out the obvious problem here:

If I want my index of my array with 79 items to take 60 seconds to go from start to finish, it looks something like:

(60 * 30) / 79 = 22.7848101 (frame to wait in between incrementing the index value)

The problem is, this needs to be an int number of frames. Rounding down makes it run a little too fast, and rounding up makes it run slow.

I dont mind if the speed isn't perfectly consistent throughout, I just want it to finish in the right amount of time overall.

how can I force it to catch up / wait for the right amount of time?  Cry
Re: incrementing through an array over time.
Reply #2 - Oct 5th, 2009, 9:17am
 
frameRate isn't really useful for timing, because it's always changing.  Even if you set the frameRate using frameRate( n ), you're just setting the maximum.  You want millis(), which returns the milliseconds the program's been running.
So,

if (millis() - lastCheck >= timeInterval){
 doStuff;
 lastCheck = millis();
}

--Ben
Page Index Toggle Pages: 1