Adding time to millis()?

Is there a good way to artificially add time to the millis() value? What I’ve tried so far hasn’t worked.

I have a project that uses millis() for timing, but also disables interrupts often enough that millis() loses significant time. I’m trying to find a way to mitigate that by adding time back to millis() based upon micros(), but I havent had much luck. I’m not looking for 100% millis accuracy, but closer than “not close at all” would be cool. :slight_smile:

It looks like I can call System1MsTick directly, but I’m not having much luck there. Here is my test code:

void setup(){
    delay(10000); // Give myself time to get serial connection started
    
    uint32_t startMillis = millis();
    delay(10);
    Serial.println(String(millis()) + " " + String(millis()-startMillis));
   
    for(int i = 0; i < 1000; ++i) {
        System1MsTick();
    }
    
    Serial.println(String(millis()) + " " + String(millis()-startMillis));
}

Hammering System1MsTick seems to have no effect at all. Is there a better way to do this?

a good read on millis() and micros() and stopping interrupts.

I’ve done this with Arduino to calibrate the clock depending on temperature:
Off the top of my head:

x=somevalue;
d=1000; //1 sec
n++;
if (n%x==0) d++; //or d–
delay(d);

I believe the fix for millis() running slow is a relatively easy one - I’ll try to get that in for 0.5.0

1 Like

Ha, yeah, it’d definitely be nicer if millis didnt run slow in the first place. But I’m also fine with fudging it if it’d let me. :slight_smile: