Micros Rollover

Hi all, I've looked at a bunch of stuff with how to deal with the micros() rollover, but it seems that in everybody else's the Arduino rolls back to zero but then starts counting again from there, which would not be problematic for my purposes. Mine is resetting to zero but then just printing out zeros anytime millis() is used for the remainder of the time that the code is run. I have it as an unsigned long variable. (Also a note: it rolls over after about 35 minutes instead of 70, so I don't know if I should have something else changed to fix that). Thanks for any help!

Help us to help you by posting the (full) code; and please use code tags.

You won't have any problem with rollover of millis() or micros if you use it like in the demo Several Things at a Time

...R

millis() overflow . . . a bad thing

http://www.gammon.com.au/forum/?id=12127

.

The problem is on line **. You are using a ___ when you should have a ?? there.

Here’s parts of the code that deals with timing (the whole thing is too long to post)

unsigned long Time;

void setup()
{
 Serial.begin( 115200 ); // Initialize the USB serial connection with the PC at 115200 baud
 if (Usb.Init() == -1) { // Try to initialize the USB host
   Serial.println("OSC did not start."); // Dump error message over serial if error
 }
 delay(200); // Wait for the USB host
 if (!Hid.SetReportParser(0, &Joy)) { // Try to set the human interface device report parser
   ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1  ); // Dump error message over serial if error
 }
 establishContact();
}

void loop()
{
 if (Serial.available() > 0) { // If bytes are available to be read in the USB Serial port buffer
   CommandByte = Serial.read(); // Read one byte (the operation code)
   switch (CommandByte) { // Determine which op code was read
     case 60: Serial.println("5"); break; // If the op code was char(60), this is a handshake. Return handshake with string(5).
     case 61: { // If the op code was char(61), the following bytes are trackball task variables to set.
         while (Serial.available() == 0) {} // Make sure a byte is available in the buffer
         if (firstTrial == true)
         {
           Serial.println(micros());
         }
         delay(10);
         Command = Serial.read();
         Serial.println("250");
       } break;
     case 62: { // If the op code was char(62), start a new trial.
         Usb.Task(); // Run the USB polling script
         StartTime = micros(); // Log the start-time of the ball idle period
         Serial.println(StartTime);
         BallPosition = 0; // Zero the trackball position
         RecordIndex = 0; // Set the number of position measurements taken to 0
         LastPos = 0; // Set the last position recorded to 0
         TrialPhase = 1; // Proceed to trial phase 1: Ball Idle.
       } break;
   }
 }
 if (TrialPhase == 1) { // If the current trial phase is Ball Idle
   Usb.Task(); // Run the USB polling script
   CurrentTime = micros(); // Log the current time
   if (BallPosition != 0) { // If the ball position has moved from 0
     StartTime = CurrentTime; // Reset the Idle start timer to the current time
     BallPosition = 0; // Zero the ball position
   }
   ElapsedTime = CurrentTime - StartTime; // Calculate the time elapsed between the beginning of the idle period and the current time
   if (ElapsedTime > BallIdlePeriod) { // If the elapsed time exceeds the idle period
     TrialPhase = 2; // Proceed to trial phase 2: Wait for threshold crossing
     StartTime = micros(); // Log the start-time of the choice period
     Serial.println(StartTime);
     Serial.println("260");
   }
 }

Once again I can deal with the numbers fine if they restarted at 0 and counted up again, but they just print out as 0 for all the times after the rollover period.

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

I have no doubt the bug is in your code and that the bug is in code you did not post.

I've looked at a bunch of stuff with how to deal with the micros() rollover, but it seems that in everybody else's the Arduino rolls back to zero but then starts counting again from there, which would not be problematic for my purposes. Mine is resetting to zero but then just printing out zeros anytime millis() is used for the remainder of the time that the code is run.

Are you mixing up millis and micros in your code? Where is StartTime defined? Are you assigning to it anywhere else?

lewisucd: Here's parts of the code that deals with timing (the whole thing is too long to post)

The code that you posted does not use millis()

sterretje: Help us to help you by posting the (full) code; and please use code tags.

Maybe I should not have used (full) but full.

What that means is that you either post full code or that you post a short example (that compiles without errors) that exhibits the behaviour.