const int ledPins[] = {4, 5, 6, 7, 8, 9, 10, 11, 12};
const int button1Pin = 3;
const int button2Pin = 2;
const int buzzerPin = 13;
int currentLed = 0;
bool directionForward = true;
int player1Score = 0, player2Score = 0;
unsigned long lastUpdateTime = 0;
int updateInterval = 300;
const int maxSpeed = 800;
const int minSpeed = 100;
bool inSettingsMode = false;
unsigned long button1PressTime = 0;
unsigned long button2PressTime = 0;
bool button1Pressed = false;
bool button2Pressed = false;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 9; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
if (inSettingsMode) {
settingsMode();
} else {
gameMode();
checkLongPress();
}
}
void gameMode() {
if (millis() - lastUpdateTime > updateInterval) {
digitalWrite(ledPins[currentLed], LOW);
if (directionForward) currentLed++;
else currentLed--;
if (currentLed == 8) directionForward = false;
if (currentLed == 0) directionForward = true;
digitalWrite(ledPins[currentLed], HIGH);
lastUpdateTime = millis();
}
if (digitalRead(button1Pin) == LOW) checkHit(1);
if (digitalRead(button2Pin) == LOW) checkHit(2);
if (player1Score >= 5 || player2Score >= 5) {
showMistakeAnimation(player1Score >= 5 ? 1 : 2);
}
if (Serial.available() > 0) {
char key = Serial.read();
if (key == 'K' || key == 'k') {
checkHit(1);
} else if (key == 'L' || key == 'l') {
checkHit(2);
}
}
}
void checkLongPress() {
if (digitalRead(button1Pin) == LOW) {
if (button1PressTime == 0) button1PressTime = millis();
if (millis() - button1PressTime >= 2000) {
button1PressTime = millis();
enterSettingsMode();
}
} else {
button1PressTime = 0;
}
if (digitalRead(button2Pin) == LOW) {
if (button2PressTime == 0) button2PressTime = millis();
if (millis() - button2PressTime >= 2000) {
button1PressTime = millis();
enterSettingsMode();
}
} else {
button2PressTime = 0;
}
}
void enterSettingsMode() {
inSettingsMode = true;
for (int i = 0; i < 9; i++) digitalWrite(ledPins[i], LOW);
updateSpeedDisplay();
}
void settingsMode() {
if (digitalRead(button1Pin) == LOW) {
if (!button1Pressed) {
button1Pressed = true;
if (updateInterval <= maxSpeed) updateInterval += 100;
updateSpeedDisplay();
delay(200);
}
}
if (digitalRead(button1Pin) == HIGH) {
button1Pressed = false;
}
if (digitalRead(button2Pin) == LOW) {
if (!button2Pressed) {
button2Pressed = true;
if (updateInterval > minSpeed) updateInterval -= 100;
updateSpeedDisplay();
delay(200);
}
}
if (digitalRead(button2Pin) == HIGH) {
button2Pressed = false;
}
if (digitalRead(button1Pin) == LOW) {
if (button1PressTime == 0) button1PressTime = millis();
if (millis() - button1PressTime >= 2000) {
delay(500);
inSettingsMode = false;
resetGame();
}
} else {
button1PressTime = 0;
}
if (digitalRead(button2Pin) == LOW) {
if (button2PressTime == 0) button2PressTime = millis();
if (millis() - button2PressTime >= 4000) {
delay(500);
inSettingsMode = false;
resetGame();
}
} else {
button2PressTime = 0;
}
}
void updateSpeedDisplay() {
int numLeds = map(updateInterval, maxSpeed, minSpeed, 1, 9);
for (int i = 0; i < 9; i++) {
if (i < numLeds) {
digitalWrite(ledPins[i], HIGH);
} else {
digitalWrite(ledPins[i], LOW);
}
}
}
void checkHit(int player) {
if ((player == 1 && currentLed == 8) || (player == 2 && currentLed == 0)) {
scorePoint(player);
} else {
allLedsBlink(3);
}
}
void scorePoint(int player) {
if (player == 1) {
player1Score++;
tone(buzzerPin, 1000, 200);
for (int a = 0; a < 3; a++){
for (int i = 0; i < (player == 1 ? player1Score : player2Score); i++) {
digitalWrite(ledPins[i], HIGH);
}
delay(200);
for (int x = 0; x < (player == 1 ? player1Score : player2Score); x++) {
digitalWrite(ledPins[x], LOW);
}
delay(200);
}
}
else {
player2Score++;
tone(buzzerPin, 1000, 200);
for (int a = 0; a < 3; a++){
for (int i = 0; i < (player == 1 ? player1Score : player2Score); i++) {
digitalWrite(ledPins[8-i], HIGH);
}
delay(200);
for (int x = 0; x < (player == 1 ? player1Score : player2Score); x++) {
digitalWrite(ledPins[8-x], LOW);
}
delay(200);
}
}
}
void allLedsBlink(int times) {
for (int i = 0; i < times; i++) {
for (int j = 0; j < 9; j++) digitalWrite(ledPins[j], HIGH);
delay(200);
for (int j = 0; j < 9; j++) digitalWrite(ledPins[j], LOW);
delay(200);
}
}
void showMistakeAnimation(int winner) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) digitalWrite(ledPins[j], winner == 1 ? HIGH : LOW);
tone(buzzerPin, 1200, 500);
delay(500);
for (int j = 0; j < 9; j++) digitalWrite(ledPins[j], LOW);
delay(500);
}
}
void resetGame() {
player1Score = 0;
player2Score = 0;
currentLed = 0;
directionForward = true;
for (int i = 0; i < 9; i++) {
digitalWrite(ledPins[i], LOW);
}
}