This circuit is a quiz game system built using an Arduino UNO, an I2C LCD 16x2 screen, three pushbuttons, and a resistor. The Arduino UNO controls the LCD screen to display questions and receives input from the pushbuttons to determine the user's answers. The system keeps track of the score and allows the user to restart the quiz.
I2C LCD 16x2 Screen
Arduino UNO
Pushbutton (3 units)
Resistor
Comment (3 units)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize LCD: 0x27 is the I2C address, adjust if necessary
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin definitions
const int trueButton = 7;
const int falseButton = 8;
const int restartButton = 9;
// Quiz data: 6 questions with answers (true or false)
String questions[] = {
"Kean Handsome?", // True
"Pi equals 3.14?", // True
"1+1=3?", // False
"2+2=Lapu-Lapu?", // True
"Mlue is a color?", // False
"Tomato is Fruit?" // True
};
bool answers[] = {true, true, false, true, false, true};
int totalQuestions = 6; // Number of questions
// Variables
int currentQuestion = 0;
int score = 0;
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
pinMode(trueButton, INPUT_PULLUP);
pinMode(falseButton, INPUT_PULLUP);
pinMode(restartButton, INPUT_PULLUP);
displayQuestion();
}
void loop() {
if (digitalRead(trueButton) == LOW) {
checkAnswer(true);
}
else if (digitalRead(falseButton) == LOW) {
checkAnswer(false);
}
else if (digitalRead(restartButton) == LOW) {
restartQuiz();
}
}
void displayQuestion() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Q" + String(currentQuestion + 1) + ":");
lcd.setCursor(0, 1);
lcd.print(questions[currentQuestion]);
}
void checkAnswer(bool userAnswer) {
lcd.clear();
if (userAnswer == answers[currentQuestion]) {
lcd.print("Correct!");
score++;
} else {
lcd.print("Incorrect!");
}
delay(2000); // Display result for 2 seconds
currentQuestion++;
if (currentQuestion < totalQuestions) {
displayQuestion();
} else {
displayScore();
}
}
void displayScore() {
lcd.clear();
lcd.print("Score: ");
lcd.print(score);
lcd.setCursor(0, 1);
lcd.print("Try Again?");
}
void restartQuiz() {
currentQuestion = 0;
score = 0;
displayQuestion();
}
This code initializes the LCD screen and sets up the pins for the pushbuttons. It then enters a loop where it waits for the user to press one of the buttons to answer the quiz questions or restart the quiz. The LCD displays the current question, and the user's answer is checked against the correct answer. The score is displayed at the end of the quiz, and the user can restart the quiz by pressing the restart button.