#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// Ekran pin tanımlamaları
#define TFT_CS 22
#define TFT_RST 23
#define TFT_DC 24
#define TFT_MOSI 25
#define TFT_SCK 26
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK);
// LED pinleri (16 LED)
const int ledPins[] = {44, 45, 46, 47, 48, 49, 50, 51, 52,53, 2, 3, 4, 5, 6, 7};
// Buton pinleri (16 buton)
const int buttonPins[] = {28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43};
// Özel pinler
const int CORRECT_PIN = 9;
const int WRONG_PIN = 10;
const int START_PIN = 11;
const int WIN_PIN = 12;
// Oyun değişkenleri
const int levels = 4;
const int levelData[][2] = {
{3, 5000}, // Seviye 1: 3 LED, 5 saniye
{4, 7000}, // Seviye 2: 4 LED, 7 saniye
{5, 10000}, // Seviye 3: 5 LED, 10 saniye
{6, 14000} // Seviye 4: 6 LED, 14 saniye
};
int currentLevel = 0;
int sequence[6]; // Maksimum 6 LED dizisi
int userSequence[6];
int sequenceStep = 0;
int userStep = 0;
enum GameState {MENU, SHOW_SEQUENCE, USER_INPUT, LEVEL_COMPLETE, GAME_OVER};
GameState gameState = MENU;
unsigned long startTime;
bool buttonsPressed[16] = {false};
bool gameActive = false;
// Kare koordinatları (6 kare, 2 satır 3 sütun)
const int squareCount = 6;
const int squareSize = 60;
const int squareSpacing = 10;
int squareX[squareCount];
int squareY[squareCount];
void setup() {
Serial.begin(9600);
// Pin modlarını ayarla
for (int i = 0; i < 16; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(CORRECT_PIN, OUTPUT);
pinMode(WRONG_PIN, OUTPUT);
pinMode(START_PIN, OUTPUT);
pinMode(WIN_PIN, OUTPUT);
// Özel pinleri başlangıçta LOW yap
digitalWrite(CORRECT_PIN, LOW);
digitalWrite(WRONG_PIN, LOW);
digitalWrite(START_PIN, LOW);
digitalWrite(WIN_PIN, LOW);
// Ekranı başlat
tft.begin();
tft.setRotation(1); // Yatay mod
tft.fillScreen(ILI9341_BLACK);
// Kare pozisyonlarını hesapla
calculateSquarePositions();
// Menüyü göster
showMenu();
}
void loop() {
switch (gameState) {
case MENU:
handleMenu();
break;
case SHOW_SEQUENCE:
showSequence();
break;
case USER_INPUT:
handleUserInput();
break;
case LEVEL_COMPLETE:
levelComplete();
break;
case GAME_OVER:
gameOver();
break;
}
}
void calculateSquarePositions() {
int startX = (tft.width() - (3 * squareSize + 2 * squareSpacing)) / 2;
int startY = (tft.height() - (2 * squareSize + squareSpacing)) / 2;
for (int i = 0; i < 6; i++) {
int row = i / 3;
int col = i % 3;
squareX[i] = startX + col * (squareSize + squareSpacing);
squareY[i] = startY + row * (squareSize + squareSpacing);
}
}
void showMenu() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.setCursor(40, 100);
tft.println("HAFIZANA GUVENIYORSAN");
tft.setCursor(120, 130);
tft.println("GEL!");
tft.setTextSize(1);
tft.setCursor(80, 180);
tft.println("Baslamak icin Buton1'e bas");
// Başlangıç butonu için pin kontrolü
triggerPin(START_PIN);
}
void handleMenu() {
if (!digitalRead(buttonPins[0])) { // Buton1'e basıldı
delay(50); // Debounce
if (!digitalRead(buttonPins[0])) {
startGame();
}
}
}
void startGame() {
currentLevel = 0;
gameActive = true;
startLevel();
}
void startLevel() {
// Rastgele dizi oluştur
generateSequence(levelData[currentLevel][0]);
// Ekranı temizle
tft.fillScreen(ILI9341_BLACK);
// "Işıkların hareketini izle" mesajı
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(60, 20);
tft.println("ISIKLARI IZLE");
// Kareleri çiz
drawSquares(false);
gameState = SHOW_SEQUENCE;
sequenceStep = 0;
// LED dizisini göster
showSequenceStep();
}
void generateSequence(int length) {
for (int i = 0; i < length; i++) {
sequence[i] = random(0, 16); // 0-15 arası rastgele LED
}
}
void showSequenceStep() {
if (sequenceStep < levelData[currentLevel][0]) {
// LED'i yak
digitalWrite(ledPins[sequence[sequenceStep]], HIGH);
delay(1000); // 1 saniye yanık kal
// LED'i söndür
digitalWrite(ledPins[sequence[sequenceStep]], LOW);
delay(200); // Kısa ara
sequenceStep++;
if (sequenceStep < levelData[currentLevel][0]) {
// Bir sonraki adım için tekrar çağır
showSequenceStep();
} else {
// Dizi gösterimi bitti, kullanıcı girişine geç
delay(500);
startUserInput();
}
}
}
void showSequence() {
// Bu fonksiyon showSequenceStep() tarafından handle ediliyor
}
void startUserInput() {
userStep = 0;
startTime = millis();
// Ekranı temizle
tft.fillScreen(ILI9341_BLACK);
// Kareleri çiz (sadece kullanılacak kadarında soru işareti)
drawSquares(true);
gameState = USER_INPUT;
}
void drawSquares(bool showQuestionMarks) {
int squaresToShow = levelData[currentLevel][0];
for (int i = 0; i < 6; i++) {
// Kareyi çiz
tft.drawRect(squareX[i], squareY[i], squareSize, squareSize, ILI9341_WHITE);
if (i < squaresToShow && showQuestionMarks) {
// Soru işareti göster
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(3);
tft.setCursor(squareX[i] + squareSize/2 - 10, squareY[i] + squareSize/2 - 15);
tft.println("?");
} else if (i >= squaresToShow) {
// Boş kare
tft.fillRect(squareX[i] + 2, squareY[i] + 2, squareSize - 4, squareSize - 4, ILI9341_DARKGREY);
}
}
}
void handleUserInput() {
// Zaman kontrolü
if (millis() - startTime > levelData[currentLevel][1]) {
gameState = GAME_OVER;
triggerPin(WRONG_PIN);
return;
}
// Buton kontrolü
for (int i = 0; i < 16; i++) {
if (!digitalRead(buttonPins[i]) && !buttonsPressed[i]) {
buttonsPressed[i] = true;
// Karşılık gelen LED'i yak
digitalWrite(ledPins[i], HIGH);
if (i == sequence[userStep]) {
// Doğru buton
triggerPin(CORRECT_PIN);
// Karede tik işareti göster
updateSquare(userStep, true);
userStep++;
if (userStep >= levelData[currentLevel][0]) {
// Seviye tamamlandı
delay(500);
// Tüm LED'leri söndür
for (int j = 0; j < 16; j++) {
digitalWrite(ledPins[j], LOW);
}
triggerPin(WIN_PIN);
gameState = LEVEL_COMPLETE;
}
} else {
// Yanlış buton
triggerPin(WRONG_PIN);
delay(500);
// Tüm LED'leri söndür
for (int j = 0; j < 16; j++) {
digitalWrite(ledPins[j], LOW);
}
gameState = GAME_OVER;
}
delay(300); // Buton basma efekti için
digitalWrite(ledPins[i], LOW);
break;
}
if (digitalRead(buttonPins[i])) {
buttonsPressed[i] = false;
}
}
}
void updateSquare(int index, bool correct) {
int x = squareX[index];
int y = squareY[index];
// Kareyi temizle
tft.fillRect(x + 2, y + 2, squareSize - 4, squareSize - 4, ILI9341_BLACK);
tft.drawRect(x, y, squareSize, squareSize, ILI9341_WHITE);
if (correct) {
// Tik işareti çiz
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(4);
tft.setCursor(x + squareSize/2 - 15, y + squareSize/2 - 20);
tft.println("✓");
}
}
void levelComplete() {
// Tebrikler mesajı
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.setCursor(100, 100);
tft.println("TEBRIKLER!");
delay(2000);
// Sonraki seviyeye geç
currentLevel++;
if (currentLevel < levels) {
startLevel();
} else {
// Oyun tamamlandı
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(3);
tft.setCursor(80, 100);
tft.println("OYUNU KAZANDIN!");
delay(3000);
gameState = MENU;
showMenu();
}
}
void gameOver() {
// Tüm LED'leri söndür
for (int i = 0; i < 16; i++) {
digitalWrite(ledPins[i], LOW);
}
// Oyun bitti mesajı
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.setCursor(70, 100);
tft.println("LUTFEN TEKRAR");
tft.setCursor(80, 130);
tft.println("DENEYINIZ");
delay(3000);
gameState = MENU;
showMenu();
}
void triggerPin(int pin) {
// Önce 10ms LOW
digitalWrite(pin, LOW);
delay(10);
// Sonra 100ms HIGH
digitalWrite(pin, HIGH);
delay(100);
// Tekrar LOW
digitalWrite(pin, LOW);
}