#include <Wire.h>
#include <Keypad.h>
#include <SoftwareWire.h>
#include <SoftwareSerial.h>
#include "DFPlayer_Mini_Mp3.h"
// #include <NusabotSimpleTimer.h>
#include "LiquidCrystal_I2C_soft.h"
// #include <Arduino_FreeRTOS.h>
// #include <semphr.h>
// NusabotSimpleTimer timer;
SoftwareWire wire1(44, SCL);
SoftwareWire wire2(45, SCL);
LiquidCrystal_I2C lcd(&wire1, 0x27, 20, 4);
LiquidCrystal_I2C lcd1(&wire2, 0x27, 20, 4);
SoftwareSerial mySerial(3, 2); // Declare pin TX & RX
// //TX DF Player ke pin D3
// //RX DF Player ke pin D2 (melalui R 1k-4.7k)
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {11, 10, 9, 8};
byte pin_column[COLUMN_NUM] = {7, 6, 5, 4};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
char keys1[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows1[ROW_NUM] = {36, 37, 38, 39};
byte pin_column1[COLUMN_NUM] = {40, 41, 42, 43};
Keypad keypad1 = Keypad(makeKeymap(keys1), pin_rows1, pin_column1, ROW_NUM, COLUMN_NUM);
const int SEG_A1 = A0;
const int SEG_B1 = A1;
const int SEG_C1 = A2;
const int SEG_D1 = A3;
const int SEG_E1 = A4;
const int SEG_F1 = A5;
const int SEG_G1 = A6;
const int SEG_H1 = A7;
const int SEG_A = 28;
const int SEG_B = 29;
const int SEG_C = 30;
const int SEG_D = 31;
const int SEG_E = 32;
const int SEG_F = 33;
const int SEG_G = 34;
const int SEG_H = 35;
const int BUTTON_PIN = 22;
const int LED_PIN1 = 23;
const int BUZZER_PIN1 = 24;
const int LED_PIN = 26;
const int BUZZER_PIN = 27;
int score = 0;
int digit = 0;
int score1 = 0;
int digit1 = 0;
int difficulty = 0;
bool quizStarted = false;
bool quiz1Started = false;
unsigned long lastQuestionTime = 0;
unsigned long lastQuestionTime1 = 0;
int currentQuestionIndex = 1;
void setup() {
lcd.init();
lcd.backlight();
lcd1.init();
lcd1.backlight();
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN1, OUTPUT);
pinMode(BUZZER_PIN1, OUTPUT);
pinMode(SEG_A, OUTPUT);
pinMode(SEG_B, OUTPUT);
pinMode(SEG_C, OUTPUT);
pinMode(SEG_D, OUTPUT);
pinMode(SEG_E, OUTPUT);
pinMode(SEG_F, OUTPUT);
pinMode(SEG_G, OUTPUT);
pinMode(SEG_H, OUTPUT);
pinMode(SEG_A1, OUTPUT);
pinMode(SEG_B1, OUTPUT);
pinMode(SEG_C1, OUTPUT);
pinMode(SEG_D1, OUTPUT);
pinMode(SEG_E1, OUTPUT);
pinMode(SEG_F1, OUTPUT);
pinMode(SEG_G1, OUTPUT);
pinMode(SEG_H1, OUTPUT);
mySerial.begin (9600);
mp3_set_serial (mySerial);
delay(10);
mp3_reset();
delay(10);
mp3_set_volume (100);
delay(1000);
lcd.setCursor(0, 0);
// 01234567890123456789
lcd.print(" Pengembangan ");
lcd.setCursor(0, 1);
// 01234567890123456789
lcd.print(" Game Quiz Master ");
lcd.setCursor(0, 2);
// 01234567890123456789
lcd.print("Dwi Septiani Sahira");
lcd.setCursor(0, 3);
// 01234567890123456789
lcd.print(" 20101152620012 ");
lcd1.setCursor(0, 0);
// 01234567890123456789
lcd1.print(" Pengembangan ");
lcd1.setCursor(0, 1);
// 01234567890123456789
lcd1.print(" Game Quiz Master ");
lcd1.setCursor(0, 2);
// 01234567890123456789
lcd1.print("Dwi Septiani Sahira");
lcd1.setCursor(0, 3);
// 01234567890123456789
lcd1.print(" 20101152620012 ");
delay(3000);
lcd.clear();
lcd1.clear();
int progress = 0;
while (progress <= 100) {
lcd.setCursor(2, 1);
lcd.print("Progress : ");
lcd.print(progress);
lcd.print(" %");
lcd1.setCursor(2, 1);
lcd1.print("Progress : ");
lcd1.print(progress);
lcd1.print(" %");
delay(100);
progress += 10;
}
delay(500);
lcd.clear();
lcd1.clear();
delay(500);
// updateScore();
// updateScore1();
// timer.setInterval(500, startQuiz);
// timer.setInterval(1000, startQuiz1);
// xTaskCreate(main2, "Led Task", 15, NULL, 1, NULL);
// xTaskCreate(startQuiz, "QuizTask", 120, NULL, 2, NULL);
// xTaskCreate(startQuiz1, "Quiz1Task", 120, NULL, 2, NULL);
}
void loop(){
int potValue = analogRead(A8);
difficulty = map(potValue, 0, 1023, 1, 4);
lcd.setCursor(0, 1);
// 01234567890123456789
lcd.print(" Pilih Kesulitan: ");
lcd1.setCursor(0, 1);
// 01234567890123456789
lcd1.print(" Pilih Kesulitan: ");
if ((quizStarted == false) && (quiz1Started == false)){
displayDifficultySelection();
}
if (digitalRead(BUTTON_PIN) == LOW) {
quizStarted = true;
quiz1Started = true;
}
// vTaskDelay(100);
if (digitalRead(BUTTON_PIN) == LOW) {
quizStarted = true;
quiz1Started = true;
// delay(500);
}
if ((quizStarted == true) && (quiz1Started == true)) {
// timer.run();
startQuiz();
// delay(500);
// startQuiz1();
// delay(500);
// timer.setTimeout(500, startQuiz);
// timer.setTimeout(500, startQuiz1);
// delay(6000);
}
kosong();
kosong1();
}
// void main2(void *pvParameters) {
// while(1) {
// int potValue = analogRead(A8);
// difficulty = map(potValue, 0, 1023, 1, 4);
// lcd.setCursor(0, 1);
// // 01234567890123456789
// lcd.print(" Pilih Kesulitan: ");
// lcd1.setCursor(0, 1);
// // 01234567890123456789
// lcd1.print(" Pilih Kesulitan: ");
// if ((quizStarted == false) && (quiz1Started == false)){
// displayDifficultySelection();
// }
// if (digitalRead(BUTTON_PIN) == LOW) {
// quizStarted = true;
// quiz1Started = true;
// }
// vTaskDelay(100);
// kosong();
// kosong1();
// }
// }
void displayDifficultySelection() {
switch (difficulty) {
case 1:
lcd.setCursor(0, 2);
// 01234567890123456789
lcd.print(" Easy ");
lcd1.setCursor(0, 2);
// 01234567890123456789
lcd1.print(" Easy ");
break;
case 2:
lcd.setCursor(0, 2);
// 01234567890123456789
lcd.print(" Medium ");
lcd1.setCursor(0, 2);
// 01234567890123456789
lcd1.print(" Medium ");
break;
case 3:
lcd.setCursor(0, 2);
// 01234567890123456789
lcd.print(" Hard ");
lcd1.setCursor(0, 2);
// 01234567890123456789
lcd1.print(" Hard ");
break;
case 4:
lcd.setCursor(0, 2);
// 01234567890123456789
lcd.print(" Hard ");
lcd1.setCursor(0, 2);
// 01234567890123456789
lcd1.print(" Hard ");
break;
default:
break;
}
}
void startQuiz() {
if (quizStarted == true){
lcd.clear();
lcd1.clear();
score = 0;
score1 = 0;
updateScore();
updateScore1();
while (currentQuestionIndex <= 5) {
unsigned long currentTime = millis();
unsigned long currentTime1 = millis();
displayQuestion(currentQuestionIndex);
displayQuestion1(currentQuestionIndex);
delay(14000);
if (currentTime - lastQuestionTime >= 150) {
char userAnswer = waitForAnswer();
checkAnswer(userAnswer, currentQuestionIndex - 1);
lastQuestionTime = currentTime;
}
if (currentTime1 - lastQuestionTime1 >= 100) {
char userAnswer1 = waitForAnswer1();
checkAnswer1(userAnswer1, currentQuestionIndex - 1);
lastQuestionTime1 = currentTime1;
}
currentQuestionIndex++;
}
// if (difficulty != 0) {
// for (int i = 1; i <= 5; i++) {
// displayQuestion(i);
// displayQuestion1(i);
// delay(14000);
// char userAnswer = waitForAnswer();
// char userAnswer1 = waitForAnswer1();
// checkAnswer(userAnswer, i);
// checkAnswer1(userAnswer1, i);
// }
// }
lcd.clear();
lcd.setCursor(0, 1);
// 01234567890123456789
lcd.print(" Quiz Selesai ");
lcd.setCursor(0, 2);
// 01234567890123456789
lcd.print(" Skor : ");
lcd.print(score);
// delay(3000);
lcd1.clear();
lcd1.setCursor(0, 1);
// 01234567890123456789
lcd1.print(" Quiz Selesai ");
lcd1.setCursor(0, 2);
// 01234567890123456789
lcd1.print(" Skor : ");
lcd1.print(score1);
delay(3000);
difficulty = 0;
digit = 0;
updateScore();
quizStarted = false;
lcd.clear();
difficulty = 0;
digit1 = 0;
updateScore1();
quiz1Started = false;
lcd1.clear();
}
}
// int startQuiz1() {
// if (quiz1Started == true){
// lcd1.clear();
// score1 = 0;
// updateScore1();
// if (difficulty != 0) {
// for (int i = 1; i <= 5; i++) {
// displayQuestion1(i);
// char userAnswer1 = waitForAnswer1();
// checkAnswer1(userAnswer1, i);
// }
// }
// lcd1.clear();
// lcd1.setCursor(0, 1);
// // 01234567890123456789
// lcd1.print(" Quiz Selesai ");
// lcd1.setCursor(0, 2);
// // 01234567890123456789
// lcd1.print(" Skor : ");
// lcd1.print(score1);
// delay(3000);
// difficulty = 0;
// digit1 = 0;
// updateScore1();
// quiz1Started = false;
// lcd1.clear();
// }
// }
void displayQuestion(int questionNumber) {
switch (difficulty) {
case 1:
displayEasyQuestion(questionNumber);
displayEasyQuestion1(questionNumber);
break;
case 2:
displayMediumQuestion(questionNumber);
displayMediumQuestion1(questionNumber);
break;
case 3:
displayHardQuestion(questionNumber);
displayHardQuestion1(questionNumber);
break;
case 4:
displayHardQuestion(questionNumber);
displayHardQuestion1(questionNumber);
break;
}
delay(50);
}
void displayQuestion1(int questionNumber) {
switch (difficulty) {
case 1:
displayEasyQuestion1(questionNumber);
break;
case 2:
displayMediumQuestion1(questionNumber);
break;
case 3:
displayHardQuestion1(questionNumber);
break;
case 4:
displayHardQuestion1(questionNumber);
break;
}
delay(50);
}
void displayAnswerOptions(String optionA, String optionB, String optionC, String optionD) {
lcd.setCursor(0, 2);
// 01234567890123456789
lcd.print("A." + String(optionA));
lcd.setCursor(10, 2);
lcd.print("B." + String(optionB));
lcd.setCursor(0, 3);
lcd.print("C." + String(optionC));
lcd.setCursor(10, 3);
lcd.print("D." + String(optionD));
}
void displayAnswerOptions1(String optionA, String optionB, String optionC, String optionD) {
lcd1.setCursor(0, 2);
lcd1.print("A." + String(optionA));
lcd1.setCursor(10, 2);
lcd1.print("B." + String(optionB));
lcd1.setCursor(0, 3);
lcd1.print("C." + String(optionC));
lcd1.setCursor(10, 3);
lcd1.print("D." + String(optionD));
}
char waitForAnswer() {
char key;
while (true) {
key = keypad.getKey();
if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
break;
}
}
return key;
}
char waitForAnswer1() {
char key1;
while (true) {
key1 = keypad1.getKey();
if (key1 == 'A' || key1 == 'B' || key1 == 'C' || key1 == 'D') {
break;
}
}
return key1;
}
void checkAnswer(char userAnswer, int questionNumber) {
if (difficulty == 0) {
return;
}
char correctAnswer;
switch (difficulty) {
case 1:
correctAnswer = getEasyCorrectAnswer(questionNumber);
break;
case 2:
correctAnswer = getMediumCorrectAnswer(questionNumber);
break;
case 3:
correctAnswer = getHardCorrectAnswer(questionNumber);
break;
case 4:
correctAnswer = getHardCorrectAnswer(questionNumber);
break;
}
if (userAnswer == correctAnswer) {
score++;
updateScore();
flashLED();
} else {
buzzIncorrect();
}
}
void checkAnswer1(char userAnswer1, int questionNumber) {
if (difficulty == 0) {
return;
}
char correctAnswer;
switch (difficulty) {
case 1:
correctAnswer = getEasyCorrectAnswer(questionNumber);
break;
case 2:
correctAnswer = getMediumCorrectAnswer(questionNumber);
break;
case 3:
correctAnswer = getHardCorrectAnswer(questionNumber);
break;
case 4:
correctAnswer = getHardCorrectAnswer(questionNumber);
break;
}
if (userAnswer1 == correctAnswer) {
score1++;
updateScore1();
flashLED1();
} else {
buzzIncorrect1();
}
}
void flashLED() {
digitalWrite(LED_PIN, HIGH);
delay(1500);
digitalWrite(LED_PIN, LOW);
}
void flashLED1() {
digitalWrite(LED_PIN1, HIGH);
delay(1500);
digitalWrite(LED_PIN1, LOW);
}
void buzzIncorrect() {
tone(BUZZER_PIN, 700);
delay(500);
noTone(BUZZER_PIN);
delay(500);
tone(BUZZER_PIN, 900);
delay(500);
noTone(BUZZER_PIN);
}
void buzzIncorrect1() {
tone(BUZZER_PIN1, 700);
delay(500);
noTone(BUZZER_PIN1);
delay(500);
tone(BUZZER_PIN1, 900);
delay(500);
noTone(BUZZER_PIN1);
}
char getEasyCorrectAnswer(int questionNumber) {
switch (questionNumber) {
case 1:
return 'B';
case 2:
return 'C';
case 3:
return 'B';
case 4:
return 'B';
case 5:
return 'A';
}
}
char getMediumCorrectAnswer(int questionNumber) {
switch (questionNumber) {
case 1:
return 'A';
case 2:
return 'B';
case 3:
return 'A';
case 4:
return 'D';
case 5:
return 'C';
}
}
char getHardCorrectAnswer(int questionNumber) {
switch (questionNumber) {
case 1:
return 'B';
case 2:
return 'C';
case 3:
return 'C';
case 4:
return 'D';
case 5:
return 'A';
}
}
void updateScore() {
if (score > 5) {
digit = 5;
} else {
digit = score;
}
displayDigit(digit);
}
void updateScore1() {
if (score1 > 5) {
digit1 = 5;
} else {
digit1 = score1;
}
displayDigit1(digit1);
}
void displayDigit(int digit) {
if (digit == 0){
nol();
}
else if (digit == 1){
satu();
}
else if (digit == 2){
dua();
}
else if (digit == 3){
tiga();
}
else if (digit == 4){
empat();
}
else if (digit == 5){
lima();
}
}
void displayDigit1(int digit1) {
if (digit1 == 0){
nol1();
}
else if (digit1 == 1){
satu1();
}
else if (digit1 == 2){
dua1();
}
else if (digit1 == 3){
tiga1();
}
else if (digit1 == 4){
empat1();
}
else if (digit1 == 5){
lima1();
}
}