Let’s install all the code and sounds on the boards before assembling the circuit. This makes it easier to handle basic troubleshooting (in case a part needs replacing) before everything’s carefully packed inside the model.

If this is your first time using the Adafruit Pro Trinket microcontroller, it’s recommended that you read through the Introducing Pro Trinket guide first, which in turn will direct you to the Adafruit Arduino IDE Setup guide. Get to where you can upload the basic “blink” sketch to the board…that’s a good indication that all the software parts are present and working.

There are three Arduino software libraries that the d20 sketch depends on. These are easily installed in recent versions of the Arduino IDE (1.6.5 or later). From the “Sketch” menu, select Include Library→Manage Libraries… the install the following:

  • Adafruit MMA8451 Library
  • Adafruit Unified Sensor
  • Adafruit Soundboard library

The first two are needed for the accelerometer board that gives us the d20’s orientation, the third handles communication between the Pro Trinket and Audio FX Sound Board, to trigger specific sounds.

The software and audio files for the Talking D20 can then be downloaded from Github:

The Audio folder contains sound files to be installed on the Audio FX Mini Sound Board. This board plugs into your computer with a Micro USB cable…it should then appear as a small (2 megabyte) flash drive. There may be some sample files already on the drive…if so, those can be deleted…then copy the contents of the Audio folder here. Do not copy the Audio folder itself…copy the files inside…33 at the time of this writing.

The d20 folder contains the Arduino code for the Pro Trinket. Open this in the Arduino IDE, select “Pro Trinket 5V/16MHz (USB)” from the Boards menu and upload to the board (press the reset button to start the bootloader).

If you just want to use the standard code and don’t plan to make modifications, skip ahead to the next page.

Custom Sounds and Behaviors

The sound files are stored in Ogg Vorbis format (.ogg) to save space…it’s a format similar to MP3 but without patent restrictions. Few systems can play or record .ogg files “out of the box” — so if you’ll be making your own sounds, you’ll probably need to download a package such as Audacity to convert audio files to this format.

In addition to the 20 sound files corresponding to each face, there’s a startup greeting plus a few jabs or complements if you make a particularly bad or good roll. These are selected randomly (as is the announcement, “You roll…” etc.) so it’s less repetitive to play with.

If you want to make changes to the code for more sounds or different gags, toward the top of the sketch is this big table:

static const char PROGMEM bigStringTable[] =   // play() index
  "01      " "02      " "03      " "04      "  //  0- 3
  "05      " "06      " "07      " "08      "  //  4- 7
  "09      " "10      " "11      " "12      "  //  8-11
  "13      " "14      " "15      " "16      "  // 12-15
  "17      " "18      " "19      " "20      "  // 16-19
  "ANNC1   " "ANNC2   " "ANNC3   "             // 20-22
  "BAD1    " "BAD2    " "BAD3    "             // 23-25
  "GOOD1   " "GOOD2   " "GOOD3   "             // 26-28
  "STARTUP " "03ALT   " "BATT1   " "BATT2   "; // 29-32

Because of the way Arduino handles strings in program memory, we’re doing something a bit unconventional here: all the filenames are smooshed together into a single large string, but we’re using code formatting shenanigans to make them appear distinct (there are quotes around each filename, but no commas in-between…the compiler joins these into a contiguous string).

Each filename must be padded with spaces if necessary to fill exactly 8 characters. Any more or less will throw the whole thing off!

Declaring these as an array of separate strings in program memory (PROGMEM) would be even uglier…string tables are one situation where Arduino’s PROGMEM syntax just gets nasty.

To play a sound file from this table, pass its index (starting from 0) to the play() function. For example, index #29 is the string "STARTUP " — corresponding to the file startup.ogg, the sound played when the d20 circuit is switched on:

  play(29);   // Startup greeting

If you insert or rearrange sounds, the indices will change, and you may need to edit several places in the code to accommodate the changed order.

This guide was first published on Aug 07, 2015. It was last updated on Mar 08, 2024.

This page (Code) was last updated on Jul 20, 2015.

Text editor powered by tinymce.