2.0 Final Product
D&D Digital Dice
D&D Digital Dice
Push Button to Arduino
Pull-up Resistor Circuit
Side 1 --> A2 (5V Output)
Side 2 --> A0 (analog input)
Usues Arduino's internal pullup resistor on pin A0
Potentiometer to Arduino
10K Pot 1 output --> A1
i2C LCD Screen to Uno
Vcc --> +5v
Gnd --> Gnd
SDA --> A4
SCL --> A5
Power Block to Arduino
Cut and strip the striped (+) wire from the power block.
Attach to one side of on/off rocker.
Solder red wire from other side of rocker to barrel connector.
Strip & Solder end of ground wire to barrel connector.
include
#include
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
}
}
}
}