Run Two Things at Once
Blink and read a button simultaneously.
Run Two Things at Once is a free Arduino & IoT Academy lesson on CoddyKit — lesson 4 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.
The Multitasking Dream
Real devices do many things at once: blink a light while watching a button. With non-blocking timing your board can finally juggle both.
Each Task Gets Its Timer
The trick is one timer per task. Give the blink its own previousMillis and interval, separate from anything else you want to run.
Track Time Separately
Keep a dedicated previousMillis for the LED so its schedule never tangles with other timed jobs in the same loop.
unsigned long ledPrevious = 0;
const long ledInterval = 500;Blink Block
First, the blink block checks its own timer. When 500 milliseconds pass it toggles the LED and resets, then control moves on.
if (millis() - ledPrevious >= ledInterval) {
ledPrevious = millis();
}Read the Button Every Loop
Right after the blink block, you read the button on every single pass. Because nothing froze, no press is ever missed.
int state = digitalRead(buttonPin);Two Jobs, One Loop
Now the same loop handles both: it times the blink and checks the button thousands of times a second. They feel simultaneous.
Add a Third Task
Want more? Just add another timer and block, like reading a sensor every two seconds. The pattern scales to many tasks cleanly.
Keep the Loop Fast
The rule that makes it work: never block. Keep every part of the loop quick so it can return often and serve all tasks fairly.
Not True Parallelism
The board still does one thing at a time, just very fast. This is cooperative multitasking, where each task does a little and yields.
Banish the Big delay
One long delay anywhere breaks the whole illusion, since it freezes every task. Replace pauses with millis checks to keep things smooth.
The Heart of Responsive IoT
This state machine style, many small timed checks in one loop, is how responsive sensors, displays, and connected devices are really built.
Quick Check
How does one loop blink an LED and read a button at the same time?
Recap
You combined timers in one loop to multitask: blink and read a button together, with no blocking delay. That is the core skill behind real IoT devices. 🚀
Frequently asked questions
Is the “Run Two Things at Once” lesson free?
Yes — the full text of “Run Two Things at Once” 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 “Run Two Things at Once”?
Blink and read a button simultaneously. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Run Two Things at Once” 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
- int, float, bool on Arduino
- Why delay() Blocks Everything
- Blink Without delay Using millis
- Run Two Things at Once