I just got my first Arduino project -- an electronic dice roller -- working. It's pretty simple -- maybe a better term would be infantile -- but for someone with no experience in this area it's a bit of a kick.
Attached are some photos (inside and out) and a schematic, and the code is below.
/* Electronic Dice Roller, v. 1.0
By Robert Rothman, 8/16/14 */
#define buttonPin 2 /* Connect roll button to pin2 */
#define bigJuliePin 3 /* Connect Big Julie switch to pin 3 */
#define die0Latch 4 /* Connect latch pin for shift register 0 to pin 4 */
#define die0Clock 5 /* Connect clock pin for shift register 0 to pin 5 */
#define die0Data 6 /* Connect data pin for shift register 0 to pin 6 */
#define die1Latch 7 /* Connect latch pin for shift register 1 to pin 7 */
#define die1Clock 8 /* Connect clock pin for shift register 1 to pin 8 */
#define die1Data 9 /* Connect data pin for shift register 1 to pin 9 */
boolean rolled [] = {false, false};
void setup()
{
pinMode (buttonPin, INPUT);
pinMode (die0Latch, OUTPUT);
pinMode (die0Clock, OUTPUT);
pinMode (die0Data, OUTPUT);
pinMode (die1Latch, OUTPUT);
pinMode (die1Clock, OUTPUT);
pinMode (die1Data, OUTPUT);
pinMode (bigJuliePin, INPUT);
randomSeed (analogRead(0));
Serial.begin(9600);
}
void loop()
{
boolean rollButtonPressed = digitalRead(buttonPin);
boolean bigJulieModeOff = digitalRead (bigJuliePin);
int die;
int dieScore = 0;
int pips[2];
if (rollButtonPressed) /* Wait until roll button is pressed */
{
Serial.println("Button Pressed");
for (die = 0; die <2; die ++) /* All this happens twice, once for each die */
{
rolled[die] = true;
if (rolled[die])
{
if(bigJulieModeOff) /* test to see if Big Julie mode is OFF (i.e., switch is turned ON) */
{
dieScore = rollDie();
Serial.println(dieScore);
pips[die] = figurePips(dieScore - 1);
}
else /* if Big Julie Mode is on, you have to remember where da spots formerly was */
{
pips[die] = 0;
}
showTheRoll(pips[die], die);
}
}
}
}
int rollDie()
{
int number = random(1,7);
return number;
}
int figurePips(int diescore)
{
int pipsLocation[] = {B00001000, B01000001, B00101010, B01100011, B01101011, B01110111}; /* These represent the physical locations of the pips on each side of a standard die. Note that although there are only six possible numbers, there are seven possible locations for each pip. */
Serial.println(pipsLocation[diescore-1]);
return pipsLocation[diescore];
}
void showTheRoll(int pipsOn, int dieNumber) /* sends info to the two shift registers, one for each die, which control the LEDs */
{
if (dieNumber == 0)
{
digitalWrite (die0Latch, LOW);
shiftOut (die0Data, die0Clock, MSBFIRST, pipsOn);
digitalWrite (die0Latch, HIGH);
rolled[0]=false;
}
else
{
digitalWrite (die1Latch, LOW);
shiftOut (die1Data, die1Clock, MSBFIRST, pipsOn);
digitalWrite (die1Latch, HIGH);
rolled[1]=false;
}
}
Rob Rothman
Dice Roller Schematic.pdf (58.8 KB)