Delay Function Error solution

unsigned long previousMillis = 0UL;
      unsigned long interval = 1000UL;
      void setup()
      { /* do setup */ }
        void loop()
      {  
       /* other code */  
       unsigned long currentMillis = millis();  
       if(currentMillis - previousMillis > interval)  
      {	
       /* The Arduino executes this code once every second 	
       (interval = 1000 (ms) = 1 second). 	
       */ 	
        // Don't forget to update the previousMillis value 	
        previousMillis = currentMillis; 
          }
        }
  • Note how the loop()-method in the example sketch above can execute code with every iteration and some other code only once every second. This behavior is another benefit of using the millis()-method instead of delay(). With a delay-call, the entire program would halt for a second. When using millis(), ensure that you update the “previousMillis” variable somewhere in the if-block to ensure that the timings remain correct in consecutive iterations of the loop()-method.*

What "error" is this solving?

I moved your topic to an appropriate forum category @kanansananse.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

The delay function in programming pauses the execution of code for a specified duration. An error here could arise from incorrect usage of the delay function, which might lead to unexpected program behavior or inaccurate timing delays.

Did you repair the delay() function provided by Arduino?

Why are you posting in ALL BOLD?

But that's not an error with the delay function itself - that's just (possible) mis-use of the delay function.

yes, this basic concept is common practice.

1 Like

Indeed - there's a tutorial on it:

And even an example included in the IDE:

Your posts do not contain a question.

What is your question?

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