Trouble using millis() in a long loop

Hi everyone !

I have some troubles using the millis() function in my code. I created an objet which contains some arrays and variables and I’d like to use a lot of these objects in a sequence (I mean from 0ms to 5000ms something happens with these objects, from 5000ms to 10000ms something else happens, …).
In order to know the current time, I use the millis() functiun. It seems to work fine, but when I use a lot of these objects, it doesn’t work at all (5s become 10s…). And I Don’t understand why, because millis() normally runs independently from the programm if I understand well.
Here is my code :

void loop() {

  current_time = millis();

  for (byte i=0; i<NUMBER_OF_OBJECT; i++){

     
      //Step 0
      if(current_time>=0 && current_time<5000){
       
       //Work with my object
      }
    
      
      //End Step 0



       //Step 1
      if(current_time>=5000 && current_time<10000){
        
      //Work with my object

      }//End Step 0
     

}

I am using the Adafruit Neopixel Library, if it can help.

I hope I am clear enough and I thank you a lot in advance for your help ! :slight_smile:

If you provide a snippet of your code, expect a snippet of an answer.

I will tell you that

  current_time = millis();

will cause current_time to ALWAYS be greater than zero (assuming that current_time is declared unsigned long). In fact, on my Uno, millis() starts at 4 or 5.

(deleted)

Thanks for your answers ! :slight_smile:

I only posted that part of the code because everything else isn't part of the problem I think (there are juste initializations and work on variables).

I agree with what you said, but actually it shouldn't be a problem because when the timer is greater then 5000, the first step is completed and the loop won't go through it anymore. And the timer is a unsigned lolng yes :slight_smile:

I looked at the blink wthout delay example, but if I understant well it isn't exactly what I need, because this example is for making things happening during a certain amount of time, and I would like to make things happend after a certain amount of time, am I right ? :slight_smile:

spycatcher2k:
Look at the blink wthout delay example - you are doing it totally wrong.

No, what he is doing is just fine, based on his stated goal…

Regards,
Ray L.

I looked at the blink wthout delay example, but if I understant well it isn't exactly what I need, because this example is for making things happening during a certain amount of time, and I would like to make things happend after a certain amount of time, am I right ?

There is no difference between the two

You either do something during the period or you do something when the period ends. The important part is saving the start time of the period and checking often whether it has ended.

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Declarations are missing from the posted code and can be significant. I would not have to make unwanted assumptions if the declarations were present.

A short compilable example program that could be posted could serve everyone well.

Thanks for your answers !

The Blink without delay example isn’t really what I want to do. Actually, I’d like things to happend one by one. And my code worked well if I use only a few of my objects, but when I use a lot of these objects it doesn’t work anymore. I mean, it stills do the things one by one, but the last of each step is wrong. It looks like the millis() functiun is delayed. If I try to print it in the Serial Port, I clearly see that 1s isn’t one second anymore.

Is it possible that the time my code worked had an impact on the timer ?

I can’t post an example to compile, I have a home made Library, so it won’t make any sense I think :slight_smile:

Thanks for your help !

Are you messing with the timer? Are you disabling or otherwise messing with interrupts?

When I say timer I mean the one that millis() is using. Otherwise I am not using any other interrupts :slight_smile:

I think I just find the issue ! Actually, everything is working fine, but as I said, I am also using the Adafruit Neopixel Library and it seems that when I set the color of the pixel in the loop, it creates a delay.
Any idea to prevent that ? :slight_smile:

when I set the color of the pixel in the loop, it creates a delay.

How are you doing that ?
Where are you doing that ?
How often are you doing that ?

If only you had posted the complete program (and your library too)

I just figured out everything ! I found the solution on numLEDs = 100 causes timing issues · Issue #54 · adafruit/Adafruit_NeoPixel · GitHub

PaintYourDragon "The NeoPixel library has to disable all interrupts during a call to show() in order to maintain cycle-accurate timing for the LED data stream...ANY interruption will corrupt this. The millis() and micros() functions depend on a periodic timer interrupt behind the scenes. Since the interrupt is briefly disabled during each show(), the clock will lose time."

If it can help someone else with this issue :slight_smile:

Thank you everyone !