Dice Roll Simulator (1-6)

Arduino Dice Roll Simulator using a 7-segment display and button to generate random numbers from 1 to 6 with hardware and software setup.

Brief Introduction

Dice Roll Simulator (1-6) (cpp)
DESCRIPTION:
This program simulates the roll of a 6-sided die using Arduino. By pressing a button, the microcontroller generates a random number from 1 to 6 and displays it on a 7-segment display. The code includes the necessary hardware and software setup for operation.

ADDITIONAL NOTES:
The code uses a simple method to generate random numbers by exploiting an unconnected analog pin to initialize the random generator seed. The 7-segment display is common cathode, so to light a segment the corresponding pin must be set to HIGH. The button is connected with a pull-down resistor to avoid floating readings.

EXPLANATIONS:
- The setup() function configures the segment pins as OUTPUT and the button pin as INPUT.
- The loop() function checks the button state; when pressed, it generates a random number from 1 to 6 and updates the display.
- The mostraNumero(int n) function lights the necessary segments to show the number n on the 7-segment display.
- Software debounce is implemented with a 200 ms delay to avoid multiple button reads.
- The random number generator seed is initialized by reading an unconnected analog pin to increase entropy.

Example Code: cpp

// Dice roll simulator (1-6) with Arduino and 7-segment display

// Pin definition for 7-segment display segments (common cathode)
const int segmenti[7] = {2, 3, 4, 5, 6, 7, 8}; // A,B,C,D,E,F,G

// Button pin
const int pinPulsante = 9;

// Segment map for numbers 1 to 6
// Each array indicates which segments to light (1=on, 0=off)
// Segment order: A,B,C,D,E,F,G
const byte numeri[6][7] = {
  // 1
  {0,1,1,0,0,0,0},
  // 2
  {1,1,0,1,1,0,1},
  // 3
  {1,1,1,1,0,0,1},
  // 4
  {0,1,1,0,0,1,1},
  // 5
  {1,0,1,1,0,1,1},
  // 6
  {1,0,1,1,1,1,1}
};

void setup() {
  // Set segment pins as output
  for (int i = 0; i < 7; i++) {
    pinMode(segmenti[i], OUTPUT);
    digitalWrite(segmenti[i], LOW); // turn off all segments at startup
  }

  // Set button pin as input
  pinMode(pinPulsante, INPUT);

  // Initialize random number generator by reading an unconnected analog pin (A0)
  randomSeed(analogRead(A0));
}

void loop() {
  // Read button state
  int statoPulsante = digitalRead(pinPulsante);

  if (statoPulsante == HIGH) { // button pressed
    // Generate a random number from 1 to 6
    int numero = random(1, 7);

    // Display the number
    mostraNumero(numero);

    // Software debounce: wait 200 ms to avoid multiple reads
    delay(200);

    // Wait for button release before continuing
    while (digitalRead(pinPulsante) == HIGH) {
      delay(10);
    }
  }
}

// Function to light segments corresponding to number n (1-6)
void mostraNumero(int n) {
  if (n < 1 || n > 6) return; // invalid number

  for (int i = 0; i < 7; i++) {
    if (numeri[n - 1][i] == 1) {
      digitalWrite(segmenti[i], HIGH); // turn on segment
    } else {
      digitalWrite(segmenti[i], LOW);  // turn off segment
    }
  }
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Arduino Uno (or compatible)

Common cathode 7-segment display

Momentary push button

220 ohm resistors for the display segments

10k ohm resistor for the button (pull-down)

Connecting wires

Breadboard

Arduino Pinout:
  • Display segments: A->pin 2, B->pin 3, C->pin 4, D->pin 5, E->pin 6, F->pin 7, G->pin 8
  • Button: pin 9 (digital input)
Software:
  • Arduino IDE
  • No external libraries required
  • Random seed configuration via reading unconnected analog pin (A0)

Similar Topics

Supermarket Queue Simulator with std::queue
Supermarket Queue Simulator in C++ using std::queue to model FIFO customer service, demonstrating queue operations and status updates in a console program.
Bank Account Simulator with Deposit and Withdrawal
Simple C++ bank account simulator with deposit, withdrawal, and balance display. Learn class management, methods, and error handling easily.
Traffic Light Simulator with Timed State Change
Arduino traffic light simulator using LEDs with timed state changes: green 5s, yellow 2s, red 5s. Easy to modify and runs on Arduino UNO.
Battle Simulator Between Characters with Classes and Polymorphism
C++ battle simulator using classes and polymorphism to model Warrior, Mage, and Archer characters with turn-based combat and extendable design.
Multi-level Parking Simulator with Stall Management
Multi-level parking simulator in C++ with stall management, LCD display, RGB LED indicators, and button controls for booking and freeing stalls.
Single-Elimination Tournament Simulator with Bracket Generation
C++ single-elimination tournament simulator with automatic bracket generation, match simulation, and winner determination for any number of participants.
Trading System Simulator with Limit Orders and In-Memory Order Book
C++ trading system simulator with in-memory order book, supporting buy/sell limit orders, automatic matching, partial fills, and efficient data structures.
Multi-floor Elevator Simulator with Request Queue and Direction Priority
Multi-floor elevator simulator in C++ for Arduino Uno with request queue, direction priority, LCD display, buttons, and LED indicators.