void setup() { Serial.begin(9600); pinMode(2, INPUT); // carry out LED pinMode(3, INPUT); // sum1 LED pinMode(4, INPUT); // sum0 LED pinMode(5, INPUT); // button 1 (option A) pinMode(6, INPUT); // button 2 (option B) pinMode(7, INPUT); // touch sensor pinMode(8, OUTPUT); // buzzer } bool gameMode = false; bool lastTouch = false; int currentQuestion = 0; int score1 = 0; int score2 = 0; int currentPlayer = 1; bool waitingForAnswer = false; struct Question { String q; String optA; String optB; int correct; // 1 = A, 2 = B }; Question questions[] = { {"Does an AND gate output 1 when both inputs are 1?", "A) Yes", "B) No", 1}, {"What is 1 + 1 in binary?", "A) 10", "B) 11", 1}, {"Does an OR gate output 1 when only one input is 1?", "A) No", "B) Yes", 2}, {"What is the output of AND gate with inputs 1 and 0?", "A) 1", "B) 0", 2}, {"What is 01 + 01 in binary?", "A) 10", "B) 11", 1}, {"Does a XOR gate output 1 when both inputs are the same?", "A) Yes", "B) No", 2}, {"What is 10 + 01 in binary?", "A) 10", "B) 11", 2}, {"Does a NOT gate flip the input?", "A) Yes", "B) No", 1}, {"What is the output of OR gate with inputs 0 and 0?", "A) 0", "B) 1", 1}, {"What is 11 + 01 in binary?", "A) 100", "B) 010", 1} }; int totalQuestions = 10; void pointSound() { tone(8, 1000, 150); delay(200); tone(8, 1300, 150); delay(200); noTone(8); } void wrongSound() { tone(8, 300, 400); delay(450); tone(8, 250, 400); delay(450); noTone(8); } void winnerSound() { tone(8, 1000, 150); delay(180); tone(8, 1200, 150); delay(180); tone(8, 1400, 150); delay(180); tone(8, 1600, 150); delay(180); tone(8, 2000, 400); delay(450); noTone(8); } void askQuestion() { Serial.println("------------------------------"); Serial.print("PLAYER "); Serial.print(currentPlayer); Serial.println("'S TURN"); Serial.println(questions[currentQuestion].q); Serial.println(questions[currentQuestion].optA); Serial.println(questions[currentQuestion].optB); Serial.println("Press Button 1 for A, Button 2 for B"); waitingForAnswer = true; } void checkAnswer(int buttonPressed) { bool correct = (buttonPressed == questions[currentQuestion].correct); if (correct) { Serial.println("CORRECT!"); pointSound(); if (currentPlayer == 1) score1++; else score2++; } else { Serial.println("WRONG!"); wrongSound(); } Serial.print("Score -> Player 1: "); Serial.print(score1); Serial.print(" Player 2: "); Serial.println(score2); if (score1 >= 3) { Serial.println("*** PLAYER 1 WINS THE GAME! ***"); winnerSound(); score1 = 0; score2 = 0; currentQuestion = 0; currentPlayer = 1; waitingForAnswer = false; gameMode = false; Serial.println("--- BACK TO ADDER MODE ---"); return; } if (score2 >= 3) { Serial.println("*** PLAYER 2 WINS THE GAME! ***"); winnerSound(); score1 = 0; score2 = 0; currentQuestion = 0; currentPlayer = 1; waitingForAnswer = false; gameMode = false; Serial.println("--- BACK TO ADDER MODE ---"); return; } currentQuestion = (currentQuestion + 1) % totalQuestions; currentPlayer = (currentPlayer == 1) ? 2 : 1; waitingForAnswer = false; delay(1000); askQuestion(); } void loop() { bool touched = digitalRead(7); if (touched && !lastTouch) { gameMode = !gameMode; delay(300); if (gameMode) { Serial.println("=== GAME MODE STARTED ==="); Serial.println("First to 3 points wins!"); score1 = 0; score2 = 0; currentQuestion = 0; currentPlayer = 1; askQuestion(); } else { Serial.println("--- ADDER MODE ---"); waitingForAnswer = false; } } lastTouch = touched; if (!gameMode) { int carry = digitalRead(2); int sum1 = digitalRead(3); int sum0 = digitalRead(4); Serial.print("Result: "); Serial.print(carry); Serial.print(sum1); Serial.println(sum0); delay(500); } else { if (waitingForAnswer) { if (digitalRead(5) == HIGH) { delay(200); checkAnswer(1); } if (digitalRead(6) == HIGH) { delay(200); checkAnswer(2); } } } }