I noticed timing inconsistencies in my project, and further debugging pinpointed it to the problem below. The text should be updating every second, but the returned values are >> 1000ms.
To clarify, the text is not showing up every second. The delays between texts vary according to fluctuating start and finish values.
I'm using a Teensy 3.2. Changing the baud rate doesn't help.
Is my timing crystal busted? Is there a serial buffering issue? I don't know how to fix either of these.
Any help would be appreciated!
Edit:
Here's the code if anyone wants to test. I found that adding more delay functions screws the output up in completely unlinear ways.
unsigned long start, finished, elapsed;
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println("Start...");
start = millis();
Serial.println(start);
delay(1000);
finished = millis();
Serial.println(finished);
Serial.println("Finished");
elapsed = finished - start;
Serial.print(elapsed);
Serial.println(" milliseconds elapsed");
Serial.println();
}
Edit #2:
I thought it was worth showing everyone this screenshot. It looks like delay(1000)
just isn't functioning at all.
Edit #3:
I debugged it with an LED and it blinked at exactly 1s intervals. It seems like delay(1000)
works, so the problem must be related to the serial buffering. I also updated my software but that didn't help.
Edit #4:
New developments from further debugging:
- The problem is not reproducible on a different computer. The Arduino serial monitor on other computers displays correctly.
- Updating to the latest OSX, arduino IDE, and teensyduino did not help.
- Terminal 'screen' command surprisingly prints correct serial communication.
- Serial monitors from 3rd party apps, like CoolTerm and Arduino IDE do not work.
println
statements together, and likewise the start=/delay/finished=/elapsed= statements, and see if that makes any difference. If so, edit question and say so. Also, it's a good idea to include in your question code that can be cut and pasted (so people can run tests) instead of just a picture of the code.23005 - 11004 = 1000
- as shown in your screenshot.23005 - 11004
is not 1000, so that code couldn't have printed that.Looks like delay just isn't functioning at all.
- on what grounds? You print at the start and end ofloop
so you expect the end time twice, right? Which is what you get. The next time is sometimes 1000 ms later, and sometimes 2000 ms (and once 4000 ms) which is kind of weird.void loop() { start = millis(); delay(1000); finished = millis(); Serial.print("Start "); Serial.println(start); Serial.print("Finished "); Serial.println(finished); }