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

10… 9… 8… Liftoff!

A number starts at 10 and counts down second by second — then your rocket blasts off!

Rocket launches, game timers, and oven timers all count down to zero like this.

The story

The problem

We want a number that gets smaller over time until something exciting happens.

Think of it like

It's like counting backwards before a race — 10, 9, 8… then GO!

Meet the parts

It counts down and shouts Liftoff!

Arduino

The brain

Loading part…

How it works

1

Show the number

Each loop shows the number that is in secondsLeft right now.

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

Time to blast off?

When the number reaches 0, we shout Liftoff! and stop counting.

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

Take away one second

After one real second, the number drops by 1 — then loop() shows it 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 — ready for liftoff!

    Loading part…

Try it

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

Peek at code

The countdown box

int secondsLeft = 10;

secondsLeft starts at 10 — a box that gets smaller by 1 each second.

Check for liftoff

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

When secondsLeft hits 0, the screen shows Liftoff! and then waits.

Take away one second

}

Subtract 1 from the box, then wait 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 box that stores a number
  • B. A kind of wire
  • C. Just a wait
Why: Yes! int secondsLeft = 10; makes a box named secondsLeft.

Q2. What happens when secondsLeft reaches 0?

  • A. The screen shows Liftoff! and stops counting
  • B. The number jumps back to 10
  • C. setup() runs again
Why: Yes! The countdown ends with Liftoff! when secondsLeft reaches 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 16.

Code walkthrough

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

Program overview

Big idea

Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.

In this project

A number starts at 10 and gets smaller by 1 each second, until it reaches 0 and says Liftoff!

Tip

Read from the top to the bottom. Tap any word or line if you need help!

int secondsLeft = 10;

setup()

Big idea

setup() runs one time when the board turns on.

In this project

It opens the message screen and says the countdown is starting.

Why here

Things we do only once go inside setup().

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

loop()

Big idea

loop() runs again and again, forever.

In this project

This is where the number shrinks, second by second, down to Liftoff!

Why here

Things that repeat go inside loop().

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 a number inside loop(), then press Run to see what happens.

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

It waits one whole second between each number.

Why here

Right after we show and lower the number.

      delay(1000);

begin

Big idea

Serial.begin opens a message screen so the board can talk to the computer.

In this project

It lets us print the countdown.

Why here

It goes in setup() once, before we print anything.

  Serial.begin(9600);

print

Big idea

Serial.print writes words or a number on the screen and stays on the same line.

In this project

It writes "T-minus " right before the number.

Why here

In loop() so we can see each count.

  Serial.print("T-minus ");

println

Big idea

Serial.println writes on the screen and then jumps to a new line.

In this project

It prints a friendly hello when the board turns on.

Why here

So each message gets its own line.

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