Mission 29 · Stage 3

Dice Simulator

variables plus random for dice

Roll a die 1–6 with random() stored in a variable when you press a button.

Dice Simulator circuit diagram

Pin connections

Part 1Part 2

Pushbutton

pin 1

Arduino

pin 2

Pushbutton

pin 2

Arduino

GND

See it

Roll the dice!

Press the button — lastRoll stores random(1, 7) for a fresh 1–6 each time.

Board games and apps store the latest roll in a variable before showing it.

The story

The problem

You want a die face that changes only when the player asks to roll.

Think of it like

Like shaking a dice cup — the number stays until you roll again.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Wait for roll button

Nothing changes until you press — lastRoll keeps the previous value.

if (digitalRead(BUTTON_PIN) == LOW)
2

Pick random die face

random(1, 7) gives 1 through 6 — stored in lastRoll until the next roll.

lastRoll = random(1, 7);
3

Show result

Print the stored roll, debounce the button, then loop() waits again.

Serial.print("You rolled: ");
Serial.println(lastRoll);

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

  • Press the button — You rolled: 1–6 on Serial.
  • Press again for a new random face!

Peek at code

lastRoll variable

const int BUTTON_PIN = 2;
int lastRoll = 1;

lastRoll remembers the most recent die face between button presses.

Setup and seed

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Dice — press button to roll");
}

Button pin, Serial, and randomSeed() prepare for fair rolls.

Roll on press

    lastRoll = random(1, 7);
    Serial.print("You rolled: ");
    Serial.println(lastRoll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(200);
  }
  delay(20);
}

Press → random roll → store in lastRoll → print → wait for next press.

Show full sketch (dice-simulator.ino)
const int BUTTON_PIN = 2;
int lastRoll = 1;
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Dice — press button to roll");
}
void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    lastRoll = random(1, 7);
    Serial.print("You rolled: ");
    Serial.println(lastRoll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(200);
  }
  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 does random(1, 7) return?

  • A. 1 to 6 inclusive
  • B. 1 to 7 inclusive
  • C. Always 1
Why: Correct — you get 1, 2, 3, 4, 5, or 6 — perfect for a die.

Code lab — try on your own

  1. Pretend last roll was 6 — change int lastRoll = 1 to int lastRoll = 6.

    Hint: Line 2.

  2. Add a comment on lastRoll = random(1, 7) explaining “new die face”.

    Hint: Line 13 inside the if block.

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

Roll a die 1–6 with random() stored in a variable when you press a button.

Why here

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

const int BUTTON_PIN = 2;
int lastRoll = 1;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Dice Simulator.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Dice — press button to roll");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Dice Simulator.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    lastRoll = random(1, 7);
    Serial.print("You rolled: ");
    Serial.println(lastRoll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(200);
  }
  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 Dice Simulator circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Dice Simulator.

Why here

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

  if (digitalRead(BUTTON_PIN) == LOW) {

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Dice Simulator.

Why here

Goes in loop() to keep checking the sensor.

  randomSeed(analogRead(0));

delay

Technical

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

In this project

Controls speed so you can see Dice Simulator in the simulator.

Why here

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

      delay(20);

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Dice Simulator.

Why here

In loop() when you want different values each time.

    lastRoll = random(1, 7);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Dice Simulator 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 Dice Simulator on the screen.

Why here

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

    Serial.print("You rolled: ");

println

Technical

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

In this project

Prints one line of output for Dice Simulator.

Why here

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

  Serial.println("Dice — press button to roll");