Skip to content

Instantly share code, notes, and snippets.

@kpeatt
Created January 14, 2014 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpeatt/8413536 to your computer and use it in GitHub Desktop.
Save kpeatt/8413536 to your computer and use it in GitHub Desktop.
Digital Dice Roller for Arduino
const int ledPins[] = {13,12,11,10,9,8};
const int button1Pin = 3;
const int buzPin = 7;
int time;
int index;
int loopCount = 0;
int delayTime;
unsigned long startTime = millis();
unsigned long endTime = startTime;
void setup()
{
for(index = 0; index <= 5; index++)
{
pinMode(ledPins[index],OUTPUT);
}
pinMode(button1Pin, INPUT);
Serial.begin(9600);
}
void loop()
{
int button1State;
button1State = digitalRead(button1Pin);
if (button1State == LOW)
{
spinIt();
}
}
void diceRoll()
{
int result = random(6);
for (index = 0; index <= result; index++) {
digitalWrite(ledPins[index], HIGH);
tone(buzPin, 100, 20);
}
}
void diceCount(int number, int on)
{
for (index = 0; index < number; index ++)
{
digitalWrite(ledPins[index], on);
}
}
void spinIt()
{
startTime = millis();
endTime = startTime;
time = 3000;
loopCount = 0;
for (index = 0; index <= 5; index++) {
digitalWrite(ledPins[index], LOW);
}
if (loopCount == 0) // Only run the random blink once
{
while ((endTime - startTime) <= time) // Do it for a set amount of time and then stop
{
randomBlink();
endTime = millis();
if (loopCount == 0) {
loopCount += 1;
}
}
}
diceRoll();
}
void randomBlink()
{
delayTime = 50;
for (index = 0; index <= 5; index++)
{
index = random(6);
diceCount(index, 1);
tone(buzPin, 300, 20);
delay(delayTime);
diceCount(index, 0);
delayTime += 20;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment