Electronic Dice Roller

Introduction

This post details a project I recently finished, an electronic dice roller. My goal for this device was to display the result of a simulated “dice roll”, allowing for easy and quick generation of the sum of random numbers from 1 to n, where n is chosen by the user. This can be helpful in many role-playing games such as Dungeons and Dragons, where there are many times you need to roll something like two six sided dice (2d6), one twenty-sided dice (1d20), or three eight-sided dice for example (3d8).

Layout and Planning

After a few iterations of different screen designs and button layouts, I settled on a final design.

Preliminary design sketches/ideas:

(click to enlarge images)

My finalized design employed the use of a 128×64 pixel OLED display, mainly because of its flexibility: I could easily change my graphical interface as opposed to, say, seven-segment LED displays which severely limit the number of digits that can be displayed. They also are more complicated to use because you need many output pins and therefore would be forced to add extra components such as shift registers to make it work with a microcontroller of a limited number of pins.

The screen I got on Amazon also is dual color, in the sense that the first 16 rows are yellow, and the rest blue. This was perfect for my application because it fits my graphical layout (number of dice rolled at top in yellow, and blue for the larger text result). Plus, it just looks nicer than a single-color screen.

Final Schematic
My final sketched schematic and plan for laying out components

Apart from viewing the screen, the user interacts with the dice roller by pressing the “roll” button (the red upper button) which rolls the selected number of dice with the selected number of sides, or the “add” button (the yellow lower button) which adds a new roll to the number already displayed on the screen. The number of dice and modifier can be seen at the top yellow portion of the screen, and the roll result in a blue larger font at the bottom. If the result is greater than three digits, an error message is shown, as only three digits can fit on the screen.

buttons gif
The device in use: first rolling 1d20 (the default value), then the user changes it to 2d8-3, rolls a few times, and then presses the “add” button to add a number of rolls together.

The values for number of dice rolled, number of sides, and the modifier added or subtracted can be easily changed by using the knob: when pressed, it cycles which area is highlighted, and the value can be modified by turning the knob clockwise or counterclockwise. This makes changing settings quick and intuitive, and allows for very quick rolling of various amounts of dice.

screen gif
Close-up of the screen

Prototyping

IMG_1167

Now I had to program my Arduino to do what I needed it to: interface with a screen, read the input from two buttons, and adjust values based on a rotary encoder.

Luckily, communicating with the screen was made quick and simple because of Adafruit’s Libraries. (SSD1306 for interfacing with the driver chip for the screen, and their GFX library for quickly and easily adding text or shapes to the display.)

The rotary encoder used external interrupts, which are a very useful feature that the ATMega328 has and the Arduino IDE has support for. It allows code to be ran only when a pin changes, and otherwise will not slow down the main loop() program by polling with the digitalRead() function. You can read more about this useful feature here: http://playground.arduino.cc/Code/Interrupts

Construction

After prototyping my design with my Arduino Uno and a breadboard, I arranged each part on a proto board. I also decided to use a bare ATMega328 chip because of its ease of use from prototyping on my Uno and integrating it into my final circuit. Because it is the chip that runs my Arduino, putting it in my final circuit is as easy as popping the chip out, soldering the connections in place, adding a 16Mhz crystal oscillator and 22pF capacitors, and adding a pull-up resistor to stop the reset from activating.

Figuring out where to most efficiently place components
Figuring out where to most efficiently place components

From the beginning, I wanted this project to run on an external battery, and a 9V battery seemed perfect for this task. I used a 5V linear voltage regulator to supply the microcontroller and display with the correct voltage. I added a 10uF capacitor on the output because it needs it for stability reasons, and a 0.1uF decoupling capacitor, though this is probably not necessary.

Finally I added the screen, which partially covers the voltage regulator and microchip to keep the device as compact as possible. After that was done (and after testing to make sure it all actually worked), I cut the extra protoboard to size, to make it fit better inside a case.

I also connected a power switch in series with the battery source to allow it to be turned on and off. And finally it works! A small, compact self-contained package that now does exactly what I want: to roll dice. All I need to do is build a case to house my newly-soldered electronics.

