THE FUTURE MARKETACADEMY
Your classroomRoom 15 · Science Center
School of Science

Embedded Systems: microcontrollers and sensors

Step 6 of 7 in Science Center

Next in your path: 15.9.4 Science Mentor: judging and coaching

What you'll be able to do
  • Write a sketch that does several jobs at once without blocking, using millis() and a simple state machine.
  • Wire and read inputs reliably: pull-up resistors, debouncing and interrupts.
  • Turn sensor readings into real units with an ADC, a voltage divider and a calibration.
  • Choose between UART, I2C and SPI for a sensor or module and wire it correctly.
  • Budget current and battery life, and keep 3.3 V and 5 V parts safe together.
  • Test, debug and document an embedded system, and show a graded project.

Before this course: Robotics Basics: sensors, motors & simple code (course 15.8), which builds on Electricity & Electronics Basics (course 15.7). You should already know Ohm's law, how to read a Serial Monitor, setup() and loop(), analogRead(), and how to use a breadboard.

Safety rule for this whole course: every project runs from AA/AAA batteries, a 9 V battery or USB power only (5 V). Never connect anything to a wall outlet or take apart anything that plugs into the wall. Do not use lithium-polymer or other rechargeable lithium packs unless an adult who knows how to handle them is with you. Kids: build with an adult.

Words in this course 45Open the Vocabulary Hall
01

Doing many jobs at once: millis() and state machines

In simple words

Imagine you are cooking soup and also watching a pot of rice. If you stand still and stare at the soup for ten minutes, the rice burns. A good cook glances at the clock, does a bit of each job, and keeps moving. A good program does the same: it checks the clock instead of freezing.

Key facts
  • An embedded system is a small computer built into a device to do one set of jobs, usually with sensors in and actuators out. A microcontroller board is the usual starting point.
  • delay(1000) stops your code for a second. Nothing else (reading a button, updating a display) can run during it.
  • millis() returns the milliseconds passed since the program began. Its data type is unsigned long, and it overflows (goes back to zero) after about 50 days.
  • Non-blocking timing pattern: save the time of the last action, and each pass through loop() check whether enough time has gone by. Compare with subtraction: now - last >= interval. With unsigned numbers, subtraction keeps working even when the counter wraps back to zero.
  • A state machine is a program that is always in exactly one named state (for example IDLE, MEASURING, ALARM) and has rules for moving to the next state. It keeps long programs organised and easy to test.
Example (blink and read at the same time)
const unsigned long BLINK_MS = 500;
unsigned long lastBlink = 0;
bool ledOn = false;

void setup() {
 pinMode(LED_BUILTIN, OUTPUT);
 Serial.begin(9600);
}

void loop() {
 unsigned long now = millis();
 if (now - lastBlink >= BLINK_MS) {
  lastBlink = now;
  ledOn = !ledOn;
  digitalWrite(LED_BUILTIN, ledOn);
 }
 // other jobs can run here every pass, for example reading a button
}
Example (states)
StateWhat it doesMoves to
IDLEWaitsMEASURING every 2 seconds
MEASURINGReads the sensorALARM if the value is too high, otherwise IDLE
ALARMFlashes an LEDIDLE when a button is pressed
Step by step
  1. Open the Arduino IDE and start a new sketch.
  2. Type the non-blocking blink from the lesson and upload it.
  3. Add a second job in loop() that prints millis() once per second using its own last variable and the same subtraction pattern.
  4. Watch the LED blink and the Serial Monitor print at the same time.
  5. Replace one delay() in an old sketch of yours with the millis() pattern.
  6. Draw the state table for a project idea: states, what each does, and what moves it to the next state.
  7. Check: two jobs run at different rhythms and neither one freezes the other.
Watch on YouTube
Walk away from delay: using millis()talofer99 · YouTube · youtube.com

Videos by other creators on YouTube, for reference. They are not made by or affiliated with The Future Market.

LESSON 01Doing many jobs at once: millis() and state machines
LESSON 01 / ANIMATED LESSON / AI VOICE00:00 / 01:15
Read the video script
[Host] "Delay is a trap. While your code sleeps, it cannot see a button or a sensor."
[Screen: timeline with two blinking LEDs at different speeds] "Instead, ask the clock: how long since I last did this?"
[Text on screen: now - last >= interval] "Subtract, compare, and update the time you last acted."
[Screen: three boxes IDLE, MEASURING, ALARM with arrows] "Then give your program named states. It is always in exactly one."
[Host] "Small tasks, a clock and states: that is how real devices multitask."
Try it now

