const int buttonD4 = 2;
const int buttonD6 = 3;
const int buttonD8 = 4;
const int buttonD10 = 5;
const int buttonD20 = 6;
const int buttonD100 = 7;
const int buttonD999 = 8;
const int data = 9;
const int latch = 10;
const int clock = 11;
const byte defaultDigit = B00000010;
const byte digitDance[] = {
// ABCDEFGDp
// ||||||||
B10010000,
B01001000,
B00000010,
B00100100
};
const byte digits[] {
// ABCDEFGDp
// ||||||||
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
int randNum;
void setup() {
Serial.begin(9600);
pinMode(buttonD4, INPUT);
pinMode(buttonD6, INPUT);
pinMode(buttonD8, INPUT);
pinMode(buttonD10, INPUT);
pinMode(buttonD20, INPUT);
pinMode(buttonD100, INPUT);
pinMode(buttonD999, INPUT);
pinMode(data, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(clock, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(buttonD4) == HIGH) {
showRandom(4);
} else if (digitalRead(buttonD6) == HIGH) {
showRandom(6);
} else if (digitalRead(buttonD8) == HIGH) {
showRandom(8);
} else if (digitalRead(buttonD10) == HIGH) {
showRandom(10);
} else if (digitalRead(buttonD20) == HIGH) {
showRandom(20);
} else if (digitalRead(buttonD100) == HIGH) {
showRandom(100);
} else if (digitalRead(buttonD999) == HIGH) {
showRandom(999);
}
delay(50);
}
void showRandom(int max) {
Serial.print("D");
Serial.print(max);
Serial.println(" launched !");
randNum = getRandom(max);
dance();
displayNumber();
}
void dance() {
for (int i = 0; i < 8; i++) {
byte digit = digitDance[i % 4];
display(digit, digit, digit);
delay(100);
}
}
int getRandom(int max) {
int seed = analogRead(0) + analogRead(1);
Serial.print("Seed : ");
Serial.println(seed);
randomSeed(seed);
return random(max) + 1;
}
void displayNumber() {
Serial.print("Number : ");
Serial.println(randNum);
byte hundreds = randNum % 1000 / 100;
byte tens = randNum % 100 / 10;
byte units = randNum % 10;
display(digits[hundreds], digits[tens], digits[units]);
}
void display(byte tens, byte units, byte hundreds) {
digitalWrite(latch, LOW);
shiftOut(data, clock, LSBFIRST, units);
shiftOut(data, clock, LSBFIRST, tens);
shiftOut(data, clock, LSBFIRST, hundreds);
digitalWrite(latch, HIGH);
}