Arduino D&D Project: Attribute Roller

So I’ve been playing a lot of Dungeons and Dragons lately (and I mean, a lot.  2-3 hours a day, 4-5 days a week) and it got me to thinking: As much as I like rolling the dice sometimes it is just easier to have a thing that does it for you.  There are plenty of websites that will do a dice roll, and even different kinds of dice rolling.  It isn’t all that difficult in actuality to program a computer to do this.  Even Google’s search will respond to “Roll a dice” and the like.

Well, Dan got me the Sunfounder Super Kit (which happens to include a handy-dandy display ) for my Arduino Uno R3 which got me thinking: I bet I can make my Arduino roll dice for me.  Let’s start with a simple one (this is going to be an ongoing series I think, as I keep coming up with extra things to implement): rolling for attributes.

If you have never played Dungeons and Dragons before let’s start with some basics.  You create a character and one of the first steps to creating a character is to roll for your attributes (Strength, Dexterity, Constitution, Intelligence, Wisdom, and Charisma).  You can choose to accept a standard set of values for this (15 14 13 12 10 and 8) or you can attempt to roll better scores.  To do this you roll 4 6-sided dice and add up the 3 highest scores.  You roll 6 sets of these and boom: your attribute scores (enter them wherever you want).

My Attribute Roller V1 generates 4 random numbers, then sums the 3 largest numbers.  It then displays the 4 dice and the total score.

2016-01-13 15.51.20

To complete this project you will need:

  1. 1x Arduino Uno
  2. 1x breadboard
  3. Assorted lengths of connecting wire
  4. 1x standard display (1602)
  5. 1x 1/6W 5% 330 Ohm Resistor
  6. 1x Potentiometer (I used one that came with a Vilros Ultimate Starter Kit which is a 10K Trimpot)

Let’s start with the easy part: code for the Arduino.

D&D Attribute Roller v1 (INO file)

The above file is for use in the Arduino code suite, available here.

The code is presented as follows:

/*
LiquidCrystal Library – Hello World

Demonstrates the use a 16×2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.

The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 13 Jan 2016
This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/LiquidCrystal

by Michael Santangelo
– Now rolls 4d6, drops lowest 1, adds the remainder for an attribute score.
*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
//Serial.begin(9600);
randomSeed(analogRead(0));
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(” D&D Roller “);
int diceRolls[4];
int countRoll = 0;
int curRoll = 0;
int curDis = 0;
int minRoll = 0;
int thisRoll = 0;
int sumRolls = 0;
while (countRoll<4){
curRoll = random(1,6);
//Serial.print(curRoll);
//Serial.print(” “);
diceRolls[countRoll]=curRoll;
countRoll++;
}
//Serial.println(“”);
lcd.setCursor(0,1);
while (curDis<4){
thisRoll = diceRolls[curDis];
if(curDis==0){
minRoll=thisRoll;
}
else{
if(thisRoll<minRoll){
sumRolls=sumRolls+minRoll;
minRoll=thisRoll;
}
else{
sumRolls=sumRolls+thisRoll;
}
}
lcd.print(diceRolls[curDis]);
lcd.print(” “);
curDis++;
}
lcd.print(“Score:”);
lcd.print(sumRolls);
countRoll = 0;
curRoll = 0;
curDis = 0;
minRoll = 0;
thisRoll = 0;
sumRolls = 0;
}

void loop() {
while(1);
}

Upload this code to your Arduino Uno and then turn it off so we can connect things.  You can click the image below to see a full-size version, which shows how I connected everything.  You don’t need to use the same color wire obviously, nor do you need to do the jumper method (connecting the top to the bottom of the breadboard instead of just connecting the pins directly) but this is just how I like to do things for prototyping.

2016-01-14 10_30_00-D&D Roller.fzz - Fritzing - [Breadboard View]

The above image was generated using Fritzing, which I highly recommend.

You will need to connect:

  • Arduino Ground to: LCD Pin 16 (K), LCD Pin 1 (VSS), LCD Pin 5 (RW), and Potentiometer Pin 1 (-)
  • Arduino 5V to: LCD Pin 2 (VDD), the 330 Ohm Resistor which then connects to LCD Pin 15 (A), and Potentiometer Pin 3 (+).
  • Arduino Pin Digital 2 to: LCD Pin 14 (D7)
  • Arduino Pin Digital 3 to: LCD Pin 13 (D6)
  • Arduino Pin Digital 4 to: LCD Pin 12 (D5)
  • Arduino Pin Digital 5 to: LCD Pin 11 (D4)
  • Arduino Pin Digital 11 to: LCD Pin 6 (E)
  • Arduino Pin Digital 12 to: LCD Pin 4 (RS)
  • Arduino Pin Analog 0 to: Open Wire [This is necessary to properly get random data for the seed so we don’t roll the same numbers every time we reset the Arduino.]
  • Potentiometer Pin 2 (Output) to: LCD Pin 3 (V0).

Connect the pins and turn it on.  You should get a proper display with “D&D Roller” followed by 4 numbers (the rolled dice) and then your Score.  Press the reset button to get another set.

Things to come in the next version:

  1. A physical button to get a new roll
  2. Roll 6 sets of dice, use 2 buttons to scroll up and down through the set of dice.

-M

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.