Mission 24 · Stage 3

Countdown Timer

decrement a variable over time

Count down from 10 to 0 using an int variable and Serial output.

Countdown Timer circuit diagram

See it

T-minus… Liftoff!

secondsLeft counts down from 10 — each second prints until blastoff.

Rocket launches, game timers, and oven timers all count down a variable to zero.

The story

The problem

You need a number that shrinks over time until something exciting happens.

Think of it like

Like counting backwards before hide-and-seek — 10, 9, 8… then GO!

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Show current count

Every loop pass prints the value stored in secondsLeft.

Serial.print("T-minus ");
Serial.println(secondsLeft);
2

Check for liftoff

When the variable hits zero, we celebrate and stop counting down.

if (secondsLeft <= 0) {
  Serial.println("Liftoff!");
}
3

Subtract one second

After waiting one real second, the variable drops by 1 — then loop() prints again.

secondsLeft = secondsLeft - 1;
delay(1000);

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed!

    Loading part…

Try it

  • Open Serial Monitor and watch T-minus 10, 9, 8…
  • Wait for Liftoff! at the end.

Peek at code

Countdown variable

int secondsLeft = 10;

secondsLeft starts at 10 — an int variable that will shrink each second.

Liftoff check

    while (true) {
      delay(1000);
    }
  }
  secondsLeft = secondsLeft - 1;

When secondsLeft reaches 0, print Liftoff! and stay in an endless wait.

Decrement each second

}

Subtract 1 from the variable, then delay(1000) for one real second.

Show full sketch (countdown-timer.ino)
int secondsLeft = 10;
void setup() {
  Serial.begin(9600);
  Serial.println("Countdown starting...");
}
void loop() {
  Serial.print("T-minus ");
  Serial.println(secondsLeft);
  if (secondsLeft <= 0) {
    Serial.println("Liftoff!");
    while (true) {
      delay(1000);
    }
  }
  secondsLeft = secondsLeft - 1;
  delay(1000);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. What happens when secondsLeft reaches 0?

  • A. Serial prints Liftoff! and stops counting
  • B. The variable resets to 10
  • C. setup() runs again
Why: Correct — countdown ends with Liftoff! when secondsLeft <= 0.

Code lab — try on your own

  1. Start from 15 — change int secondsLeft = 10 to int secondsLeft = 15.

    Hint: Line 1 at the top.

  2. Count down faster — change delay(1000) to delay(500) at the end of loop().

    Hint: Line 18.

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robo Gurukul Studio.

Program overview

Technical

Sketches have globals, then setup() once, then loop() forever.

In this project

Count down from 10 to 0 using an int variable and Serial output.

Why here

Read from top to bottom. Hover words or lines for help!

int secondsLeft = 10;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Countdown Timer.

Why here

One-time setup belongs here—not in loop().

void setup() {
  Serial.begin(9600);
  Serial.println("Countdown starting...");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Countdown Timer.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  Serial.print("T-minus ");
  Serial.println(secondsLeft);
  if (secondsLeft <= 0) {
    Serial.println("Liftoff!");
    while (true) {
      delay(1000);
    }
  }
  secondsLeft = secondsLeft - 1;
  delay(1000);
}

Try this: Change numbers in loop(), then compile and run the simulator.

delay

Technical

Waits for some time. Nothing else runs during the wait.

In this project

Controls speed so you can see Countdown Timer in the simulator.

Why here

Right after an action that should stay the same for a moment.

      delay(1000);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Countdown Timer print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Countdown Timer on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

  Serial.print("T-minus ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Countdown Timer.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Countdown starting...");