Using TimerTask or another Thread for this is a daft idea that’s just going to tie the OP in knots trying to get things to synchronize. Please note the “I’m new to Processing” - consider half the seasoned developers on here can’t do multi-threaded programming right!
There are a few things wrong with the initial code, but not that can’t easily be fixed. Try -
int c = 0;
float bpm = 120;
long interval = (long) (1e9 / (bpm / 60));
long nextTime;
void setup() {
size(600, 600);
nextTime = System.nanoTime() + interval;
}
void draw() {
background(0);
if(System.nanoTime() >= nextTime) {
nextTime = nextTime + interval;
c = 255;
}
if (c > 0) {
fill(c);
ellipse(width/2, height/2, height/2 + 0.016*height, height/2 + 0.016*height);
c -= 30;
}
}
Note
- Don’t use floats for time - keep it in longs
- Make sure you follow @MxFxM advice about time >= nextTime, but you don’t need temp, because …
- To get fixed rate you need to add the interval to your previous nextTime, not the current time, otherwise you accumulate errors.
- I needed to add the
if (c > 0)
section to animate down the fill colour, or sometimes the default renderer output doesn’t show for me - seems some calls todraw()
don’t make it on screen! (I repurposed your count variable for this )