Isabellaw55
Published © GPL3+

Variable-Sided Digital Dice

Set the number of sides to your die and roll by pressing a push button!

BeginnerFull instructions provided1,911
Variable-Sided Digital Dice

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
×1
Pushbutton Switch, Momentary
Pushbutton Switch, Momentary
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Arduino Uno to LCD Screen

Arduino Uno to Potentiometer

Arduino Uno to Push Button

Code

Variable Digital Die

Arduino
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C LCD(0x27,20,4); 

int button = A0; //A0 connected to push button
int powerOut = A2; //A0 set to a constant power output for pullup resistor
int pot = A1; //A1 connected to potentiometer
long randomNum = 0; //initialize random number to 0
int generating = 0; //state for generating the random number
int upperLimit = 0; //upperlimit for die
int potVal = 0; //initialize input from potentiometer
int state = 0;

void setup()  
{
  Serial.begin(9600);  //set baude rate to 9600 [standard communication rate for LCD screens]
  pinMode(button, INPUT_PULLUP); //use internal pullup resistor on button input pin
  pinMode(powerOut, OUTPUT); //set A2 to be a power output
  pinMode(pot, INPUT); //set A1 to be input for potentiometer
  LCD.begin();//Initializing display
  LCD.backlight();//To Power ON the back light
  delay(100); //pause for 100ms
  LCD.clear(); //clear screen
  delay(200); //pause for 200ms
  digitalWrite(powerOut, HIGH); //set A2 to constantly output 5V
}
 
void loop() // run over and over
{
  digitalWrite(powerOut, LOW); //set A2 to constantly output 0V
  LCD.setCursor(0,0);
  LCD.print("  DM BLAISE'S DIE  ");
  state = digitalRead(button);
  potVal = analogRead(pot);
  delay(10);
  if (potVal < 25){ //if potentiometer value is less than 25..
    upperLimit = 3;  //set upper limit to 3 (random function is upper limit exclusive so this correlates to a D2)
  }
  else if (potVal < 50){
    upperLimit = 5; //D4
  }
    else if (potVal < 75){
    upperLimit = 7; //D6
  }
    else if (potVal < 100){
    upperLimit = 9; //D8
  }
    else if (potVal < 125){
    upperLimit = 11; //D10
  }
      else if (potVal < 150){
    upperLimit = 13; //D12
  }
      else if (potVal > 150){
    upperLimit = 21; //D20
  }
  LCD.setCursor(9,1); //column 9, second row down
  LCD.print("D"); //print D
  LCD.setCursor(10,1); //column 10, second row down
  LCD.print(upperLimit-1); //subtract 1 to reveal actual value
  LCD.print("     "); //space to overright old data printed on screen
  if (state == LOW){ //if the button has been pressed (0V connected to A0)
    LCD.setCursor(0,0); //set curser at first pixel
    LCD.print("  DM BLAISE'S DIE  "); //center text on top row
    generating = 1; //state of loop is 1 
    randomNum = random(1, upperLimit); //generate a number between 1 and set value (upper value EXCLUSIVE)
    LCD.clear(); //clear screen
    LCD.setCursor(9,1); //column 9, second row down
    LCD.print("D"); //print "D" for Dice
    LCD.setCursor(10,1); //Column 10, second row down
    LCD.print(upperLimit-1); //subtract one from upperLimit to reveal the die number
    LCD.print("     "); //space
    LCD.setCursor(9,2); //column 9, third row down
    LCD.print(randomNum); //print the random number
    delay(100); //delay 100ms to debounce the button
    while (generating == 1){ //stay in a loop until the button is released
       if (digitalRead(button) == HIGH) { //if button state returns to high
       generating = 0; //set state variable to 0
       delay(100); //delay 100ms
    }
  }
}
}

Credits

Isabellaw55
0 projects • 0 followers

Comments