A random math question appears at the top of the OLED display while three moving targets display possible answers. Use a potentiometer to aim the cannon, press a push button to fire, and hit the correct answer before it moves away.
Components RequiredArduino Uno
SSD1306 I²C OLED Display (128×64)
10 kΩ Potentiometer
Push Button
Breadboard
Jumper Wires
Circuit DiagramAs you can see three answer bubbles are moving on OLED display, with one containing the correct answer.
The potentiometer controls the angle of the cannon. When push button is pressed, a projectile is launched in the selected direction.
If the projectile hits the correct answer you will score +10 points and A new question appears
If the wrong answer is hit score decreases by 5 points. You have a strike of 3 wrong answers. After that Game Over. Press button again to restart.
EWsim Live Simulation Link:Link: EWsim Project Link
How the Code WorksFirst we will initialize all the variables. Like int score = 0, wrongCount = 0, bool gameOver = false, int numA = 0, numB = 0, correctAnswer = 0, char opSymbol = 'x';
Here numA and numB are the numbers in the question and I have used opSymbol to represent the operation ie multiplication or addition for our random question generation.
Next we define a target struct instead of writing separate variables for every target. We have x, y positions, value is the number inside them and active to check if it is still active. Because we have three targets, array becomes targets[3].
struct Target {
float x, y, speed;
int value;
bool active;
};
Target targets[3];For bullet :bulletX bulletY - current position of bullet
bulletVx, bulletVy - velocity of bullet
bulletActive - check whether a bullet is currently flying.
Here we are using speed- float s = 4.5;
So to calculate the values we split in two components
bulletVx = s * cos(rad);
bulletVy = -s * sin(rad);
And then these components are then added to the bullet's position in every loop to create its trajectory.
bulletX += bulletVx;
bulletY += bulletVy;
For new question generationThis function first randomly chooses between multiplication and addition.
void generateNewQuestion() {
int op = random(0, 2);
if (op == 0) {
opSymbol = 'x';
numA = random(2, 10);
numB = random(2, 10);
correctAnswer = numA * numB;
}
else {
opSymbol = '+';
numA = random(10, 45);
numB = random(10, 45);
correctAnswer = numA + numB;
}For multiplication, two numbers between 2 and 9 are generated. For addition, two numbers between 10 and 44 are generated. The program then calculates and stores the correct answer in correctAnswer
Create three targetsWe create three moving targets and randomly chooses one of them to contain the correct answer. Each target is given a random starting position, a different vertical position, and a random horizontal speed. The positive or negative speed determines whether the target moves right or left
int c = random(0, 3);
for (int i = 0; i < 3; i++) {
targets[i].x = random(15, 95);
targets[i].y = 18 + i * 9;
targets[i].speed = (random(5, 15) / 10.0) * (random(0, 2) ? 1 : -1);
targets[i].active = true;The selected target gets the correct answer.
if (i == c)
targets[i].value = correctAnswer;For the other two targets, this generates incorrect answers close to the correct answer. It also checks that an incorrect target doesn't accidentally contain the correct answer.
else {
int w;
do {
w = correctAnswer + random(-10, 11);
} while (w <= 0 || w == correctAnswer);
targets[i].value = w;
}For aiming and firingThe potentiometer gives an analog value between 0 and 1023. We map this value to an angle between 15° and 165° to control the cannon's direction. When the fire button is pressed, the bullet starts from the cannon and its horizontal and vertical velocity components are calculated using this angle.
int rawAim = analogRead(POT_AIM_PIN);
float deg = map(rawAim, 0, 1023, 165, 15);
float rad = deg * 3.14159 / 180.0;Collision + scoringThe program continuously checks the distance between the bullet and each active target. If the distance is less than a certain value, the bullet is considered to have hit the target. The program then compares the target's value with correctAnswer
if (targets[i].value == correctAnswer) {
score += 10;
generateNewQuestion();
}
else {
score = max(0, score - 5);
wrongCount++;
}Moving the targetsIn every loop, the target's speed is added to its x-position, making it move horizontally. When it reaches the edge of the screen, its speed is reversed, causing it to move back in the opposite direction.
targets[i].x += targets[i].speed;
if (targets[i].x < 10 || targets[i].x > 118)
targets[i].speed *= -1;The Arduino continuously repeats these steps: read the cannon angle, move the targets, check for a button press, move the projectile, and check for collisions. The OLED is refreshed after each loop to show the updated question, targets, cannon, score, and feedback. This creates the real-time gameplay where you have to aim and fire at the correct answer before the targets move away.
Still need to fix a few things in the last display screen and also new features can be added

_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)









_3u05Tpwasz.png?auto=compress%2Cformat&w=40&h=40&fit=fillmax&bg=fff&dpr=2)
Comments