Why its not 1,000ms but arround 1,000.01ms


unsigned long myTime = 0;
unsigned long myInterval = 1000;

void setup() {

  Serial.begin (9600);
}

void loop() {  

  if ( millis () - myTime >= myInterval) {
    myTime = millis ();
 
  analogRead (A0);
  Serial.println (analogRead (A0));
  }
  
}

the input not exactly every 1000ms

i am using uno

Try:
myTime = myTime + myInterval;


Also, you are dealing with a low grade ceramic oscillator on the Arduino.

2 Likes
myTime = millis ();

Should be

myTime += myInterval;

this one not work...

Not work in what way ?

this one not work.. the timer keep adding arround 100ms at every 1 minutes

thanks larry.. maybe because of the low grade ceramic oscilator.. that error cant repair

DO NOT post pictures of Serial monitor output. Copy and paste the output here in code tags. This makes it so much easier to read and examine in detail

1 Like

I'm not sure what you used to measure at 10ns precision

So you are looking at accuracy and precision for the clock. The difference between 1000ms and 1000.01ms is 0,001%.. Clearly beyond the low grade ceramic oscilator that's builtin

You could try with micro and a faster baud rate see if you get better results

unsigned long lastTime = 0;
const unsigned long myInterval = 1000000ul;

void setup() {
  Serial.begin (115200); // don't go slow. interrupts do have impact. even go for 1000000 or 2000000 
}

void loop() {  
  uint32_t now = micros();
  if ( now - lastTime >= myInterval) {
    lastTime += myInterval;
    Serial.println (analogRead (A0));
  }
}

same result here...
i want to use the motor stepper to follow earth rotation, arround 4,300,800 step for 1 full rotation for 86,124 minutes + 0.916 seconds

i think just reduce the speed arround 0.001%

and your physical gears are precise to 0.001%?

my Gear designed for 1 rotation every 4,300,800 step.. if use less step for 1 rotation, maybe the image not good for star tracking.. not try yet

the worm gear 96:1
the gear 3.5:1
the motor 400:1
the microstepper 32:1

DS3231 real time clock module generating a 1 Hz square wave (or interrupt) would keep more accurate time.

it takes about 0.1 ms to read an analog input and OP is doing it twice, so I wonder what's the purpose of being that precise in the first place anyway...

telescope for star tracking... but i think i found another way