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 › Your best metronome
Page Index Toggle Pages: 1
Your best metronome (Read 1311 times)
Your best metronome
Jun 7th, 2010, 12:21pm
 
Hi,

I'd like to know how you guys do a metronome.
I had a pretty simple idea 2 or 3 months ago that was something like this :

Code:

 int counter1 = 0;
 int ellipseSize;

void setup() {
  size(256,256);
  frameRate(30);
  smooth();
  noStroke();
}

void draw() {
 background(255);
 ellipse(width/2,height/2,ellipseSize,ellipseSize);
 counter1 = (counter1 + 1)%15;
  if (counter1 == 1) {
   fill(255,0,0,200);
    ellipseSize = 40;
 } else {
   fill(0);
   ellipseSize = 10;
 }
 
 if (ellipseSize > 30) {
   println("Nb Millis : " + millis());
 }
}



I'ts pretty stable, well, more than millis. If you have other ideas just sahre them here!
Re: Your best metronome
Reply #1 - Jun 7th, 2010, 2:51pm
 
I wouldn't count on frameRate-based stuff for timing code.  You aren't setting the absolute frame rate in setup() -- what the frameRate(n) command does is set the maximum FPS.  It can easily fluxuate if a user starts another CPU-intensive process, or stutter during garbage collection.  I usually set frameRate to 99999 to break the built-in limit of 60 FPS, and instead rely on millis():

Code:
int lastCheck; // global int
lastCheck=0; // the last line in your setup() function

void draw(){
while ((millis()-lastCheck)>=10){ // 10 here is the 'granularity'
  lastCheck+=10;
  update(); // update counters
}
render(): // draw the circle at the appropriate size
}

Page Index Toggle Pages: 1