Write a sketch that blinks one LED every 300 ms and prints a counter every 2 seconds, with no delay() anywhere. Then draw its state table on paper. If you have no board, use a free simulator and print messages instead of lighting an LED.

ClaudeStuck? Copy a ready-made prompt for Claude

Copy this, open Claude, paste it, and Claude will walk you through the lesson step by step.

For a grown-up: Claude is for adults (18+). Copy this, paste it into Claude, and use the answer to explain the lesson to your child.

I'm learning "Embedded Systems: microcontrollers and sensors" on The Future Market. Lesson 1: "Doing many jobs at once: millis() and state machines".

What the lesson teaches:
- An embedded system is a small computer built into a device to do one set of jobs, usually with sensors in and actuators out. A microcontroller board is the usual starting point.
- delay(1000) stops your code for a second. Nothing else (reading a button, updating a display) can run during it.
- millis() returns the milliseconds passed since the program began. Its data type is unsigned long, and it overflows (goes back to zero) after about 50 days.
- Non-blocking timing pattern: save the time of the last action, and each pass through loop() check whether enough time has gone by. Compare with subtraction: now - last >= interval. With unsigned numbers, subtraction keeps working even when the counter wraps back to zero.
- A state machine is a program that is always in exactly one named state (for example IDLE, MEASURING, ALARM) and has rules for moving to the next state. It keeps long programs organised and easy to test.

The exercise I'm working on:
Write a sketch that blinks one LED every 300 ms and prints a counter every 2 seconds, with no delay() anywhere. Then draw its state table on paper. If you have no board, use a free simulator and print messages instead of lighting an LED.

I'm stuck. Please:
1. Explain the main idea in plain, simple words with one everyday example.
2. Walk me through the exercise one small step at a time. Ask me what I've done so far before giving the next step.
3. Don't just give me the final answer. Help me do it myself, and check my work when I share it.
I'm a parent helping my child (about 8 years old) with a lesson called "Doing many jobs at once: millis() and state machines" from the course "Embedded Systems: microcontrollers and sensors".

Here's what the lesson says:
Imagine you are cooking soup and also watching a pot of rice. If you stand still and stare at the soup for ten minutes, the rice burns. A good cook glances at the clock, does a bit of each job, and keeps moving. A good program does the same: it checks the clock instead of freezing.

The activity:
Write a sketch that blinks one LED every 300 ms and prints a counter every 2 seconds, with no delay() anywhere. Then draw its state table on paper. If you have no board, use a free simulator and print messages instead of lighting an LED.

Please explain this so an 8-year-old understands: short sentences, a fun everyday example, and no hard words.
Then give me 3 simple questions I can ask my child to check they understood, and a way to do the activity together, one tiny step at a time.
Open Claude
Sources
My notes
02

Reliable inputs: pull-ups and debouncing

This lesson is part of full access: every course and every level for one payment.

03

Interrupts: reacting right now

This lesson is part of full access: every course and every level for one payment.

04

From voltage to real units: ADCs, dividers and calibration

This lesson is part of full access: every course and every level for one payment.

05

Talking to chips: UART, I2C and SPI

This lesson is part of full access: every course and every level for one payment.

06

Power, voltage levels and battery life

This lesson is part of full access: every course and every level for one payment.

07

Testing, debugging and a graded project plan

This lesson is part of full access: every course and every level for one payment.

++

Final project

This lesson is part of full access: every course and every level for one payment.

??

Quiz

  1. Why avoid delay() in a multitasking program?

  2. What does millis() return, and when does it overflow?

  3. Why does now - last >= interval still work after millis() wraps?

  4. What does INPUT_PULLUP do, and how does a button read?

  5. What is switch bounce?

  6. Which pins support interrupts on an Uno Rev3?

  7. What should you not do inside an ISR?

  8. Why mark shared ISR variables volatile?

  9. How big is one step of the Uno's ADC with a 5 V reference?

  10. In a divider with Vin = 5 V and R1 = R2, what is Vout?

  11. Which Uno pins are I2C SDA and SCL?

  12. What does SPI use to choose which device talks?

  13. A battery holds 1,000 mAh and the project averages 5 mA. About how long does it last?

EX

Final exam

This lesson is part of full access: every course and every level for one payment.

CE

Certificate

This lesson is part of full access: every course and every level for one payment.