/*
Arduino Code to drive LED Multigame Board (Roulette, Dice, Yes/No ...)
(c) tgtech 2003
*/
int SER_Pin = 7; // Arduino pin to provide SERIAL data to the 75HC595 Shift Register ICs
int RCLK_Pin = 12; // Pin to drive the RESET CLOCK IC pin
int SRCLK_Pin = 11; // Pin to drive the SERIAL CLOCK IC pin
int RESET_Pin = 8; // Pin to connect the reset button
int POT_VALUE = A0; // Pin to connect wiper of Potentiometer for reading
int MaxDelay; // Maximum delay between LED on/off times for realism
int DelayValue = 5; // Start value for delay
int StopAt; // Holds the random number to stop spin
int FullSpins; // Number of full spins of 'wheel'
int LEDNumber; // Holds the LED number to turn on/off
constexpr int ShiftRegisters = 2; // Number of 75HC595 shift register ICs
constexpr int PinCount = ShiftRegisters * 8; // 8 pins per IC, to connect LEDs
boolean Registers[PinCount]; // Array to hold value to write to registers
void setup() {
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);
pinMode(RESET_Pin, INPUT_PULLUP);
// reset the registers
ClearRegisters();
WriteRegisters();
// Serial.begin is only needed for printing to debug serial monitor
Serial.begin(115200);
randomSeed(analogRead(7)); // Initialize the random seed to 'random' noise on an unused analog pin
MaxDelay = random(200, 250); // Maximum delay for LED on/off period
}
// Set then enable display registers; This is SLOW, so call after values are set
void WriteRegisters() {
digitalWrite(RCLK_Pin, LOW);
for(int i = PinCount - 1; i >= 0; i--) {
digitalWrite(SRCLK_Pin, LOW);
int val = Registers[i];
digitalWrite(SER_Pin, val);
digitalWrite(SRCLK_Pin, HIGH);
}
digitalWrite(RCLK_Pin, HIGH);
}
// Clear all register pins by setting them LOW
void ClearRegisters() {
for(int i = PinCount - 1; i >= 0; i--){
Registers[i] = LOW;
}
}
// Set an individual pin HIGH or LOW (on or off)
void setRegisterPin(int index, int value) {
Registers[index] = value;
}
void loop() {
// Check if the RESET button was pressed
if(digitalRead(RESET_Pin) == LOW) {
ClearRegisters();
WriteRegisters();
FullSpins = random(2,5); // Pick a random number of full spins before stopping at the number (between firstnum and secondnum-1)
StopAt = random(PinCount); // Pick a random number to end the spin
int CountTo = FullSpins * PinCount + StopAt; // we'll count up to the number chosen
Serial.print("Full Spins: "); Serial.print(FullSpins); Serial.print(" StopAt: "); Serial.println(StopAt); Serial.print(" CountTo: "); Serial.println(CountTo); // only for simulation to print to debug serial monitor
for (int x=0; x <= CountTo; x++) {
LEDNumber = x - ( PinCount * ( x / PinCount)); // do some modulo math
//Serial.print(x);Serial.print(" - ");;Serial.print(LEDNumber);Serial.print("-");
if (DelayValue <= MaxDelay) {
setRegisterPin(LEDNumber, HIGH);
WriteRegisters();
delay(DelayValue);
if (x < CountTo) { // Leave the LED on when the CountTo number is reached
setRegisterPin(LEDNumber, LOW);
WriteRegisters();
DelayValue++;
tone(6, 1000, 5);
}
}
delay(70);
}
DelayValue=5; // Reset initial delay value at end of spin
}
}