Saturday, October 2, 2010

shakeable 20 sided die with arduino

Shakeable 20 sided die with arduino.

Here is a video



Sparkfun 7 segment serial display

3 axis accelerometer from Pololu


/* Shakeable 20 sided die.

Y axis on accelerometer goes to analog pin 1 on arduino
VCC and GND on accelerometer to their respective locations

RX on display to digital I/O pin 12 on arduino
VIN and GND to their respective locations

This code requires the newsoftserial library. Make sure you have it installed

My code is copyright under the Gnu General Public Licence (GLP) version 3 or any later version
The text of the GPL can be found at


#include //Make sure you have this library installed,

#define SerInToArdu 2 // input pin to arduino. Not used in this example
#define SerOutFrmArdu 12 // output pin from arduino to display

NewSoftSerial mySerialPort(SerInToArdu,SerOutFrmArdu); /* creates a serial channel to send the data to and from the display. */

int y = 0; // variable to store accelerometer value

int ytrip = 0; //variable to indicate if accelerometer has been tripped

long ran; // variable to store random number in

void setup() {

pinMode(SerOutFrmArdu,OUTPUT); //sets pin to output
pinMode(SerInToArdu,INPUT); /*Not actually needed... put in to be explicit as to data direction over serial lines */


randomSeed(analogRead(0)); // seed for random number generator using value of unused analog pin

mySerialPort.print("v"); //a special code to reset the display.
mySerialPort.print("diCE"); //send word "diCE" to display


void loop() {

y = analogRead(1); //read Y axis value from accelerometer

if (y > 400) ytrip++; // if accelerometer value is greater than 400 then set ytrip to 1

if (ytrip == 1){ /* check if accelerometer has been tripped

Initiate for loop. Displays 10 random numbers on the display. Last number stays on display

for (int num = 0; num <= 10; num++) {

ran = random(1, 20); //generate random number from 1 to 20

mySerialPort.print("v"); //reset display.

if (ran < 10) mySerialPort.print("0"); //display 0 as first digit if ran is less than 10

mySerialPort.print(ran); //send value of ran to display



if (ytrip > 0) ytrip = 0; //reset ytrip to 0