0Pricing
Arduino & IoT Academy · Lesson

Blink Without delay Using millis

Track elapsed time to run tasks on schedule.

Blink Without delay Using millis is a free Arduino & IoT Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

A Built-in Stopwatch

The millis function returns how many milliseconds your board has been running since it powered on. It is a free, always-counting stopwatch. ⏱️

unsigned long now = millis();

Why unsigned long

Store millis in an unsigned long. The count grows huge over time, and a plain int would overflow in under a minute.

The Core Idea

Instead of pausing, you remember the last time you acted, then keep checking how much time has passed since then on every loop.

Remember the Last Moment

Keep a variable like previousMillis to store when you last toggled the LED. You will compare it against the current time each pass.

unsigned long previousMillis = 0;

Set Your Interval

Choose an interval, say 1000 milliseconds, as the gap between blinks. Acting only after this much time passes gives a steady rhythm.

const long interval = 1000;

Has Enough Time Passed

Each loop, subtract previousMillis from the current millis. If the gap is at least your interval, it is time to act.

if (millis() - previousMillis >= interval) {

Reset the Timer

The instant you act, set previousMillis to the current time. This restarts the countdown so the next blink waits a full interval again.

previousMillis = millis();

Flip the LED

Inside the timed block you simply toggle the LED, flipping it on if it was off and off if it was on, for a clean blink.

ledState = !ledState;
digitalWrite(ledPin, ledState);

The Loop Never Stops

Between blinks the loop keeps spinning at full speed, free to read sensors or buttons. Nothing is ever frozen waiting.

Subtraction Handles Overflow

After about 49 days millis wraps to zero, but the subtraction trick still gives the right gap, so your timing keeps working.

A Reusable Pattern

This millis pattern works for any repeating task, not just blinking. Change the interval and the action and you can schedule anything.

Quick Check

Why do we store the result of millis in an unsigned long?

Recap

You learned the millis pattern: remember the last time, check the elapsed gap, then act and reset. Now your board blinks while staying fully alert. 🎉

Frequently asked questions

Is the “Blink Without delay Using millis” lesson free?

Yes — the full text of “Blink Without delay Using millis” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “Blink Without delay Using millis”?

Track elapsed time to run tasks on schedule. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Blink Without delay Using millis” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. int, float, bool on Arduino
  2. Why delay() Blocks Everything
  3. Blink Without delay Using millis
  4. Run Two Things at Once
← Back to Arduino & IoT Academy