Digital Die

Come on lucky number! Use this digital die next time you play a board game.
You “throw” the die by shaking a tilt sensor. The LEDs will cycle through different numbers, slowing down until it finally stops and reveals the number.

Materials

  • 1 Arduino Uno board
  • 1 Education Shield
  • 1 TinkerKit tilt switch
  • 9 LEDs
  • 9 220 ohm resistors
  • 1 TinkerKit wire
  • 6 black jumper wires
  • 9 colored jumper wires

 

Instructions

  1. Attach the shield onto the top of the Arduino board.
  2. Connect three LEDs to the breadboard.
  3. Connect a 220 ohm resistor to digital pin 2. Connect the other end of the resistor to the long leg of the first LED.
  4. Connect digital pins 3 and 4 to the corresponding LED following the same method.
  5. Connect the short leg of the LEDs with a black wire.
  6. Connect three more LEDs to the breadboard.
  7. Connect digital pins 5 to 7 to the corresponding LED in series with a 220 ohm resistor.
  8. Connect all short the legs of the LEDs using 2 black jumper wires.
  9. Connect the final three LEDs to the breadboard.
  10. Connect digital pins 8, 11 and 12 to the corresponding LED in series with a 220 ohm resistor.
  11. Connect the short legs of the LEDs using black jumper wires, and connect them to GND.
  12. Connect the TinkerKit tilt switch to D9.
  13. Connect the Arduino assembly to the computer, upload example Digital Die and try the game.

Code

Find the code in File>Examples>EducationShield>Block2-Sports>Projects>DigitalDie

How it works

  • The EducationShield library is included.
  • The LED variables are declared, pinCount and ledPins[].
  • The TiltSwitch object is declared, ts.
  • The die patterns are defined in a two dimensional array, dice[6][9]. A die has 6 different sides, and on each side there are 9 potential spots for dots.
  • The die patterns are defined in a way to make it easy to visualize. In each pattern (each die side) there are nine zeros and ones. A 1 represents an LED turned on (a dot), and a 0 represents an LED turned off (blank)
  • The variable waitTime is declared to hold the time in milliseconds to wait between the change of the pattern displayed.
  • In setup(), the led pins are configured as outputs.
  • The tilt switch object is initialized.
  • The random number generator is initialized.
  • In loop(), waitTime is set to be equal to 2.
  • While waitTime is less than 1000, the following happens.
  • The variable value is declared and assigned a random value between 0 and 6.
  • value is passed to the function displayDice() and the program jumps to that function.
  • In displayDice(), a for loop loops through the LEDs and turns them on or off according to the patterns in dice[][]. When value is equal to 2, the 9 zeros and ones of dice[2][] (the pattern that shows 3) will be looped through.
  • The program jumps back to the while loop.
  • waitTime is increased by being multiplied with 1.3. This means that the 25th time the while loop runs, waitTime will be more than 1000.
  • The program pauses for as many milliseconds as the value of waitTime.
  • If waitTime is less than 1000, the while loop keeps looping, displaying the next randomly chosen pattern.
  • When waitTime finally is not less than 1000, the program leaves the while loop.
  • The program pauses until the tilt switch is pressed, or tilted.
  • loop() continues to loop.

Troubleshooting

  • If the die does not show the correct combinations of turned on LEDs, refer to the illustrations and double check all your connections. Make sure the shield and jumper wires are firmly connected. Check the LED polarities to make sure they are consistent and properly wired.
  • If your tilt switch is not working correctly, make sure that it is in an upright position when the die is still “rolling”. Check the reference for debugging the tilt switch.

Learn by doing

  • Change the patterns: make the LEDs display something else.
  • Build a case: Use a 9V battery to power the Arduino and design a case so that you can simply shake the box to have it “roll the die”.