#include <Keypad.h>
int score = 0;
int buzzer = 13;
const int ledPins[] = {6, 7,8, 9, 10, 11, 12, A4};
int ledIndexes[] = {0, 1, 2, 3, 5, 6, 7};
int totalLEDs = 8;
bool gameStarted = false;
int lightDuration = 1000; // Initial light duration in milliseconds
int currentLight = 0; // The current light that is on
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT); // Set LED pins as output
}
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == 'A') {
gameStarted = true;
score = 0;
lightDuration = 1000;
currentLight = -1;
} else if (key == 'D') {
gameStarted = false;
blinkAndBuzz(score);
} else if (key == '*') {
turnOnAllLights();
} else if (key == '0') {
turnOffAllLights();
} else if (gameStarted) {
checkScore(key);
}
}
if (gameStarted) {
updateLights();
}
}
void blinkAndBuzz(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(ledPins[0], HIGH);
tone(buzzer, 262);
delay(50);
digitalWrite(ledPins[0], LOW);
noTone(buzzer);
delay(50);
}
}
void turnOnAllLights() {
for (int i = 0; i < sizeof(ledPins)/sizeof(int); i++) {
digitalWrite(ledPins[i], HIGH);
}
}
void turnOffAllLights() {
for (int i = 0; i < sizeof(ledPins)/sizeof(int); i++) {
digitalWrite(ledPins[i], LOW);
}
}
void checkScore(char key) {
int index = key - '1';
if (digitalRead(ledPins[index]) == HIGH) {
score++;
digitalWrite(ledPins[index], LOW);
tone(buzzer, 262);
delay(25);
noTone(buzzer);
// Move to the next light immediately after a successful hit
currentLight++;
if (currentLight >= sizeof(ledPins)/sizeof(int)) {
currentLight = -1;
lightDuration *= .9;
}
updateLights();
}
}
void updateLights() {
static unsigned long lastUpdate = 0; // The last time the lights were updated
unsigned long currentMillis = millis(); // The current time
// Check if it's time to update the lights
if (currentMillis - lastUpdate >= lightDuration) {
lastUpdate = currentMillis; // Update the last update time
// Turn off the current light
digitalWrite(ledPins[currentLight], LOW);
// Move to the next light
currentLight++;
if (currentLight >= sizeof(ledPins)/sizeof(int)) {
currentLight = 0; // Wrap around to the first light
// Decrease the light duration for the next round
lightDuration = lightDuration * 90 / 100;
}
// Turn on the next light
digitalWrite(ledPins[currentLight], HIGH);
// Play the sound effect for the current light
playSound(currentLight);
}
}
void playSound(int light) {
int frequency1, frequency2;
int duration1 = 25, duration2 = 75;
switch (light) {
case 0:
frequency1 = 1109;
frequency2 = 1175;
break;
case 1:
frequency1 = 1319;
frequency2 = 1397;
break;
case 2:
frequency1 = 1109;
frequency2 = 1175;
break;
case 3:
frequency1 = 1319;
frequency2 = 1397;
break;
case 4:
frequency1 = 1865;
frequency2 = 1760;
break;
case 5:
frequency1 = 1175;
frequency2 = 1397;
break;
case 6:
frequency1 = 1760;
frequency2 = 1661;
break;
case 7:
frequency1 = 1661;
duration1 = duration2 = 100; // Only one tone for this light
break;
default:
return; // Invalid light number
}
tone(buzzer, frequency1, duration1);
if (light !=7){
delay(duration1);
noTone(buzzer);
tone(buzzer, frequency2, duration2);
}
}