Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Timer Project

  1. #1

    Timer Project

    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 by mtiger; 01-06-2019 at 01:08 AM.

  2. #2
    Anyone? Tested this with a simple handheld clock and it drifts also.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,006
    Quote Originally Posted by mtiger View Post
    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.

  4. #4
    Thanks Paul. That was a doozy error on my part. Everything is working fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •