#include <LiquidCrystal_I2C.h>
const int dataPin = 4;
const int clockPin = 3;
const int latchPin = 2;
const uint8_t switchAnodes[6] = {8, 9, 10, 11, 12, 13}; // 6 rows
const uint8_t switchCathodes[2] = {A2, A3}; // 2 columns
#define cevir 7
#define buzzer 1
LiquidCrystal_I2C lcd(0x27, 16, 2);
int gamerpuan = 20;
int gameroyunhakki = 3;
int switchIndex = 0;
int currentRow = -1;
int currentCol = -1;
int score = 0;
bool ledSelected[10] = {false};
int selectedCount = 0;
// Define note frequencies (in Hz)
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
// Define melody arrays for game over, you win, and start game
int gameOverMelody[] = {NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4};
int gameOverDurations[] = {8, 8, 8, 8, 8, 8, 8, 8}; // 8th notes
int youWinMelody[] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5};
int youWinDurations[] = {8, 8, 8, 8, 8, 8, 8, 8}; // 8th notes
int startGameMelody[] = {NOTE_C4, NOTE_E4, NOTE_G4, NOTE_C5};
int startGameDurations[] = {8, 8, 8, 8}; // 8th notes
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(buzzer, OUTPUT); // Ensure buzzer pin is set as output
karsilama();
}
void loop() {
if (digitalRead(cevir) == LOW) { // If button is pressed
startGame();
selectRandomLED();
delay(100); // Debounce delay
}
checkSwitches(); // Check switch states and update score
// Check if player wins
if (score >= 20) {
youWin();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Kazandin!");
lcd.setCursor(0, 1);
lcd.print("puanin: ");
lcd.print(score);
delay(50); // Display winning message
resetGame();
}
// Check if player loses
if (gameroyunhakki <= 0) {
gameOver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Kaybettin!");
lcd.setCursor(0, 1);
lcd.print("puanin: ");
lcd.print(score);
delay(50); // Display losing message
resetGame();
}
}
void karsilama() {
for (int i = 0; i < 6; ++i) {
pinMode(switchAnodes[i], INPUT_PULLUP); // Enable internal pull-up resistors
}
for (int i = 0; i < 2; ++i) {
pinMode(switchCathodes[i], OUTPUT);
}
randomSeed(millis());
//randomSeed(analogRead(A6));
resetLEDSelection();
selectRandomLED();
pinMode(cevir, INPUT_PULLUP); // Enable internal pull-up resistor
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bir kopuklu");
lcd.setCursor(0, 1);
lcd.print("Kahve icermisin");
delay(30);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Baslat");
}
void selectRandomLED() {
if (selectedCount >= 10) {
resetLEDSelection();
}
int newLEDIndex;
do {
newLEDIndex = random(0, 10);
} while (ledSelected[newLEDIndex]);
// LED seçildikten SONRA currentRow ve currentCol güncellenmeli
currentRow = newLEDIndex % 6; // 5 satır olduğunu varsayarak
currentCol = newLEDIndex / 6;
ledSelected[newLEDIndex] = true;
selectedCount++;
// veriGonder(1 << newLEDIndex); // Shift register'a LED'i yakacak veriyi gönder
veriGonder(~(1 << newLEDIndex));
int activeLED = newLEDIndex;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("puan: ");
lcd.print(score);
lcd.setCursor(0, 1);
lcd.print("Aktif LED: ");
lcd.print(activeLED +1);
lcd.setCursor(15, 0);
lcd.print(gameroyunhakki);
delay(100);
}
void veriGonder(uint16_t veri) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, highByte(veri));
shiftOut(dataPin, clockPin, MSBFIRST, lowByte(veri));
digitalWrite(latchPin, HIGH);
}
void resetLEDSelection() {
for (int i = 0; i < 10; ++i) {
ledSelected[i] = false;
}
selectedCount = 0;
}
void checkSwitches() {
for (int col = 0; col < 2; ++col) {
digitalWrite(switchCathodes[col], LOW);
for (int row = 0; row < 6; ++row) {
if (digitalRead(switchAnodes[row]) == LOW) {
switchIndex = row + col * 6;
int expectedLED = currentRow + currentCol * 6;
Serial.print("Switch Index: ");
Serial.println(switchIndex);
Serial.print("Expected LED: ");
Serial.println(expectedLED);
if (switchIndex == expectedLED) {
score += switchIndex +1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Puan: ");
lcd.print(score);
delay(100);
selectRandomLED();
Serial.println(switchIndex);
Serial.println(expectedLED);
}
else {
//score -= switchIndex;
gameroyunhakki--;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Puan: ");
lcd.print(score);
lcd.setCursor(0, 1);
lcd.print("Hak: ");
lcd.print(gameroyunhakki);
delay(100);
selectRandomLED();
Serial.println(switchIndex);
Serial.println(expectedLED);
}
delay(50); // debounce için
}
}
digitalWrite(switchCathodes[col], HIGH);
}
}
void gameOver() {
for (int i = 0; i < 8; i++) {
int duration = 1000 / gameOverDurations[i];
tone(buzzer, gameOverMelody[i], duration);
delay(duration * 1.3);
}
}
void youWin() {
for (int i = 0; i < 8; i++) {
int duration = 1000 / youWinDurations[i];
tone(buzzer, youWinMelody[i], duration);
delay(duration * 1.3);
}
}
void startGame() {
for (int i = 0; i < 4; i++) {
int duration = 1000 / startGameDurations[i];
tone(buzzer, startGameMelody[i], duration);
delay(duration * 1.3);
}
}
void resetGame() {
score = 0;
gamerpuan = 0;
gameroyunhakki = 3;
karsilama();
}