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.
Page Index Toggle Pages: 1
timer (Read 1155 times)
timer
Dec 2nd, 2009, 5:07pm
 
I made a program that grabs data from the web and then draws an image using the data. The data is being loaded in void setup so I can use it everywhere in the code, not just draw, I want to update the data grabbing and the void draw every 5 minutes but I cannot just put a timer around the whole code. what is the best thing to do?
Re: timer
Reply #1 - Dec 2nd, 2009, 6:29pm
 
I'm pretty much just asking how to run void setup more than once
Re: timer
Reply #2 - Dec 3rd, 2009, 4:10am
 
Thundarr wrote on Dec 2nd, 2009, 6:29pm:
I'm pretty much just asking how to run void setup more than once

Not the right solution...

I often say: "Don't load data in draw(), except in some cases".
Well, that's precisely one of these cases.
But of course, unlike most snippets I saw, we won't load data on each frame. Just using the good old "elapsed time" pattern.
Code:
// Declare a global time tracking variable
long timeSinceLastEvent;
long DELAY_BETWEEN_EVENTS = 5 * 1000; // 5s

void setup()
{
// Usual setup stuff, size() and al
// Record starting time
timeSinceLastEvent = millis();
}

void draw()
{
if (millis() - timeSinceLastEvent > DELAY_BETWEEN_EVENTS)
{
DoSomething();
timeSinceLastEvent = millis();
}
}

You can also do something every n frameCount frames, but it is less reliable (you can change frameRate).
Re: timer
Reply #3 - Dec 3rd, 2009, 2:00pm
 
Thanks, yes that does work for part of it, but then the data is only located in void draw, and void mousepressed cannot access the data, any solution for that? of maybe load the data in mouseclicked too?
Re: timer
Reply #4 - Dec 3rd, 2009, 2:07pm
 
also declare a global variable/array for your data ( dont know what kind of data, depending on what you load) so you can access and update it from anywhere.
Re: timer
Reply #5 - Dec 3rd, 2009, 6:58pm
 
thanks got it
Page Index Toggle Pages: 1