Dungeons and Dragons Dice Gauntlet
Adding the Code
This code is going to tell the Arduino to roll a random number when it gets input from the Y-axis of the accelerometer. The limit of the random number is determined by which switch you flip. You can certainly make any changes to this code that you'd like, but it will work as it is if you'd rather not. Hook up your FTDI board to your computer and the Arduino board. Then cut and paste this code (or download it from here), and upload it to the LilyPad.
language:c
/* Accelerometer controlled Random Number Generator using LilyPad
Arduino and accelerometer
by: John Trepke and Brad Woodward 1/31/12
SparkFun Electronics
created on 1/31/12
license: Beerware- feel free to use this code and maintain
attribution. If we ever meet and you are overcome with gratitude,
feel free to express your feelings via beverage.
Arm movement will generate a random number between one and
selected maximum.
Hardware: LilyPad Main Board
*/
const int buttonPin1 = 3; // Switch #1 on the DIP switch is digital pin3 on Arduino.
const int buttonPin2 = 4; // Switch #2 on the DIP switch is digital pin4 on Arduino.
const int buttonPin3 = 5; // Switch #3 on the DIP switch is digital pin5 on Arduino.
const int buttonPin4 = 6; // Switch #4 on the DIP switch is digital pin6 on Arduino.
const int buttonPin5 = 7; // Switch #5 on the DIP switch is digital pin7 on Arduino.
const int buttonPin6 = 8; // Switch #6 on the DIP switch is digital pin8 on Arduino.
const int buttonPin7 = 9; // Switch #7 on the DIP switch is digital pin9 on Arduino.
const int accel = 1; // Accelerometer's Y axis is anolog pin 1 on Arduino
int highnum = 0; // Variable to hold the high value for 'random()'
long ran; // Variable to store random number in
void setup() {
pinMode(accel, INPUT); // Sets analog pin 1 to input.
pinMode(buttonPin1, INPUT); // Sets digital pin 3 to input.
pinMode(buttonPin2, INPUT); // Sets digital pin 4 to input.
pinMode(buttonPin3, INPUT); // Sets digital pin 5 to input.
pinMode(buttonPin4, INPUT); // Sets digital pin 6 to input.
pinMode(buttonPin5, INPUT); // Sets digital pin 7 to input.
pinMode(buttonPin6, INPUT); // Sets digital pin 8 to input.
pinMode(buttonPin7, INPUT); // Sets digital pin 9 to input.
digitalWrite(buttonPin1, HIGH); // Sets digital pin 3 HIGH.
digitalWrite(buttonPin2, HIGH); // Sets digital pin 4 HIGH.
digitalWrite(buttonPin3, HIGH); // Sets digital pin 5 HIGH.
digitalWrite(buttonPin4, HIGH); // Sets digital pin 6 HIGH.
digitalWrite(buttonPin5, HIGH); // Sets digital pin 7 HIGH.
digitalWrite(buttonPin6, HIGH); // Sets digital pin 8 HIGH.
digitalWrite(buttonPin7, HIGH); // Sets digital pin 9 HIGH.
Serial.begin(9600); // Start a 9600 baud Serial
randomSeed(analogRead(0)); // Seed 'random()' using an unused analog pin.
Serial.print("v"); // Reset the display
Serial.print(0x7A, BYTE); // 'Brightness'
Serial.print(0x00, BYTE); // = MAX!
delay(100); // Wait 0.1 second
Serial.print("d8d"); // Send "d8d" to the display.
}
void loop() {
if (analogRead(accel) > 400) { // First trigger
for (int num = 0; num <= 100; num++) { // 100 cycles
if (analogRead(accel) < 300) { // Second trigger
trigger(); // let's Roll!
}
}
}
}
void trigger() {
if (digitalRead(buttonPin1) == LOW) {
highnum = 5; //If switch #1 is flipped on, random 1-4.
} else if (digitalRead(buttonPin2) == LOW) {
highnum = 7; //If switch #2 is flipped on, random 1-6.
} else if (digitalRead(buttonPin3) == LOW) {
highnum = 9; //If switch #3 is flipped on, random 1-8.
} else if (digitalRead(buttonPin4) == LOW) {
highnum = 11; //If switch #4 is flipped on, random 1-10.
} else if (digitalRead(buttonPin5) == LOW) {
highnum = 13; //If switch #5 is flipped on, random 1-12.
} else if (digitalRead(buttonPin6) == LOW) {
highnum = 21; //If switch #6 is flipped on, random 1-20.
} else if (digitalRead(buttonPin7) == LOW) {
highnum = 101; //If switch #6 is flipped on, random 1-100.
} else {
highnum = 1; // Use highnum == 1 to denote no switches flipped
}
if (highnum > 1) { // Is a switch flipped?
for (int num = 0; num <= 5; num++) { // Roll 5 times, keep the 5th number
Serial.print("v"); // Reset the display
ran = random(1, highnum); // Get a number
if (ran < 10) { // If the random number is 1 digit,
Serial.print("0"); // Put a 0 in front of it
}
Serial.print(ran); // Show the number
delay(100); // Wait 0.1 second between the faux-rolls
}
} else { // No switches flipped?
Serial.print("v"); // Reset the display
Serial.print("DURR"); // User == idiot
}
delay(1000); // Wait 1 second to prevent accidental re-rolls.
}
If you've never used Arduino before, check out our guides on what it is and how to install it. Also, check out our Beginning LilyPad Arduino tutorial.
Troubleshooting
All of the electronics are done! You can hook up a battery, turn it on, and check that it works. If you have problems, go ahead and cuss or throw something (soft, gently) if you want. It's cool -- I'll wait. It's frustrating when things don't work the first time, every time, but don't worry, we'll sort it out!
First, check for shorts. Look for ANY places where traces are touching each other that we didn't specifically say they should be connected earlier. Be particularly careful to check where your bridges are at if you put any stitches in them. It's easy to go too far through the fabric and end up with your top trace in contact with the bottom trace.
If you've got a multimeter with a continuity setting on it, that's a great way to check. Just put one probe in contact with your positive trace and one in contact with your negative trace. If it beeps, you've got continuity. There's your problem! Just use the resistance setting to poke around and find the point of lowest resistance, and there you'll probably find your problem. If the continuity check shows nothing, check around a few more places, then check continuity where you SHOULD have it -- from the positive pad of the power supply to the positive pads of the Arduino, accelerometer, and display, then likewise for the negative pads. Look for somewhere that the connection might be broken, and shore it up!
If everything DID work, that's great! From here on out, it's all crafty stuff.