// Dont Forget to Support My Channel
// Electro Pemula on Youtube and TikTok
// https://www.youtube.com/channel/UCa8eHXL2aL6lc1UN_bcge7A/
// https://www.tiktok.com/@elektropemula/
// Project Video : https://www.tiktok.com/@elektropemula/video/7421878163455741190
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int ledPins[] = {4, 6, 8, 10}; // Pin LED
const int buttonPins[] = {5, 7, 9, 11}; // Pin tombol
const int buzzerPin = 3; // Pin buzzer
int sequence[100]; // Array untuk menyimpan urutan LED
int level = 0;
// Frekuensi suara untuk setiap LED
const int tones[] = {261, 329, 392, 523}; // Do, Mi, Sol, Do tinggi
unsigned long debounceDelay = 200; // Waktu debounce (dalam milidetik)
unsigned long lastDebounceTime[4] = {0, 0, 0, 0}; // Penyimpan waktu debounce untuk setiap tombol
bool buttonState[4] = {HIGH, HIGH, HIGH, HIGH}; // Status tombol saat ini
bool lastButtonState[4] = {HIGH, HIGH, HIGH, HIGH}; // Status tombol sebelumnya
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Simon Says Game");
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(buzzerPin, OUTPUT);
delay(2000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Get Ready!");
delay(1000);
startNewLevel();
}
void loop() {
showSequence();
if (!getPlayerInput()) {
lcd.setCursor(0, 1);
lcd.print("Wrong! Game Over");
tone(buzzerPin, 1000, 1000); // Buzzer berbunyi jika salah
delay(2000);
lcd.clear();
level = 0; // Reset game
startNewLevel();
} else {
lcd.setCursor(0, 3);
lcd.print("Correct! Next LV");
delay(1000);
startNewLevel();
}
}
void startNewLevel() {
level++;
// Tambah dua LED acak ke urutan
for (int i = 0; i < 2; i++) {
sequence[level - 1 + i] = random(0, 4); // Tambahkan dua LED acak ke urutan
}
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Level: ");
lcd.print(level);
delay(1000);
}
void showSequence() {
for (int i = 0; i < level * 2; i++) { // Ganti level dengan level * 2
int currentLED = sequence[i];
digitalWrite(ledPins[currentLED], HIGH);
tone(buzzerPin, tones[currentLED]); // Mainkan nada sesuai LED
delay(500); // Tunda agar pemain bisa mengingat
digitalWrite(ledPins[currentLED], LOW);
noTone(buzzerPin); // Matikan buzzer setelah LED mati
delay(250);
}
}
bool getPlayerInput() {
for (int i = 0; i < level * 2; i++) { // Ganti level dengan level * 2
bool correctInput = false;
while (!correctInput) {
for (int j = 0; j < 4; j++) {
int reading = digitalRead(buttonPins[j]);
// Debounce tombol
if (reading != lastButtonState[j]) {
lastDebounceTime[j] = millis(); // Reset waktu debounce
}
if ((millis() - lastDebounceTime[j]) > debounceDelay) {
if (reading != buttonState[j]) {
buttonState[j] = reading;
if (buttonState[j] == LOW) {
if (j == sequence[i]) {
digitalWrite(ledPins[j], HIGH);
tone(buzzerPin, tones[j]); // Mainkan nada saat tombol ditekan
delay(400);
digitalWrite(ledPins[j], LOW);
noTone(buzzerPin); // Matikan buzzer setelah tombol dilepas
correctInput = true;
} else {
return false; // Pemain menekan tombol yang salah
}
}
}
}
lastButtonState[j] = reading;
}
}
}
return true;
}