How to remove the delay in an Arduino sketch? The delay(); is a blocking function and does not let you multitask with the Arduino or do more than one thing at a time. You want to do two things at once with the Arduino. Therefore you need to get rid of delay in your sketch. This shows how to use a millisecond timer to replace the delay function in an Arduino sketch and have a much better non blocking sketch that can be combined with other sketches.
How to Remove Delay from an Arduino Sketch
In this Arduino tutorial, I will show you how to remove the delay function from an Arduino sketch so that you can use it in multitasking with the Arduino. The delay function is a blocking function, so it prevents the Arduino from doing anything else while it is waiting for the delay to finish.
To remove the delay function from an Arduino sketch, you will need to:
- Create a new variable to store the current time in milliseconds.
- Create another variable to store the previous time in milliseconds.
- Create an if statement that checks if the current time is greater than or equal to the previous time plus the desired delay time.
- If the condition is met, then execute the code that you want to run every time the delay time elapses. Reset the previous time variable to the current time.