Saturday, October 2, 2010

shakeable 20 sided die with arduino

Shakeable 20 sided die with arduino.

Here is a video

http://www.youtube.com/watch?v=j5M7wRkePJQ


HARDWARE


Arduino

Sparkfun 7 segment serial display

http://www.sparkfun.com/commerce/product_info.php?products_id=9765

3 axis accelerometer from Pololu

http://www.pololu.com/catalog/product/766

CODE


/* 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 http://www.gnu.org/licenses/gpl.html

*/

#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 */

mySerialPort.begin(9600);

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

mySerialPort.print("v"); //a special code to reset the display.
delay(1000);
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

delay(200);

}
}

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


}

No comments: