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!

lapNumber and totalPresses are separate variables — green adds a lap, red resets both.

Running apps track lap number and total steps with different variables.

The story

The problem

Sometimes you need more than one number — lap count and total presses are different ideas.

Think of it like

Like lap boards at a track: one shows “lap 3” and another shows “30 total steps”.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Check reset button

Red button clears both variables back to zero — a fresh start.

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

Check lap button

Green button means record another lap.

if (digitalRead(LAP_BUTTON) == LOW)
3

Increment both counters

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

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

Print and wait

Show both numbers on Serial, then loop() checks 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!

    Loading part…

Try it

  • Green button = new lap. Red button = reset both counters.
  • Watch Serial for Lap 1 | Total presses: 1

Peek at code

Two counter variables

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

totalPresses and lapNumber are separate int boxes — reset clears both.

Reset handler

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

Red button sets both variables to 0 and prints Counters reset!

Lap handler

    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);

Green button adds 1 to each variable and prints Lap N | Total presses: N.

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 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 you press the red reset button?

  • A. Both totalPresses and lapNumber go back to 0
  • B. Only lapNumber resets
  • C. The Arduino restarts
Why: Correct — red button clears totalPresses and lapNumber 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 comment on lapNumber = lapNumber + 1 explaining “next lap”.

    Hint: Line 24.

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

Track total presses and laps with two int variables.

Why here

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

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

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Lap Counter.

Why here

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

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

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Lap Counter.

Why here

Repeating work (blink, read sensors) goes here.

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 numbers in loop(), then compile and run the simulator.

pinMode

Technical

Tells a pin if it listens or drives something.

In this project

Gets the Lap Counter circuit ready in the simulator.

Why here

Goes in setup() because we only set pins once at the start.

  pinMode(LAP_BUTTON, INPUT_PULLUP);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Lap Counter.

Why here

Goes in loop() so we can react when something changes.

  if (digitalRead(RESET_BUTTON) == LOW) {

delay

Technical

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

In this project

Controls speed so you can see Lap Counter in the simulator.

Why here

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

      delay(20);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Lap Counter 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 Lap Counter on the screen.

Why here

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

    Serial.print("Lap ");

println

Technical

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

In this project

Prints one line of output for Lap Counter.

Why here

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

  Serial.println("Lap Counter — green = lap, red = reset");