# Inaccurate time intervals for a Metronome

**URL:** <https://discourse.processing.org/t/inaccurate-time-intervals-for-a-metronome/2463>\
**Category:** Processing\
**Created:** [August 8, 2018, 4:48am UTC](https://discourse.processing.org/t/inaccurate-time-intervals-for-a-metronome/2463 "2018-08-08T04:48:25Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 8, 2018, 5:10pm UTC](https://discourse.processing.org/t/inaccurate-time-intervals-for-a-metronome/2463/10 "2018-08-08T17:10:55Z")

</div>

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 -

```java
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 to `draw()` don’t make it on screen! (I repurposed your count variable for this 😄 )

---

_[View the full topic](https://discourse.processing.org/t/inaccurate-time-intervals-for-a-metronome/2463)._
