#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Keypad.h>
#define TFT_DC 48
#define TFT_CS 53
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
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);
const int SEG_A = A0;
const int SEG_B = A1;
const int SEG_C = A2;
const int SEG_D = A3;
const int SEG_E = A4;
const int SEG_F = A5;
const int SEG_G = A6;
const int SEG_H = A7;
const int BUTTON_PIN = 22;
const int LED_PIN = 23;
const int BUZZER_PIN = 24;
int score = 0;
int digit = 0;
int difficulty = 0;
void setup() {
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, 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);
tft.begin();
tft.setRotation(3);
tft.setCursor(100, 40);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(3);
tft.println("Hello And");
tft.setCursor(80, 100);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Wellcome To");
tft.setCursor(70, 160);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.println("Unknown AIoT");
delay(5000);
updateScore();
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(3);
}
void loop() {
char key = keypad.getKey();
int potValue = analogRead(A8);
difficulty = map(potValue, 0, 1023, 1, 4);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(30, 50);
tft.print("Pilih Kesulitan:");
tft.setCursor(30, 100);
displayDifficultySelection();
// delay(100);
if (digitalRead(BUTTON_PIN) == LOW) {
startQuiz();
delay(1000);
}
if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
checkAnswer(key, 1);
}
}
void displayDifficultySelection() {
switch (difficulty) {
case 1:
tft.print("Easy");
break;
case 2:
tft.print("Medium");
break;
case 3:
tft.print("Hard");
break;
case 4:
tft.print("Hard");
break;
default:
break;
}
}
void startQuiz() {
tft.fillScreen(ILI9341_BLACK);
score = 0;
updateScore();
if (difficulty != 0) {
for (int i = 1; i <= 5; i++) {
displayQuestion(i);
char userAnswer = waitForAnswer();
checkAnswer(userAnswer, i);
}
}
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(50, 80);
tft.print("Quiz Selesai");
tft.setCursor(50, 150);
tft.print("Skor: ");
tft.print(score);
delay(2000);
difficulty = 0;
digit = 0;
updateScore();
tft.fillScreen(ILI9341_BLACK);
displayDifficultySelection();
}
void displayQuestion(int questionNumber) {
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
switch (difficulty) {
case 1:
displayEasyQuestion(questionNumber);
break;
case 2:
displayMediumQuestion(questionNumber);
break;
case 3:
displayHardQuestion(questionNumber);
break;
case 4:
displayHardQuestion(questionNumber);
break;
}
delay(2000);
}
void displayEasyQuestion(int questionNumber) {
switch (questionNumber) {
case 1:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("2 + 2 = ?");
displayAnswerOptions("4", "5", "6", "7");
break;
case 2:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("5 - 3 + 2 = ?");
displayAnswerOptions("3", "4", "5", "7");
break;
case 3:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("1 + 2 - 3 + 4 - 1 = ?");
displayAnswerOptions("4", "5", "3", "7");
break;
case 4:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("17 + 5 - 9 = ?");
displayAnswerOptions("11", "9", "10", "12");
break;
case 5:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("8 - 7 + 5 = ?");
displayAnswerOptions("6", "5", "4", "3");
break;
}
}
void displayMediumQuestion(int questionNumber) {
switch (questionNumber) {
case 1:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("2 / 2 = ?");
displayAnswerOptions("7", "5", "3", "1");
break;
case 2:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("5 * 3 = ?");
displayAnswerOptions("17", "16", "15", "14");
break;
case 3:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("6 / 3 * 4 = ?");
displayAnswerOptions("4", "8", "6", "7");
break;
case 4:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("15 / 5 * 2 = ?");
displayAnswerOptions("6", "5", "9", "7");
break;
case 5:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("4 * 5 / 2 = ?");
displayAnswerOptions("4", "8", "12", "10");
break;
}
}
void displayHardQuestion(int questionNumber) {
switch (questionNumber) {
case 1:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("2 / 2 - 1 = ?");
displayAnswerOptions("0", "5", "6", "7");
break;
case 2:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("5 * 3 + 7 = ?");
displayAnswerOptions("12", "23", "22", "24");
break;
case 3:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("10 + 2 / 3 * 4 = ?");
displayAnswerOptions("4", "3", "1", "2");
break;
case 4:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("15 / 5 * 2 - 6 = ?");
displayAnswerOptions("3", "2", "1", "0");
break;
case 5:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 40);
tft.print("Pertanyaan " + String(questionNumber));
tft.setCursor(50, 80);
tft.print("4 * 5 / 2 + 5 - 7 = ?");
displayAnswerOptions("8", "9", "10", "11");
break;
}
}
void displayAnswerOptions(String optionA, String optionB, String optionC, String optionD) {
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(50, 120);
tft.print("A. " + String(optionA));
tft.setCursor(50, 150);
tft.print("B. " + String(optionB));
tft.setCursor(50, 180);
tft.print("C. " + String(optionC));
tft.setCursor(50, 210);
tft.print("D. " + String(optionD));
}
char waitForAnswer() {
char key;
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(50, 280);
tft.print("Jawab:");
while (true) {
key = keypad.getKey();
if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
break;
}
}
return key;
}
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;
}
if (userAnswer == correctAnswer) {
score++;
updateScore();
flashLED();
} else {
buzzIncorrect();
}
}
void flashLED() {
digitalWrite(LED_PIN, HIGH);
delay(1500);
digitalWrite(LED_PIN, LOW);
}
void buzzIncorrect() {
tone(BUZZER_PIN, 700);
delay(500);
noTone(BUZZER_PIN);
delay(500);
tone(BUZZER_PIN, 900);
delay(500);
noTone(BUZZER_PIN);
}
char getEasyCorrectAnswer(int questionNumber) {
switch (questionNumber) {
case 1:
return 'A';
case 2:
return 'B';
case 3:
return 'C';
case 4:
return 'D';
case 5:
return 'A';
}
}
char getMediumCorrectAnswer(int questionNumber) {
switch (questionNumber) {
case 1:
return 'D';
case 2:
return 'C';
case 3:
return 'B';
case 4:
return 'A';
case 5:
return 'D';
}
}
char getHardCorrectAnswer(int questionNumber) {
switch (questionNumber) {
case 1:
return 'A';
case 2:
return 'B';
case 3:
return 'C';
case 4:
return 'C';
case 5:
return 'A';
}
}
void updateScore() {
if (score > 5) {
digit = 5;
} else {
digit = score;
}
displayDigit(digit);
}
void displayDigit(int digit) {
if (digit == 0){
digitalWrite(SEG_A, 1);
digitalWrite(SEG_B, 1);
digitalWrite(SEG_C, 1);
digitalWrite(SEG_D, 1);
digitalWrite(SEG_E, 1);
digitalWrite(SEG_F, 1);
digitalWrite(SEG_G, 0);
digitalWrite(SEG_H, 1);
}
else if (digit == 1){
digitalWrite(SEG_A, 0);
digitalWrite(SEG_B, 1);
digitalWrite(SEG_C, 1);
digitalWrite(SEG_D, 0);
digitalWrite(SEG_E, 0);
digitalWrite(SEG_F, 0);
digitalWrite(SEG_G, 0);
digitalWrite(SEG_H, 1);
}
else if (digit == 2){
digitalWrite(SEG_A, 1);
digitalWrite(SEG_B, 1);
digitalWrite(SEG_C, 0);
digitalWrite(SEG_D, 1);
digitalWrite(SEG_E, 1);
digitalWrite(SEG_F, 0);
digitalWrite(SEG_G, 1);
digitalWrite(SEG_H, 1);
}
else if (digit == 3){
digitalWrite(SEG_A, 1);
digitalWrite(SEG_B, 1);
digitalWrite(SEG_C, 1);
digitalWrite(SEG_D, 1);
digitalWrite(SEG_E, 0);
digitalWrite(SEG_F, 0);
digitalWrite(SEG_G, 1);
digitalWrite(SEG_H, 1);
}
else if (digit == 4){
digitalWrite(SEG_A, 0);
digitalWrite(SEG_B, 1);
digitalWrite(SEG_C, 1);
digitalWrite(SEG_D, 0);
digitalWrite(SEG_E, 0);
digitalWrite(SEG_F, 1);
digitalWrite(SEG_G, 1);
digitalWrite(SEG_H, 1);
}
else if (digit == 5){
digitalWrite(SEG_A, 1);
digitalWrite(SEG_B, 0);
digitalWrite(SEG_C, 1);
digitalWrite(SEG_D, 1);
digitalWrite(SEG_E, 0);
digitalWrite(SEG_F, 1);
digitalWrite(SEG_G, 1);
digitalWrite(SEG_H, 1);
}
}