/*
Arduino | coding-help
Help me build my math quiz!!
potato — 9/21/26 6:01 AM
I'm new to arduino and yet I took on the stupid idea to make
a Math quiz using arduino for my project due this week.
How it should go: power on> welcome > push buttons to choose
the right answers> after 5 questions the display will show your score>
if the score is 5/5 then the servo motor will rotate a lil and
drop a candy! > back to start
What I got so far: just the circuit right.
I've written a code copied from here and there and I still don't
know why it's not working.
What I need help with: the code!! Please help me with the code!!!
*/
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
const int NUM_QUESTIONS = 5;
const int NUM_BTNS = 2;
const int BTN_PINS[] = {12, 11};
const int LED_PINS[] = {10, 9};
const int SERVO_PIN = 8;
const unsigned long DEBOUNCE_TIME = 20;
char *topLine[5] = {
"3*6+116=",
"19-34*61=",
"100/25*25=",
"99-56*225/5=",
"3+2x=11"
};
int botLine[5] = {
134,
34,
12,
-2421,
7
};
int answers[] = {1, 0, 0, 1, 0};
int score = 0;
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool checkAnswer(int answer, int qNum) {
//Serial.println(answer);
if (answer == answers[qNum]) {
score++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Correct! :)");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong :(");
}
delay(2000);
}
// function returns which button was pressed, or 0 if none
int checkButtons() {
static bool currState[NUM_BTNS];
static bool lastState[NUM_BTNS];
static unsigned long lastTime[NUM_BTNS];
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
currState[i] = digitalRead(BTN_PINS[i]);
unsigned long now = millis();
if (currState[i] != lastState[i] && now - lastTime[i] >= DEBOUNCE_TIME) {
if (!currState[i]) {
//Serial.println("Button pressed!");
btnPressed = i + 1;
} else {
//Serial.println("Button released!");
}
lastTime[i] = now;
lastState[i] = currState[i];
}
}
return btnPressed;
}
int showQuestion(int number) {
int answer = 0;
int btnNumber = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(topLine[number]);
lcd.setCursor(9, 1);
lcd.print(botLine[number]);
while (btnNumber == 0) {
btnNumber = checkButtons();
if (btnNumber) {
Serial.print("Question ");
Serial.print(number + 1);
Serial.print(" answer - ");
if (btnNumber == 1) {
answer = 1;
Serial.println("Yes");
}
if (btnNumber == 2) {
Serial.println("No");
}
}
}
return answer;
}
void showRules() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Is this correct?");
lcd.setCursor(0, 1);
lcd.print("Answer Yes or No");
delay(1000);
}
void showScore() {
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Score ");
lcd.setCursor(7, 1);
lcd.print(score);
Serial.print("Score: ");
Serial.println(score);
// check for win
if (score == 5) {
servo.write(0);
digitalWrite(LED_PINS[0], HIGH);
digitalWrite(LED_PINS[1], LOW);
} else {
digitalWrite(LED_PINS[0], LOW);
digitalWrite(LED_PINS[1], HIGH);
}
delay(5000);
// reset
score = 0;
servo.write(90);
digitalWrite(LED_PINS[0], LOW);
digitalWrite(LED_PINS[1], LOW);
}
void showSplash() {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Math Quiz");
lcd.setCursor(5, 1);
lcd.print("V1.0");
delay(2000);
lcd.clear();
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
servo.attach(SERVO_PIN);
servo.write(90);
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
pinMode(LED_PINS[i], OUTPUT);
}
showSplash();
}
void loop() {
static int qNumber = 0;
showRules();
int answer = showQuestion(qNumber);
checkAnswer(answer, qNumber);
qNumber++;
if (qNumber == NUM_QUESTIONS) {
showScore();
qNumber = 0;
}
}
YES
NO