This is my first tinkercad arduino uno program and I am trying to build a piece of code that will teach children maths. I ran the following program and it gives errors saying, "invalid header file" .
Can anyone help me solve it? Many Thanks.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define LCDs with their I2C addresses
LiquidCrystal_I2C lcdQuestion(0x27, 16, 2); // Question display
LiquidCrystal_I2C lcdAnswer1(0x26, 16, 2); // Answer 1 display
LiquidCrystal_I2C lcdAnswer2(0x25, 16, 2); // Answer 2 display
LiquidCrystal_I2C lcdAnswer3(0x24, 16, 2); // Answer 3 display
LiquidCrystal_I2C lcdAnswer4(0x23, 16, 2); // Answer 4 display
// Button pins
const int button1 = 2; // Answer 1
const int button2 = 3; // Answer 2
const int button3 = 4; // Answer 3
const int button4 = 5; // Answer 4
const int buttonReset = 6; // Reset and difficulty level
// Game variables
int level = 1; // Difficulty level
int score = 0; // Player's score
int correctAnswer = 0; // Correct answer index
unsigned long lastPress = 0;
bool waitingForAnswer = true;
// Function to generate a math question
void generateQuestion() {
int num1, num2, answer, wrong1, wrong2, wrong3;
char operatorSymbol;
// Generate random numbers and operator based on level
if (level == 1) {
num1 = random(1, 11);
num2 = random(1, 11);
if (random(0, 2) == 0) {
operatorSymbol = '+';
answer = num1 + num2;
} else {
operatorSymbol = '-';
answer = num1 - num2;
}
} else if (level >= 2) {
num1 = random(1, 21);
num2 = random(1, 21);
int operatorChoice = random(0, 4);
if (operatorChoice == 0) {
operatorSymbol = '+';
answer = num1 + num2;
} else if (operatorChoice == 1) {
operatorSymbol = '-';
answer = num1 - num2;
} else if (operatorChoice == 2) {
operatorSymbol = '*';
answer = num1 * num2;
} else {
operatorSymbol = '/';
num2 = random(1, 11); // Prevent division by zero and large results
num1 = num2 * random(1, 11); // Ensure clean division
answer = num1 / num2;
operatorSymbol = '/';
}
}
// Display the question
lcdQuestion.clear();
lcdQuestion.print("Q: ");
lcdQuestion.print(num1);
lcdQuestion.print(" ");
lcdQuestion.print(operatorSymbol);
lcdQuestion.print(" ");
lcdQuestion.print(num2);
// Generate answers
correctAnswer = random(1, 5);
wrong1 = answer + random(-5, 6);
wrong2 = answer + random(-5, 6);
wrong3 = answer + random(-5, 6);
// Assign answers to displays
lcdAnswer1.clear();
lcdAnswer2.clear();
lcdAnswer3.clear();
lcdAnswer4.clear();
if (correctAnswer == 1) lcdAnswer1.print(answer); else lcdAnswer1.print(wrong1);
if (correctAnswer == 2) lcdAnswer2.print(answer); else lcdAnswer2.print(wrong2);
if (correctAnswer == 3) lcdAnswer3.print(answer); else lcdAnswer3.print(wrong3);
if (correctAnswer == 4) lcdAnswer4.print(answer); else lcdAnswer4.print(wrong1 + 1);
}
// Function to handle button presses
void handleButtonPress(int button) {
if (!waitingForAnswer) return;
if (button == correctAnswer) {
score++;
lcdQuestion.clear();
lcdQuestion.print("Correct!");
} else {
lcdQuestion.clear();
lcdQuestion.print("Wrong!");
}
waitingForAnswer = false;
delay(2000);
generateQuestion();
waitingForAnswer = true;
}
// Function to reset the game
void resetGame() {
score = 0;
level = 1;
lcdQuestion.clear();
lcdQuestion.print("Level 1");
delay(2000);
generateQuestion();
}
// Function to increase difficulty
void increaseDifficulty() {
level++;
if (level > 3) level = 1;
lcdQuestion.clear();
lcdQuestion.print("Level ");
lcdQuestion.print(level);
delay(2000);
generateQuestion();
}
void setup() {
// Initialize LCDs
lcdQuestion.begin();
lcdAnswer1.begin();
lcdAnswer2.begin();
lcdAnswer3.begin();
lcdAnswer4.begin();
lcdQuestion.backlight();
lcdAnswer1.backlight();
lcdAnswer2.backlight();
lcdAnswer3.backlight();
lcdAnswer4.backlight();
// Initialize buttons
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(buttonReset, INPUT_PULLUP);
// Start game
generateQuestion();
}
void loop() {
if (digitalRead(button1) == LOW) handleButtonPress(1);
if (digitalRead(button2) == LOW) handleButtonPress(2);
if (digitalRead(button3) == LOW) handleButtonPress(3);
if (digitalRead(button4) == LOW) handleButtonPress(4);
// Reset or increase difficulty
if (digitalRead(buttonReset) == LOW) {
unsigned long currentPress = millis();
if (currentPress - lastPress < 500) {
increaseDifficulty(); // Double click detected
} else {
resetGame(); // Single click detected
}
lastPress = currentPress;
}
}
