// Arduino Dice Roller on Wokwi Arduino Simulator
//Resistors are omitted. wires are hidden for simplicity
#include <LiquidCrystal.h> //address for LCD
#define BUTTON_PIN A0 // Button to pin A0
const byte die1Pins[] = { 2, 3, 4, 5, 6, 7, 8}; // const, are just normal variables, whose values can't be changed. They take up program memory space, LED pins
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 13, d7 = 1; //Assigning LCD pins to Uno DI's
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // LCD pins that are in use, sets them
int buzzer = 15; // Buzzer to pin 15, (A1)
int tone_On = 0; //initialise frequency generated for the buzzer
int num1; // innitialise num 1
void setup() { // Code only runs once at beginning
lcd.begin(16, 2); //LCD had 2 lines and can display 16 character per line
lcd.setCursor(3, 0);
lcd.print("Welcome to");
lcd.setCursor(2, 1);
lcd.print("The Dice Game!");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hold the button");
lcd.setCursor(0, 1);
lcd.print("to roll the dice!");
delay(2000);
lcd.clear();
pinMode(A0, INPUT_PULLUP); // Used to configure specific pin to behave as either an input or an output
pinMode (buzzer, OUTPUT); // Set buzzer as output
for (byte i = 0; i < 7; i++) { //
pinMode(die1Pins[i], OUTPUT); // LEDs set as outputs, icremented
}
}
void displayNumber(const byte pins[], byte number) { //takes the display number
digitalWrite(pins[0], number > 1 ? HIGH : LOW); // top-left, greater than, ? do high,: else low
digitalWrite(pins[1], number > 3 ? HIGH : LOW); // top-right
digitalWrite(pins[2], number == 6 ? HIGH : LOW); // middle-left
digitalWrite(pins[3], number % 2 == 1 ? HIGH : LOW); // center
digitalWrite(pins[4], number == 6 ? HIGH : LOW); // middle-right
digitalWrite(pins[5], number > 3 ? HIGH : LOW); // bottom-left
digitalWrite(pins[6], number > 1 ? HIGH : LOW); // bottom-right
}
bool randomReady = false;
void loop() { //Code repeats over and over
bool buttonPressed = digitalRead(BUTTON_PIN) == LOW; //if button pressed disable internal pull up
bool buttonNotPressed = digitalRead(BUTTON_PIN) == HIGH; //if button pressed enable internal pull up
if (!randomReady && buttonPressed) {
// to initialize the random number generator
randomSeed(micros()); // initialises random number generator (micro timing)
randomReady = true; // random generator
}
if (buttonPressed) { // If button pressed rolling and prints number
tone_On = 0;
for (byte i = 0; i < 7; i++) { // i=0 means you’re starting the ‘for’ loop count at 0, i<7 means whilst i is less than 7, keep running, i++ means when the loop is run, add add +1 to i., The loop will run over and over until i = 7, Then it’ll exit the loop
num1 = random(1, 7); //random number
displayNumber(die1Pins, num1); //Display on leds num 1
delay(50 + i * 20); //delay microseconds
lcd.setCursor(4, 0);
lcd.print("!Rolling!");
lcd.setCursor(7, 1);
lcd.print(num1); //Prints number
}
}
if (buttonNotPressed && randomReady) { //Only if both are true
lcd.setCursor(4, 0);
lcd.print("!Winner!");
tone_On++; //Increment operator
delay(750);
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print("!Winner!");
delay(750);
lcd.setCursor(4, 0);
lcd.print(" ");
}
if (tone_On == 1) //If they have same value
{
for (int i = 0; i < num1; i++)
{
tone(buzzer, 1500);
delay(500);
noTone(buzzer);
delay(500);
}
}
if (buttonNotPressed){ //If buton not pressed, ready to roll, winner
lcd.setCursor(1.5, 0);
lcd.print("!Ready to Roll!");
delay(1000);
lcd.setCursor(0, 0);
lcd.print(" ");
delay(1000);
}
}