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 › problem with millis();
Page Index Toggle Pages: 1
problem with millis(); (Read 735 times)
problem with millis();
Mar 29th, 2006, 5:16am
 
hi people, im having a problem with the millis() function. i want that after one minute of initializing my applet a new point appears in my applet. i made this code but it doesnt work.. any help would be appreciated:

void setup(){
  size(200,200);
 }

void draw() {
 float m= millis();
  print(m);
if ((m % 6000)==0){
  punto();
  }
}

void punto(){
 point(random(300),random(300));
 }
Re: problem with millis();
Reply #1 - Mar 29th, 2006, 11:01am
 
I can hazard a guess at why it's not working.

You sketch isn't the only thing running on your computer, and processing does some background commands in between the stuff in draw(), and I think only runs draw at the rate your monitor updates at the fastest, so the chances of you calling millis() at the exact milisecond that it's a multiple of 6000 is fairly unlikely I'm afraid.

Also, millis is an integer value not a float.

One thing you could do is see if at least 6000 millis have elapsed since you last draw a point, and trigger on that.

Code:
int lastRun;

void setup()
{
size(200,200);
lastRun=0;
}

void draw()
{
int m=millis();
if(m>=(lastRun+6000))
{
lastRun=m;
punto();
}
}

void punto(){
point(random(300),random(300));
}
Re: problem with millis();
Reply #2 - Mar 29th, 2006, 11:20am
 
Better, keep a count of how many you've drawn.  Otherwise eventually you'll lag slightly (lastRun will almost always be be more than 6000).

Code:


int puntCount;

void setup(){
size(200,200);
lastRun=0;
}

void draw() {
int m = millis();
if(m >= (puntCount+1)*6000) {
punto();
puntCount++;
}
}

void punto(){
point(random(300),random(300));
}



You could also calculate the number of seconds elapsed, from the millis count.  If you divide millis by 1000 then that gives you the number of seconds elapsed, a whole number because millis returns an int.  You still need to remember the last time you drew a point and make sure 6 seconds have elapsed.  That looks like this:

Code:


int lastS = 0;

void setup(){
size(200,200);
}

void draw(){
// get number of seconds elapsed from millis()
int s = millis() / 1000;
if(s == lastS+6)
{
punto();
lastS = s;
}
}

void punto(){
point(random(300),random(300));
}


Re: problem with millis();
Reply #3 - Mar 29th, 2006, 11:22am
 
Oh, the original poster requests something that happens every minute.  So this code needs to deal with 60000 milliseconds, not 6000.

Also, the point is drawn at random(300),random(300) but the screen is only 200x200.  That might confusing things too.
Page Index Toggle Pages: 1