#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin is not used
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int player1XPin = A2;//horz
const int player1YPin = A3;//vert
const int player2XPin = A1;
const int player2YPin = A0;
const int ledUp = 5;
const int ledDown = 3;
const int ledLeft = 4;
const int ledRight = 6;
const int buzzerPin = 8; // Buzzer pin
const int resetButtonPin = 7; // Connect a button to this pin for reset
int player1Score = 0;
int player2Score = 0;
void setup() {
Serial.begin(9600);
pinMode(ledUp, OUTPUT);
pinMode(ledDown, OUTPUT);
pinMode(ledLeft, OUTPUT);
pinMode(ledRight, OUTPUT);
pinMode(buzzerPin, OUTPUT); // Set up the buzzer pin
pinMode(resetButtonPin, INPUT_PULLUP); // Set up the reset button
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 5);
display.println("Reflex Game");
display.display();
delay(2000);
}
void loop() {
int speedLevel = 800; // Initial speed level, in milliseconds
int randomDirection = random(4);
switch (randomDirection) {
case 0:
digitalWrite(ledUp, HIGH);
break;
case 1:
digitalWrite(ledDown, HIGH);
break;
case 2:
digitalWrite(ledLeft, HIGH);
break;
case 3:
digitalWrite(ledRight, HIGH);
break;
}
delay(speedLevel);
digitalWrite(ledUp, LOW);
digitalWrite(ledDown, LOW);
digitalWrite(ledLeft, LOW);
digitalWrite(ledRight, LOW);
int player1Direction = getPlayerDirection(player1XPin, player1YPin);
int player2Direction = getPlayerDirection(player2XPin, player2YPin);
if (player1Score % 1 == 0 && player1Score > 0 && player2Score % 1 == 0 && player2Score > 0) {
// Increase speed level every 1 points
speedLevel -= 70;
}
updateScores(randomDirection, player1Direction, player2Direction);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("P1: ");
display.println(player1Score);
display.setCursor(64, 0);
display.print("P2: ");
display.println(player2Score);
if (player1Score >= 10 || player2Score >= 10) {
display.display(); // Display the scores
// Play buzzer sound
playBuzzer();
// Set winning message and add emojis
const char *winnerMessage = (player1Score >= 10) ? "Player 1 Wins! " : "Player 2 Wins! ";
displayWinnerAnimation(winnerMessage);
// Wait for the reset button to be pressed
while (digitalRead(resetButtonPin) == HIGH) {
// Wait
}
// Reset game state
player1Score = 0;
player2Score = 0;
speedLevel = 1000;
// Clear the display
display.clearDisplay();
display.display();
delay(2000); // Pause for 2 seconds
} else {
display.display(); // Display the scores
}
}
int getPlayerDirection(int xPin, int yPin) {
int xValue = analogRead(xPin);
int yValue = analogRead(yPin);
if (yValue > 700) {
return 0; // Up
} else if (yValue < 300) {
return 1; // Down
} else if (xValue > 700) {
return 2; // Left
} else if (xValue < 300) {
return 3; // Right
}
return -1; // No direction
}
void updateScores(int randomDirection, int player1Direction, int player2Direction) {
if (player1Direction == randomDirection) {
player1Score++;
}
if (player2Direction == randomDirection) {
player2Score++;
}
}
void displayWinnerAnimation(const char *winnerMessage) {
int textWidth = display.getCursorX(); // Get the width of the text
int displayWidth = SCREEN_WIDTH;
// Display scrolling animation
for (int i = displayWidth; i >= -textWidth; i--) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(i, 20);
display.print(winnerMessage);
display.display();
delay(50);
}
}
void playBuzzer() {
tone(buzzerPin, 2000, 500); // Play a 2kHz tone for 500 milliseconds
delay(1000); // Pause for 500 milliseconds
noTone(buzzerPin); // Stop the buzzer sound
}