The guts of the device, before the housing was made
The guts of the device, before the housing was made

Instead of using a generic black box housing, I wanted to make my own unique case. I also liked the idea of making it out of metal and luckily I had some scrap laying around from taking apart an old fax machine.

IMG_1316
The entrails of the fax machine, spread out on my table. The scrap metal can be seen in the bottom right of the image (the long grey rectangle with slots cut out)

To shape the metal to the case, I  started by using calipers to measure my electronics, and made a 2d CAD image of the screen, knob, and button layout in SolidWorks to aid in cutting in the right spots. Next, I printed this on a piece of paper at 1:1 scale, cut the paper to shape, and taped my image onto the metal sheet. After using a marker to transfer the cutout shapes over, I had a template to cut holes for the case that were placed in the exact right spots.

I then used a dremel with a cutting wheel attachment to make the hole for the buttons and screen, and a large drill bit to make the circle for the knob. If I were to redo this step, I would have drilled out my button holes as well, as it is very difficult to make square holes of the size I needed, and I ended up using round LEDs to reach the buttons anyway. Oh well, now I know how to do it better next time.

After I cut the holes in the front plate of the case, I also cut a piece for the back and used my dremel with a different attachment to sand away the metal and change the finish from a matte look to a snazzier brushed metal look.

After I cut the metal to size for the front and back, I needed to find something to reach the buttons, and needed some sort of spacer to stop the whole thing from squishing together too much, at least enough so the buttons would not be pressed continuously.

As you already know, I used red and yellow LEDs not as a source of light, but to reach the momentary buttons soldered to the board. These actually worked astonishingly well, after some modifications. First I cut off their leads and added athin ring of hot glue around their base as a way to prevent the LEDs from sliding out of the case when it was fully assembled. One thing I actually could have done  is make the buttons themselves light up when pressed, but at this point in the project I just wanted to have a finished project. Maybe I’ll make some cool light-up buttons on the next thing I make.

I made the spacers out of fairly small aluminum tubing, and hot glued them in place. This isn’t the most rugged case, but it should do for my purposes. I spaced the back far enough to easily fit a 9V battery, hot glued the switch to the front side of the case, and closed it all up with black gaffer’s tape.

And, now it’s done!

Finished!

I’ll leave you with some more images of the device. Thanks for reading!

_MG_8333
Front View
_MG_8334
Rear View
_MG_8335
Side View (right)
_MG_8331
Button close-up

Materials

  • 5V Linear Voltage Regulator (LM317?)
  • 2 x 22pF Capacitors
  • 16Mhz Crystal Oscillator
  • 10 uF Capacitor
  • 2 x Momentary push buttons
  • Atmega 328 Microcontroller
  • 1 x Rotary Encoder with integrated pushbutton
  • Knob for Rotary Encoder
  • 3 x 10 kOhm Resistors
  • 128×64 pixel OLED display
  • Solder and wire for connections
  • Prototype Board
  • 9V Battery snap leads
  • On/Off switch (SPST)

Case:

  • Hot glue
  • Dremel for cutting protoboard and creating case
  • Scrap metal for case
  • Black tape
  • Thin tubing for spacers

Winning the AAPT Photo Competition

This news is a bit old (from 2014), but I thought I should post it because I would like this site to log some of my major accomplishments.

In July 2014 I won the American Association of Physics Teachers’ high school physics photo contest. AAPT is an organization that collects and judges thousands of entries from hundreds of different schools across America and internationally.

The photo I submitted won first place in the contrived category (as opposed to natural), and I am very proud of the work and effort I put into it. I’d like to thank Mr. Lapp, my high school physics teacher, for encouraging his students to take excellent photos and doing his best to connect with every student to teach them in the best way he can.

You can find my photo and its accompanying essay on AAPT’s website here: AAPT Winner 2014, Contrived

Taking the Photo

I went through many iterations trying to take the best photo I could. I also didn’t have any way of triggering my flash to capture the droplet at an exact time, so each shoot I took at least 100 photos and picked out the few best timed and in focus shots.

My camera and background setup

Here’s how I set my camera up. In my first round of photos I used a plain white background, and I didn’t know how my photos would turn out. The key to droplet photography is the external flash, which you can see placed to the right of the small tupperware container in the photo above. With the aid of a darkened room, this makes the only light exposed on the camera’s sensor be at the exact desired instant, in this case right after the droplet hits the water. Coupled with a steady tripod and macro lens (or in my case the much less expensive set of macro extension tubes for my ordinary lens), this  is a recipe for taking up-close shots of the exact instant of a droplet hitting the surface of water.

_MG_8458
My personal favorite of this shoot

_MG_8390 _MG_8415 _MG_8441

This turned out much better than I expected, and from that moment on I knew that I had a passion for droplet photography.

Fast forward a little bit, and I was now thinking of interesting photo ideas relating to physics. I knew that water droplets diffracted light, so my first idea was to use a background and see how the droplet altered the image I saw.

_MG_2985

This was somewhat interesting, but now I was thinking of optics; I had seen droplets with a sort of “i” shape, with a column of water and a spherical droplet at the top. I knew spheres acted as converging lenses, and could display an inverted image of a real object. So I tested it by writing the word “physics” upside-down on a piece of paper and trying it out.

_MG_3125Success! So after that I printed a more colorful “Physics” image, trying to evoke more ideas of light, refraction, and wavelengths of light by using rainbow. Plus, why not show a bit of pride? So that’s how I ended up with my final image: a lot of iterations, many out of focus and mis-timed photos, and finally a successful finished image.

Water Droplet Acts as Converging Lens

I hope you enjoyed reading about my physics photo, and just as a bonus for reading this far I’ll show you some more physics related photos I took in Mr. Lapp’s physics class. Some also appear in his self-written physics textbook.

curved mirror - Ted-Myers
This image shows how a concave mirror (in this case a vanity/shaving mirror) can cause an image larger than the real object (in this case a silver whistle)
TIR - Ted-Myers
This photo illustrates total internal refraction, when the difference in optical characteriscis of two mediums causes a mirror-like effect where all light if reflected past a certain degree form the normal. This caused a double-headed quarter effect
Static Electricity - Ted-Myers
A spark of electricity bridging two copper wires, illustrating how high voltage electricity can ionize air in its effort to find the path of least resistance
First Law of Motion - Ted-Myers
A photo I actually took about a year prior to the class, this photo illustrates Newton’s first law of motion; that an object at rest tends to stay at rest. In this case, the water in a balloon keeps its shape for a short while after the outer balloon has been popped
Uniform Circular Motion - Ted-Myers
This is a drill spinning a piece of burning steel wool, creating streaks tangential to its otherwise circular path when small molten fragments fly off. This shows that the centripetal force acts towards the center of rotation, and otherwise would travel in a linear path
Capturing the popping of a balloon via a microphone and the flash device

Flash Trigger Device Using the ATtiny85

Making a Flash Trigger using Arduino code on an ATtiny85

Front view of the flash trigger deviceBackside of Flash Device

Introduction

In this post I’ll go over how I created a high-speed flash triggering device, as well as detailing how it works. Since creating this project I have learned a lot more about electronics and using the Arduino platform, and thus there parts of this device I would do differently now. But even with the mistakes I made then, this project was a great way to get me into prototyping my own devices that stand alone and perform a useful function.

What is it?

First, lets go over what it does: When a laser is crossed, an external camera flash is triggered.

The flash can also be triggered by a microphone, which will cause the flash to fire once a certain noise threshold has been reached.

So, why is this useful? A controlled flash makes it a lot easier to get consistent high speed photos. Think of this example: you want to capture a falling milk droplet right as it hits a surface; the idea is that the laser and sensor are set up so the droplet will cross the laser and cause the flash to fire, causing light to hit the camera’s sensor only at the instant that the laser is crossed.

In my device, I also added a display and adjustment knob so I can add a very specific delay, which is useful when you want the flash to fire later, so I can more easily capture specific instants after the droplet splashes up again. This allows for more flexibility in the laser placement.

Here’s a photo I took with the same scenario that I just described:

droplet

Note: The delay was actually one of my problems: due to the way my code ran, it caused too much delay. This is because the main loop of my program took too much time to run, as it would read the analog values of various pins on my microcontroller and output the delay to a screen. Were I to do this project again on the same platform, I would use external interrupts which allow a much faster execution time, as they only execute their relevant code the instant after the signal is received and don’t have to take time out of the main code loop to poll a sensor.

Now that you know the basics of what it does, lets go over how it works.

How it Works

I started by using my Arduino Uno to make an external camera flash fire. It’s a fairly simple procedure to make an external camera flash fire with an arduino. First, lets go over how the flash triggering works. Most external flashes have a port on the side, to plug in what is called the PC sync cable. You make the flash fire by completing the circuit between two terminals using this cable. This is also possible on most flashes through the hotshoe mount, which allows for proper flash firing when attached to a camera.

Because there’s a lot of current running through these wires for a short period of time, we need to isolate it from the arduino circuit. I used a photocoupler, which has a small light-emitting diode and a sensor in it. This means that when a voltage is applied through two of the pins, current can freely flow between two other pins, allowing us to control current without directly allowing it to flow through our arduino circuit (and thus isolating it). Interestingly enough, there is a small LED inside the DIP package, meaning a current-limiting resistor needs to be placed in series with the input pins. It is the lighting of this LED which allows the current to flow on the other two pins.

Here’s a demonstration of the photocoupler in action, allowing an LED to be lit up.

ooh, flashy lights!
When the input (left side) of the photocoupler is connected to power, the LED is able to draw current and light up. Notice that 330 Ohm resistors are used on both sides, because there is an LED inside the photocoupler, and the red LED on the right also needs a current limiting resistor.

(Shot of LED lighting up when the two pins are applied current, or a graphic showing current flow with arrows)

So all we need to do to make a flash fire is use our arduino to create a +5V signal on one of its pins, place a resistor in series so it doesn’t burn out the photocoupler, and connect the two wires coming from the flash to the other side of the photocoupler.

Now that the arduino can cause our flash to fire, we need to find a way to trigger the arduino’s command. I chose to use a laser and a photoresistor to make the flash fire at the instant the light from the laser ceases to shine on the photoresistor.

An arduino can only directly measure voltage, not resistance; therefore we need to set up a voltage divider with another resistor connected in series to the photoresistor for the arduino to obtain useful data about light. This way, when the photoresistor changes its resistance, we can quantitatively measure it. And of course, we’ll need a laser. You can get cheap red lasers nearly anywhere these days. In fact, I ordered ten little 5V modules for less than five dollars. I soldered one of these onto some longer wires and a tripod mount by cutting ¼” threaded metal couplers in half and taping them on.

You could do this, or use any other bright light source, as long as it will constantly stay on the photoresistor until you want the flash to be triggered. Now that we can get data about light hitting the sensor, we’re ready to start with our program.

Programming and the ATtiny

Getting into the Arduino IDE

I won’t go too far into the code I wrote, but I’ll try to touch on everything I needed to make the device work.

First, I prototyped my device using an Arduino Uno. When I was making this, (Winter of 2014), I had only been working with electronics and the Arduino platform for maybe a month or two, when I found time during school. It was during winter break that I was really able to dig into it more and make a larger project such as this. I also continue to be amazed by the support and communities around Arduino and how easy it is to set up  a project and find answers to any questions that pop up while working on electronics.

One thing that I wanted to do with this project was have a nice, easy-to-read display. I had ordered a bunch of 7-segment displays online for pretty cheap and I like the look of them so I thought I’d go with that. Because  you need to be able to control a lot of outputs to light the LEDs inside the display, I used shift registers (74HC595) to control the display.

Shows how I connected each pin of a shift register to a 7-segment display
How I connected each pin of a shift register to a 7-segment display

