Last active
February 19, 2025 09:23
-
-
Save lakshith-403/70b01baf3c39f657993db10e6a33006b to your computer and use it in GitHub Desktop.
hehe_ass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define LED_PIN 22 | |
#define ULTRASONIC1_TRIG_PIN 5 | |
#define ULTRASONIC1_ECHO_PIN 18 | |
#define ULTRASONIC2_TRIG_PIN 26 | |
#define ULTRASONIC2_ECHO_PIN 27 | |
#define WINNER_1_LED 32 | |
#define WINNER_2_LED 35 | |
struct GameState { | |
const int tune1[8] = {0, 1, 2, 1, 0, 2, 1, 0}; | |
const int tune2[8] = {2, 1, 0, 0, 1, 2, 2, 1}; | |
int currentTune = 0; | |
int currentNote = 0; | |
unsigned long lastNoteTime = 0; | |
int player1Score = 0; | |
int player2Score = 0; | |
int sensor1Readings[10]; | |
int sensor2Readings[10]; | |
int readingCount = 0; | |
int getLevelIntensity(int level) { | |
switch(level) { | |
case 0: return 255; // Dim | |
case 1: return 100; // Medium | |
case 2: return 0; // Bright | |
default: return 0; | |
} | |
} | |
int getBracket(float distance) { | |
if (distance < 10) return 0; | |
if (distance < 20) return 1; | |
return 2; | |
} | |
int getMode(int readings[], int count) { | |
int bracketCounts[3] = {0, 0, 0}; | |
for(int i = 0; i < count; i++) { | |
bracketCounts[readings[i]]++; | |
} | |
int maxCount = bracketCounts[0]; | |
int mode = 0; | |
for(int i = 1; i < 3; i++) { | |
if (bracketCounts[i] > maxCount) { | |
maxCount = bracketCounts[i]; | |
mode = i; | |
} | |
} | |
return mode; | |
} | |
float readUltrasonicSensor(int trigPin, int echoPin) { | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
long duration = pulseIn(echoPin, HIGH); | |
return duration * 0.034 / 2; | |
} | |
void playCurrentNote() { | |
const int* currentTuneArray = (currentTune == 0) ? tune1 : tune2; | |
int level = currentTuneArray[currentNote]; | |
Serial.print("Playing tune "); | |
Serial.print(currentTune + 1); | |
Serial.print(", note "); | |
Serial.print(currentNote + 1); | |
Serial.print(" of 8 (Level "); | |
Serial.print(level); | |
Serial.print(": LED Intensity="); | |
Serial.print(getLevelIntensity(level)); | |
Serial.println(")"); | |
analogWrite(LED_PIN, getLevelIntensity(level)); | |
readingCount = 0; | |
lastNoteTime = millis(); | |
} | |
void takeSensorReadings() { | |
if (readingCount >= 10) return; | |
float distance1 = readUltrasonicSensor(ULTRASONIC1_TRIG_PIN, ULTRASONIC1_ECHO_PIN); | |
float distance2 = readUltrasonicSensor(ULTRASONIC2_TRIG_PIN, ULTRASONIC2_ECHO_PIN); | |
sensor1Readings[readingCount] = getBracket(distance1); | |
sensor2Readings[readingCount] = getBracket(distance2); | |
readingCount++; | |
} | |
void updateScores() { | |
const int* currentTuneArray = (currentTune == 0) ? tune1 : tune2; | |
int expectedLevel = currentTuneArray[currentNote]; | |
int player1Level = getMode(sensor1Readings, readingCount); | |
int player2Level = getMode(sensor2Readings, readingCount); | |
if (player1Level == expectedLevel) player1Score++; | |
if (player2Level == expectedLevel) player2Score++; | |
Serial.print("Expected level: "); Serial.println(expectedLevel); | |
Serial.print("Player 1 level: "); Serial.println(player1Level); | |
Serial.print("Player 2 level: "); Serial.println(player2Level); | |
} | |
void stopNote() { | |
Serial.println("Stopping note"); | |
analogWrite(LED_PIN, 0); | |
} | |
// returns true if the game is over | |
int update() { | |
unsigned long currentTime = millis(); | |
// Take readings for 1 second after note starts | |
if (currentTime - lastNoteTime < 1000) { | |
takeSensorReadings(); | |
return 0; | |
} | |
// After 1 second, update scores and move to next note | |
updateScores(); | |
currentNote = (currentNote + 1); | |
if (currentNote == 8) { | |
Serial.println("Tune complete!"); | |
Serial.print("Final Scores - Player 1: "); | |
Serial.print(player1Score); | |
Serial.print(", Player 2: "); | |
Serial.println(player2Score); | |
digitalWrite(WINNER_1_LED, player1Score>player2Score); | |
digitalWrite(WINNER_2_LED, player1Score<player2Score); | |
return 1; | |
} | |
playCurrentNote(); | |
return 0; | |
} | |
}; | |
GameState gameState; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("\n=== Game Starting ==="); | |
pinMode(LED_PIN, OUTPUT); | |
pinMode(ULTRASONIC1_TRIG_PIN, OUTPUT); | |
pinMode(ULTRASONIC1_ECHO_PIN, INPUT); | |
pinMode(ULTRASONIC2_TRIG_PIN, OUTPUT); | |
pinMode(ULTRASONIC2_ECHO_PIN, INPUT); | |
pinMode(WINNER_1_LED, OUTPUT); | |
pinMode(WINNER_2_LED, OUTPUT); | |
gameState.currentNote = 0; | |
gameState.player1Score = 0; | |
gameState.player2Score = 0; | |
Serial.println("Pins initialized"); | |
gameState.playCurrentNote(); | |
} | |
void loop() { | |
int result = gameState.update(); | |
if (result) { | |
Serial.println("Game over!"); | |
delay(5000); | |
digitalWrite(WINNER_1_LED, 0); | |
digitalWrite(WINNER_2_LED, 0); | |
gameState.currentNote = 0; | |
gameState.player1Score = 0; | |
gameState.player2Score = 0; | |
gameState.currentTune = (gameState.currentTune + 1) % 2; | |
} | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment