Skip to content

Is millis() a bit slow? #8

@MCUdude

Description

@MCUdude

Hi!

I was going through some older MicroCore issues when I discovered that PicoCore had an updated version of your millis implementation that compiles down to a smaller size than the one MicroCore uses today.

Before copying your implementation, I decided to test how it performs compared to the one MicroCore uses today.
I've been using a dedicated frequency counter and the code below for testing.

What's interesting is that with picoCore, the period time is 9.432 s, but with MicroCore, it's 7.958 s. I know the internal 128kHz oscillator isn't that accurate but is a deviation like this correct?

// Blink without Delay

// constants won't change:
const long interval = 4096;

void setup() {
  // set the digital pin as output:
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  static uint8_t ledState = LOW;
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(LED_BUILTIN, ledState);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions