I found the Arduino Uno board could not output data with the same time span, which means each loop takes different time. I posted the time intervals of my code, and it shows there are three main different time spans which are randomly distributed (the y axis represents time inverval in seconds, x axis is the sample number ). I've checked several Arduino boards, they all have the same acts. Does anyone know how to solve it? How to make every loop really run the same time?? Thanks!
It's timing on millis(). And this is a serious issue that If I want to use if command to make the code do something when the time is 1 x looptime, 2 x looptime,3 x looptime,...n x looptime...
I am sorry, I am new to this forum and don't know how to post a code (I just attached it). I just define the now == millis() at the beginning of the loop and Serial.print it. I think it's probably the hardware issue, just make sure if it's solvable. Thanks!
What baud rate are you using for the serial connection? Serial is slow relative to most other things on the microcontroller (19200 baud is about 2k/second) - it's got a 64-byte output buffer, but when that finishes, writing to the serial port becomes blocking, and so your sketch winds up waiting for stuff to send via the serial port most of the time.
i still guess it is just the way you measure meaning resolution. Why is a year not always 365 days? because the loop time of the earth has a fraction of a day in it. So your loop is likely to have fractions of millis - ok now?
DrAzzy:
What baud rate are you using for the serial connection? Serial is slow relative to most other things on the microcontroller (19200 baud is about 2k/second) - it's got a 64-byte output buffer, but when that finishes, writing to the serial port becomes blocking, and so your sketch winds up waiting for stuff to send via the serial port most of the time.
papa_avr:
i still guess it is just the way you measure meaning resolution. Why is a year not always 365 days? because the loop time of the earth has a fraction of a day in it. So your loop is likely to have fractions of millis - ok now?
Thanks! I am a beginner of Arduino, the explanation makes sense, I just need much more time to digest it. Do you know how to solve it? If I want to run the PID control every loop time, it seems hard to set the PID sample time, i.e. If I want PID run every 100 ms, life is easier if the loop time happens to be 10ms or 20ms etc. Is it possible to measure the real loop time and make sure it does not change?
Or, I should ask is it possible to maintain the loop time to be same? If the original loop takes 94ms,95ms and 96ms, how to make the loop starts over every 100ms? Add some delay()?