I didn’t know about shift registers before making this, and I think they’re a really interesting way to expand the number of pins you can control by using only three outputs on a microcontroller. It works by having a communication protocol that uses three pins: one as a clock, to tell the register when to store each bit; one to actually send the data, as a digital 0 or 1 by sending a 0V or 5V signal; and the last one as a “latch”, to tell the shift register to output the data, causing it to create a digital signal on its eight output pins, corresponding to the data that was sequentially sent to its data pin.

Here’s a video with more information about shift registers, if you want to learn more: https://youtu.be/6BLj9Ak2Djs

With the help of four shift registers, I was able to control the values displayed on four seven-segment displays.

After figuring out how to use my displays, I just had to program and put it together. I used a 10kOhm potentiometer to select how many milliseconds of delay time I wanted. I am able to do this because the Arduino has an ADC (analog to digital converter), so it is easy to map its voltage to a 0-1000 millisecond delay.

My code allows for selecting a sound mode, or a laser mode. Each reads an analog value, but this time from a pin which is connected to either a microphone unit or the photoresistor mentioned earlier in its voltage divider setup. When one of these crosses a certain voltage threshold (which was fine-tuned by me earlier), it sends a signal to the photocoupler, allowing current to flow through the flash.

Flash fires after crossing laser threshold with finger
Flash fires after crossing laser threshold with finger

So generally, that’s how it works. Power, the sensor, and the flash unit are connected with screw terminals; there are four 7-segment displays which display the amount of delay; and the main input is a potentiometer that you turn to adjust values and enter the menu to change settings.

Next, we need to load the code onto an ATtiny. Luckily, there is a fairly easy way to do this with an Arduino Uno. With only a 10 micro-Farad capacitor and an Arduino, the serial data from your computer can be loaded onto the very small ATtiny chip. This website, http://highlowtech.org/?p=1695, has complete details on how to do it. With the new Arduino IDE, you can even download their libraries very easily, and can easily run the Uno as an ISP.

This is a key step that allowed me to move past the need to connect my Uno each time and let me make a stand-alone unit.

Construction

It was a lot of work soldering everything all together, especially with every connection I needed for the seven-segment displays. I added a 1/4 inch audio jack, which connects to the flash via the PC sync cable. Next, everything was wired up to an 8-pin IC socket adapter, where I could plug in the ATtiny85. Four shift registers were added, the potentiometer, and the screw terminals.

Here’s some images showing my planning process and the wires being soldered into place:

IMG_0749 IMG_0824 IMG_0825

After all that work, I finally had a working product! I decided to also make a nice plexiglass housing, to make it a little more robust and create a more “finished” look.

IMG_0831 IMG_0832

_MG_8101 _MG_8102

Materials

  • 4x 74HC595 Shift Registers
  • 4x Common Cathode 7-segment displays with “.”
  • 5x 330 ohm resistors (I used 1/6W)
  • 10k Potentiometer and knob
  • female ¼” female threaded connector
  • ATtiny85 and 8-Pin IC holder
  • Screw terminals
  • Various colors of ~22 gauge insulated wire
  • 1-2x 10uF capacitors
  • 3x 0.1uF capacitors
  • 1x Photocoupler (I used a 4-pin)
  • Laser Modules
  • Optical Resistor and 10k Ohm resistor
  • Double-sided Prototyping Board

Optional (for housing)

  • 4x Machine screws
  • 4x Nuts
  • 4x Washers
  • 4x Lock washers
  • Aluminium Tubing

Done!

Front view of the flash trigger device
Front view of the flash trigger device

Conclusion

And finally, I have a finished product! As I mentioned earlier, there are a few things I would do differently if I were to make this again, but I am very happy with what I made. I went from very little experience with electronics to making a finished product with a flashy screen, working input, and ability to improve my photography. I hope you have enjoyed learning about what I have made, and I’ll leave you with some photos that I have taken with this device.

Photos

_MG_7869 _MG_7918 _MG_7931 _MG_7935 _MG_7944 _MG_7960 _MG_7967 _MG_7976 droplet