#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin konfigurasi
const int button1Pin = 12; // Tombol A
const int button2Pin = 14; // Tombol B
const int buzzerPin = 15; // Buzzer
// Data pertanyaan dan jawaban
struct Question {
String question;
String answerA;
String answerB;
char correctAnswer;
};
// Array pertanyaan
Question questions[] = {
{"Anda anak ke?", "A. Pertama", "B. Kedua", 'A'},
{"Suka warna apa?", "A. Hijau", "B. Merah", 'B'},
{"Film favorit?", "A. Horor", "B. Perang", 'A'},
{"Makanan favorit?", "A. Seblak", "B. Batagor", 'A'},
{"Minuman favorit?", "A. Kopi", "B. Susu", 'A'},
};
const int questionCount = sizeof(questions) / sizeof(questions[0]);
char userAnswers[questionCount]; // Menyimpan jawaban pengguna
int currentQuestion = 0;
bool gameOver = false;
void setup() {
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
if (gameOver) {
handleRestart();
return;
}
displayQuestionAlternately(); // Tampilkan pertanyaan dan jawaban secara bergantian
}
void displayQuestionAlternately() {
while (true) {
// Tampilkan pertanyaan
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(questions[currentQuestion].question);
delay(1000);
// Tampilkan jawaban
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(questions[currentQuestion].answerA);
lcd.setCursor(0, 1);
lcd.print(questions[currentQuestion].answerB);
delay(1000);
// Cek tombol ditekan
int button1State = digitalRead(button1Pin);
int button2State = digitalRead(button2Pin);
if (button1State == LOW) {
recordAnswer('A');
break;
} else if (button2State == LOW) {
recordAnswer('B');
break;
}
}
}
void recordAnswer(char answer) {
userAnswers[currentQuestion] = answer; // Rekam jawaban pengguna
currentQuestion++;
delay(300); // Hindari bouncing tombol
if (currentQuestion < questionCount) {
playBuzzer(); // Mainkan buzzer saat pindah ke pertanyaan berikutnya
}
if (currentQuestion >= questionCount) {
checkAnswers(); // Periksa semua jawaban setelah semua pertanyaan dijawab
}
}
void playBuzzer() {
tone(buzzerPin, 1000, 200); // Bunyi buzzer selama 200ms dengan frekuensi 1000Hz
}
void checkAnswers() {
bool allCorrect = true;
for (int i = 0; i < questionCount; i++) {
if (userAnswers[i] != questions[i].correctAnswer) {
allCorrect = false;
break;
}
}
if (allCorrect) {
gameOver = true;
playMelody();
lcd.clear();
lcd.print("Selamat Ulang");
lcd.setCursor(0, 1);
lcd.print("tahun Istriku");
delay(3000);
playMelody();
lcd.clear();
lcd.print("Hadiah ada");
lcd.setCursor(0, 1);
lcd.print("di lemari!");
} else {
lcd.clear();
lcd.print("Jawaban salah!");
lcd.setCursor(0, 1);
lcd.print("Tekan tombol...");
delay(2000);
resetGame(); // Reset ke awal
}
}
void resetGame() {
currentQuestion = 0;
gameOver = false;
}
void handleRestart() {
int button1State = digitalRead(button1Pin);
int button2State = digitalRead(button2Pin);
if (button1State == LOW || button2State == LOW) {
resetGame();
}
}
void playMelody() {
int melody[] = {262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349};
int noteDurations[] = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
for (int i = 0; i < 12; i++) {
int noteDuration = 1000 / noteDurations[i];
tone(buzzerPin, melody[i], noteDuration);
delay(noteDuration * 1.3);
noTone(buzzerPin);
}
}