⏱️ Timing with delay() and millis() in Arduino
When building Arduino projects, controlling timing is crucial. Whether you want an LED to blink, a motor to run at intervals, or a sensor to read data periodically, Arduino gives you two simple tools: delay() and millis().
🔹 Using delay()
The delay() function is the easiest way to pause your program for a specific number of milliseconds. For example, delay(1000) pauses your code for one second.
Pros:
- Super easy to use
- Great for simple projects
Cons:
- Stops all other code from running while waiting
- Not ideal for projects needing multiple tasks at the same time
🔹 Using millis()
millis() is more advanced and gives you the number of milliseconds since your Arduino started running. It allows you to track time without stopping your code, which is perfect for multitasking.
How it works:
- Store a start time
- Check elapsed time using
millis() - Trigger actions when a set interval has passed
🚀 Getting Started
Try making an LED blink first using delay(). Then try the same effect with millis() so you can also read a button or sensor at the same time. You’ll see why millis() is a game-changer for more complex projects!