Mission 26 · Stage 3

Lap Counter

multiple counter variables

Track total presses and laps with two int variables.

Lap Counter circuit diagram

Pin connections

Part 1Part 2

Pushbutton

pin 1

Arduino

pin 2

Pushbutton

pin 2

Arduino

GND

Pushbutton

pin 1

Arduino

pin 3

Pushbutton

pin 2

Arduino

GND

See it

Two counters, one race!

Green button adds a lap, red button resets everything — two number boxes working together.

Running apps track your lap number and your total steps with different numbers, just like this.

The story

The problem

Sometimes one number is not enough — the lap count and the total presses are two different ideas.

Think of it like

It's like a lap board at a race: one sign shows "lap 3" and another shows the total.

Meet the parts

It watches both buttons and keeps two numbers.

Arduino

The brain

Loading part…

How it works

1

Did you press red?

The red button sets both numbers back to zero — a fresh start.

if (digitalRead(RESET_BUTTON) == LOW) {
  totalPresses = 0;
  lapNumber = 0;
}
2

Did you press green?

The green button means count one more lap.

if (digitalRead(LAP_BUTTON) == LOW)
3

Add to both boxes

Both numbers grow by 1 — lapNumber is the lap, totalPresses counts every press.

totalPresses = totalPresses + 1;
lapNumber = lapNumber + 1;
4

Show and wait

Show both numbers on the screen, then loop() checks the buttons again.

Serial.print("Lap ");
Serial.println(totalPresses);

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 to count laps!

    Loading part…

Try it

  • Green button = new lap. Red button = reset both numbers.
  • Watch the screen for Lap 1 | Total presses: 1

Peek at code

Two counter boxes

const int LAP_BUTTON = 2;
const int RESET_BUTTON = 3;
int totalPresses = 0;
int lapNumber = 0;

totalPresses and lapNumber are two separate boxes — reset clears them both.

The reset button

    lapNumber = 0;
    Serial.println("Counters reset!");
    while (digitalRead(RESET_BUTTON) == LOW) {
      delay(20);
    }
  }
  if (digitalRead(LAP_BUTTON) == LOW) {

The red button sets both boxes back to 0 and says Counters reset!

The lap button

    lapNumber = lapNumber + 1;
    Serial.print("Lap ");
    Serial.print(lapNumber);
    Serial.print(" | Total presses: ");
    Serial.println(totalPresses);
    while (digitalRead(LAP_BUTTON) == LOW) {
      delay(20);
    }
  }
  delay(20);

The green button adds 1 to each box and shows the lap and the total.

Show full sketch (lap-counter.ino)
const int LAP_BUTTON = 2;
const int RESET_BUTTON = 3;
int totalPresses = 0;
int lapNumber = 0;
void setup() {
  pinMode(LAP_BUTTON, INPUT_PULLUP);
  pinMode(RESET_BUTTON, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Lap Counter — green = lap, red = reset");
}
void loop() {
  if (digitalRead(RESET_BUTTON) == LOW) {
    totalPresses = 0;
    lapNumber = 0;
    Serial.println("Counters reset!");
    while (digitalRead(RESET_BUTTON) == LOW) {
      delay(20);
    }
  }
  if (digitalRead(LAP_BUTTON) == LOW) {
    totalPresses = totalPresses + 1;
    lapNumber = lapNumber + 1;
    Serial.print("Lap ");
    Serial.print(lapNumber);
    Serial.print(" | Total presses: ");
    Serial.println(totalPresses);
    while (digitalRead(LAP_BUTTON) == LOW) {
      delay(20);
    }
  }
  delay(20);
}

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 lapNumber = 0; makes a box named lapNumber.

Q2. What happens when you press the red reset button?

  • A. Both totalPresses and lapNumber go back to 0
  • B. Only lapNumber goes back to 0
  • C. The Arduino restarts
Why: Yes! The red button sets totalPresses and lapNumber back to 0 together.

Code lab — try on your own

  1. Start at lap 3 — change int lapNumber = 0 to int lapNumber = 3.

    Hint: Line 4.

  2. Add a note (comment) on the lapNumber line that says "next lap".

    Hint: Line 24.

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 green button adds a lap and a red button sets everything back to zero, using two number boxes.

Tip

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

const int LAP_BUTTON = 2;
const int RESET_BUTTON = 3;
int totalPresses = 0;
int lapNumber = 0;

setup()

Big idea

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

In this project

It gets the green button (pin 2) and red button (pin 3) ready and opens the message screen.

Why here

Things we do only once go inside setup().

void setup() {
  pinMode(LAP_BUTTON, INPUT_PULLUP);
  pinMode(RESET_BUTTON, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Lap Counter — green = lap, red = reset");
}

loop()

Big idea

loop() runs again and again, forever.

In this project

This is where we watch both buttons and update the numbers.

Why here

Things that repeat go inside loop().

void loop() {
  if (digitalRead(RESET_BUTTON) == LOW) {
    totalPresses = 0;
    lapNumber = 0;
    Serial.println("Counters reset!");
    while (digitalRead(RESET_BUTTON) == LOW) {
      delay(20);
    }
  }
  if (digitalRead(LAP_BUTTON) == LOW) {
    totalPresses = totalPresses + 1;
    lapNumber = lapNumber + 1;
    Serial.print("Lap ");
    Serial.print(lapNumber);
    Serial.print(" | Total presses: ");
    Serial.println(totalPresses);
    while (digitalRead(LAP_BUTTON) == LOW) {
      delay(20);
    }
  }
  delay(20);
}

Try this: Change a number inside loop(), then press Run to see what happens.

pinMode

Big idea

pinMode tells a pin if it will listen or push power out.

In this project

It sets the button pins to listen for presses.

Why here

It goes in setup() because we only set them once.

  pinMode(LAP_BUTTON, INPUT_PULLUP);

digitalRead

Big idea

digitalRead checks if a pin is ON or OFF.

In this project

It checks if the red reset button is being pressed.

Why here

It goes in loop() so we can react the moment you press.

  if (digitalRead(RESET_BUTTON) == LOW) {

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

A tiny wait helps the buttons read cleanly.

Why here

Right after we check a button.

      delay(20);

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 lap and the total.

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 "Lap " right before the lap number.

Why here

In loop() so the lap and total show on one line.

    Serial.print("Lap ");

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("Lap Counter — green = lap, red = reset");