//Runs but doesn't work. AGAIN
// define the input and output pins
const int buttonPin = 12;
const int digit1Pin = 0;
const int digit2Pin = 1;
const int digit3Pin = 2;
const int segmentAPin = 3;
const int segmentBPin = 4;
const int segmentCPin = 5;
const int segmentDPin = 6;
const int segmentEPin = 7;
const int segmentFPin = 8;
const int segmentGPin = 9;
// define arrays for the 7-segment display numbers
const byte numbers[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B00111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
// variables for the 3 digit numbers
int digit1 = 0;
int digit2 = 0;
int digit3 = 0;
// variable for the state of the button
bool buttonState = LOW;
void displayNumber(int number, int digitPin) {// Kept saying I needed to declare display number so I did
digitalWrite(digitPin, HIGH);
for (int i = 0; i < 10; i++) {
if (bitRead(numbers[number], i) == 1) {
digitalWrite(3+i, HIGH);
} else {
digitalWrite(3+i, LOW);
}
}
digitalWrite(digitPin, LOW);
}
void setup() {
// initialize the input and output pins
pinMode(buttonPin, INPUT_PULLUP);
pinMode(digit1Pin, OUTPUT);
pinMode(digit2Pin, OUTPUT);
pinMode(digit3Pin, OUTPUT);
pinMode(segmentAPin, OUTPUT);
pinMode(segmentBPin, OUTPUT);
pinMode(segmentCPin, OUTPUT);
pinMode(segmentDPin, OUTPUT);
pinMode(segmentEPin, OUTPUT);
pinMode(segmentFPin, OUTPUT);
pinMode(segmentGPin, OUTPUT);
// seed the random number generator
randomSeed(analogRead(0));
}
void loop() {
// check if the button is pressed
if (digitalRead(buttonPin) == LOW && buttonState == LOW) {
// button is pressed, start the sequence
buttonState = HIGH;
// generate random numbers for the 3 digits
digit1 = random(1, 10);
digit2 = random(1, 10);
digit3 = random(1, 10);
// scroll the numbers on the display
for (int i = 0; i < 10; i++) {
displayNumber(random(1, 10), digit1Pin);
displayNumber(random(1, 10), digit2Pin);
displayNumber(random(1, 10), digit3Pin);
delay(100);
}
// stop the numbers on the display
displayNumber(digit1, digit1Pin);
delay(500);
displayNumber(digit2, digit2Pin);
delay(500);
displayNumber(digit3, digit3Pin);
delay(500);
// check if there is a winner
if (digit1 == digit2 && digit2 == digit3) {
// all digits match, display winner animation
for (int i = 0; i < 5; i++) {
displayNumber(0, digit1Pin);
displayNumber(0, digit2Pin);
displayNumber(0, digit3Pin);
delay(200);
displayNumber(digit1, digit1Pin);
displayNumber(digit2, digit2Pin);
displayNumber(digit3, digit3Pin);
delay(200);
}
// output winner message to the serial monitor
Serial.println("WINNER!");
}
}
// check if the button is released
if (digitalRead(buttonPin) == HIGH && buttonState == HIGH) {
// button is released, reset the game
buttonState = LOW;
// reset the 3 digit numbers
digit1 = 0;
digit2 = 0;
digit3 = 0;
// turn off all segments of the display
for (int i = 3; i <= 9; i++) {
digitalWrite(i, LOW);
}
// output reset message to the serial monitor
Serial.println("Game reset");
}
}