I use the micros() function to create a one-second ticker that must remain very consistent. The simplified code looks like this:
This one-second ticker code runs very accurately and consistently, verified by comparing the LED pin rising edge to an accurate, independent one-second pulse on an o-scope.
BUT if I include the "lots of other stuff" in my code (hundreds of lines), the one-second ticker produced by the micros() function slows down by nearly one percent and is inconsistent.
My question: Are there known functions or operations that would affect this timing code? Are there specific operations or functions that I should avoid to improve accuracy and consistency?
Thanks!
oneSec = 1000000;
void loop()
{
do { } while (micros() - secTime <= oneSec); // Kill time until one second is complete
digitalWrite(ledPin, HIGH); // Blink LED.
delay(5);
digitalWrite(ledPin, LOW);
// Do lots of stuff that takes less than one second
secTime = secTime + oneSec;
} // End of Main Loop
void loop()
{
do { } while (micros() - secTime <= oneSec); // Kill time until one second is complete
digitalWrite(ledPin, HIGH); // Blink LED.
delay(5);
digitalWrite(ledPin, LOW);
// Do lots of stuff that takes less than one second
secTime = secTime + oneSec;
} // End of Main Loop
This one-second ticker code runs very accurately and consistently, verified by comparing the LED pin rising edge to an accurate, independent one-second pulse on an o-scope.
BUT if I include the "lots of other stuff" in my code (hundreds of lines), the one-second ticker produced by the micros() function slows down by nearly one percent and is inconsistent.
My question: Are there known functions or operations that would affect this timing code? Are there specific operations or functions that I should avoid to improve accuracy and consistency?
Thanks!