Arduino - elapsedMillis ERROR

hi,

having problems with a segment of code involving elapsedMillis,

i have created a time loop so serial out prints every 1 second, this works for 32 seconds and then for some reason the function no longer executes even though it meets the condition,

i have placed serial out to check that indeed the loop is still running and monitor is still function as is. also have logged the time stamps outside the function and it makes sense.. i have a global running time and a time stamp.

ignore the function - BUTTONstate1 im not utilizing when this is running.

see below code, can anyone tell me what is going wrong.

> void B1_ARM() { 
> 
>                                                                                                                  
>                                                                            
>                                                                                                 if(timeElapsed_ARM > (timeStamp_ARM + 1000)){ // serial print out every 4 seconds? // if condition previous code is more than 4 seconds it will run
>                                                                                                 
>                                                                                                           delay(5);
>                                                                                                           Serial.print("Scanning B1_ARM function for Trigger.."); // testing breach point
>                                                                                                           Serial.println();
>                                                                                                           Serial.print("button state: ");
>                                                                                                           Serial.print(BUTTONstate1,DEC);
>                                                                                                           Serial.print(" DAstate: ");
>                                                                                                           Serial.print(DAstate);
>                                                                                                           Serial.print(" breachReset: ");
>                                                                                                           Serial.print(breachReset);
>                                                                                                           Serial.println();
>                                                                                                           Serial.print(" timeStamp_ARM: ");
>                                                                                                           Serial.print(timeStamp_ARM);
> 
>                                                                                                           delay(5);
>                                                                                                           Serial.print(" timeElapsed_ARM ");
>                                                                                                           Serial.print(timeElapsed_ARM);
>                                                                                                           
>                                                                                                           timeStamp_ARM = timeElapsed_ARM; // elasped time has to be more than 4 seconds i.e 4 seconds + >> provides a place holder point in time
> 
>                                                                                                           Serial.println();
>                                                                                                           Serial.print(" timeStamp_ARM");
>                                                                                                           Serial.print(timeStamp_ARM);
>                                                                                                           Serial.println();
>                                                                                                 }
>                                                                                                 Serial.println();
>                                                                                                 Serial.print("outside function");
>                                                                                                 Serial.println();
> 
>                                                                                                  delay(5);
>                                                                                                           Serial.print(" timeElapsed_ARM ");
>                                                                                                           Serial.print(timeElapsed_ARM);
>                                                                                                           Serial.println();
>                                                                                                           Serial.print(" timeStamp_ARM");
>                                                                                                           Serial.print(timeStamp_ARM);
>                                                                                                           
> 
>                                                                                                
>                                             BUTTONstate1 = digitalRead(BUTTON1);
>                                             
>                                             if (BUTTONstate1 == HIGH && breachReset == 0) //
>                                             {
>                                           
>                                               Serial.print("button 01 ARM detected!"); // testing reach point
>                                               Serial.println();
>                                             
>                                               digitalWrite(LED2, HIGH);
>                                               delay(200);
>                                               digitalWrite(LED2, LOW);
>                         
>                                               
>                                               diveSelect(); // call for dive profile type loop
> 
>                                               
> 
>                       
>                     }
>         }

also here is a snippet from serial out

timeStamp_ARM29325Scanning B1_ARM function for Trigger..

button state: 0 DAstate: 0 breachReset: 0

timeStamp_ARM: 29325 timeElapsed_ARM 30457

timeStamp_ARM30462

outside function

timeElapsed_ARM 30525

timeStamp_ARM30462

outside function

..

outside function continues to run

timeElapsed_ARM 35767

timeStamp_ARM32738

outside function

this should trigger function as timeelapsed is more than timestamp .. but does not execute.

I can't read your code in the window, but 32 seconds is diagnostic. An int variable can't hold a number larger than 32767 and then it rolls over to negative. So if you're keeping time in an int you've got trouble at 32 seconds.

Use unsigned long for your time variables.

1 Like

Your code is not complete, and is in a format which is difficult to read, but my crystal ball tells me that some variable should be an unsigned long instead of an int.

1 Like

awesome! that would make sense, I will modify int to long and see if that resolves the problem.

thank you :slight_smile:

or… unsigned long

Time rarely goes negative,

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.