Timer Project

Status
Not open for further replies.

mtiger

Well-known member
Hi All,

Wondering if someone can help demystify the inaccurate timing aspect of my project.

The project is just a simple timer, with an encoder incrementing or decrementing the rate in which 0v - 5v (HIGH / LOW) signal is outputted, and the use of a WS2182B (neoPixel) LED for output pulses.

Using the elapsedMillis library, I've set up a typical timer . I'm also, reading an encoder function, to check when the encoder has changed position using this library. (https://github.com/mathertel/RotaryEncoder)

Now, the timer starts to drift after 30 seconds or so, and I initially thought it could be the encoder or neoPixel timing causing the issue. So I removed both of those variables and used the onboard LED (pin 13) to represent the pulse output of the timer. It still drifts....

I'm referencing the pulse using Ableton Live Metronome, and haven't come across any issues with the Inbuilt Metronome having inaccuracy issues.

Any ideas as to how to correct this? What could be causing the drift for such a trivial task?

What would you recommend for accurate timing, using an encoder and WS2812B LED for visual output?

Thanks in advance,
M

Code:
elapsedMillis timer; 
int clock;


void setup() {
  pinMode(13, OUTPUT);

}

void loop() {
  if (timer > 500) {
    clock = !clock;

    digitalWrite(13, clock);

    timer = 0;
    
  }

}
 
Last edited:
Any ideas as to how to correct this? What could be causing the drift for such a trivial task?

2 possible issues.

1: You're testing for > 500, but maybe you meant >= 500? Your code will execute every 501 ms.

2: You're setting the timer to zero. For a very simple program like this, maybe not an issue. But what happens if something else took time and the timer increments before the "timer = 0" line? Then you'll lose 1 ms. Usually the way to get a reliable interval is to subtract 501 (or 500, if you fix #1) rather than set it to zero. Then if it happens to have incremented unexpectedly, you won't lose that info.
 
Status
Not open for further replies.
Back
Top