millis() is not increment continuously when sketch starts

Hi all, I have a very simple test that ran to dump the millis() value shown below, however when I ran it, I noticed the millis() value started off from zero, it incremented for a bit, and then went back and started from zero again. Is it because the sketch program reset itself? If so, can anyone tell me why?

0 413 1419 1425 1431 1437 1443 1449 1457 1463 1469 1475 1481 1487 1494 1500 1506 1512 1518 1525 153�00 1406 1413 1419 1425 1431 1437 1443 1449 1457 1463 1469 1475 1481 1487 1494 1500 1506 1512 1518 1525 153�0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 4 7 10 14 18 22 26

void setup() { // put your setup code here, to run once: Serial.begin(9600); }

void loop() { // put your main code here, to run repeatedly: Serial.println(millis()); }

Are you using Arduino IDE 1.6.1 ? And did you not change the standard libraries ?

I'm running your test sketch on a Arduino Uno for a few minutes now. It is normally incrementing all the time.

Why do you have weird characters between the numbers ? Do you have a bad connection ? Perhaps your Arduino board is resetting. You can print a message in setup() to know when the Arduino did reset:

void setup() {
  Serial.begin(9600);
  Serial.println("\nStarted");
}

void loop() {
  unsigned long currentMillis = millis();
  Serial.println( currentMillis);
  delay(100);        // slow down the output to serial monitor
}

Thanks for the response, I ran your code again on my mega board under Ubuntu 12.04 with Arduino IDE 1.6.1, and it shows the same symptom. I don’t know why there are funny characters, or character get dropped.

St
199
300
400
500
600
700
800
901
1001
1101
1202
1301
1401

Started
0
99
199
300
400
500
600
700
800
901
1001
1101
1202
1301
1401
1502

It is resetting.
Perhaps a power issue ? Do you use a very bad usb cable ? or a hub that is not a powered hub ? or a laptop that has weak usb power ?

When you have a wall wart of 7.5 to 12V, you can power the Arduino using the power jack. Can you measure the 5V pin of the Arduino. It should be above 4.5V.

What operating system? There can be issues with impolite software probing serial connections they don't own, which can cause a reset.

wyung: Thanks for the response, I ran your code again on my mega board under Ubuntu 12.04 with Arduino IDE 1.6.1, and it shows the same symptom. I don't know why there are funny characters, or character get dropped.

To find out whether your board is resetting itself, you can print a message within the setup() function. Using such a startup message will show you exactly when the sketch restarts.

void setup() {
  Serial.begin(9600);
  Serial.println("Good night and good luck!");
}

void loop() {
  Serial.println(millis());
}

Besides of that your program seems to be running much too slow. At 9600 Baud you will get 960 characters per second, and if printing 3-digit millis() numbers and CR/LF at the end of the line, you should see one milliseconds output every five milliseconds, such like:

101
106
111
116
121
128
133
138
143
148
153
158
163
168
174
179
184
189
195
200
205
210
216
221
226
231
236

If your code runs much slower, then either your Arduino is not running at the correct speed or your PC is swallowing characters while receiving them.

Would you think that it might be possible that the serial input buffer of your PC is overrun so that characters get lost during transmission?

In that case test with a small delay in the loop() code such like:

void loop() {
  Serial.println(millis());
  delay(100);
}

What about the serial output then?