It picks the hint and checks your answer.
Arduino
The brain
game state with if decisions
Remember the Serial hint and press the correct button to grow your score.

Pin connections
| Part 1 | Part 2 | |
|---|---|---|
Arduino pin 2 | → | Button A pin 1 |
Button A pin 2 | → | Arduino GND |
Arduino pin 3 | → | Button B pin 1 |
Button B pin 2 | → | Arduino GND |
Arduino pin 13 | → | Resistor pin 1 |
Resistor pin 2 | → | Score LED anode (+) |
Score LED cathode (-) | → | Arduino GND |
Let's test your memory!
The screen gives a hint — press the matching button and build up your score.
Games keep little memory boxes, like your score, and use them to decide what happens next.
The problem
The program needs to remember things between presses, not just read once.
Think of it like
Like remembering a card's color and putting it on the matching pile.
It picks the hint and checks your answer.
Arduino
The brain
Press it when the hint says A.
Button A
Button A
Press it when the hint says B.
Button B
Button B
Keeps the score light safe.
Resistor
The protector
It flashes when you answer right.
Score LED
The score light
Pick the answer
The game secretly picks A or B and prints a hint for you.
lastChoice = random(0, 2);
Wait for your press
A press only counts while the game is waiting for your answer.
if (waiting && digitalRead(BTN_A) == LOW)
Right or wrong?
A right press adds to your score; a wrong press sets it back to 0.
if (choice == lastChoice) score++; else score = 0;
Then loop back to step 1
Follow these steps in order. Match the wires to the colors shown.
Place Arduino
Place the Arduino (uno) on the breadboard.
Arduino placed — ready to build!
Place Button A
Place the Button A (btnA) on the breadboard.
Place Button B
Place the Button B (btnB) on the breadboard.
Place Resistor
Place the Resistor (r1) on the breadboard.
Place Score LED
Place the Score LED (led1) on the breadboard.
Connect Arduino pin 2 to Button A (btnA) 1.l
Connect Arduino pin 2 to Button A (btnA) 1.l.
Connect Button A (btnA) 2.l to Arduino GND
Connect Button A (btnA) 2.l to Arduino GND.
Connect Arduino pin 3 to Button B (btnB) 1.l
Connect Arduino pin 3 to Button B (btnB) 1.l.
Connect Button B (btnB) 2.l to Arduino GND
Connect Button B (btnB) 2.l to Arduino GND.
Connect Arduino pin 13 to Resistor (r1) 1
Connect Arduino pin 13 to Resistor (r1) 1.
Connect Resistor (r1) 2 to Score LED (led1) anode (+)
Connect Resistor (r1) 2 to Score LED (led1) anode (+).
Connect Score LED (led1) cathode (-) to Arduino GND
Connect Score LED (led1) cathode (-) to Arduino GND.
Memory boxes
int lastChoice = 0; int score = 0; bool waiting = true;
lastChoice, score, and waiting remember the game between loop() runs.
check()
void check(int choice) {
waiting = false;
if (choice == lastChoice) {
score++;
digitalWrite(SCORE_LED, HIGH);
Serial.print("Correct! Score: ");
Serial.println(score);
} else {
score = 0;
digitalWrite(SCORE_LED, LOW);
Serial.println("Wrong — score reset");
}
delay(800);
digitalWrite(SCORE_LED, LOW);
delay(400);
pickNext();
while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
delay(10);
}
}It compares your press to the secret answer and updates your score and light.
The main loop
void loop() {
if (waiting && digitalRead(BTN_A) == LOW) {
check(0);
} else if (waiting && digitalRead(BTN_B) == LOW) {
check(1);
}
delay(20);
}It sends button A or B to check(), but only while the game is waiting.
const int BTN_A = 2;
const int BTN_B = 3;
const int SCORE_LED = 13;
int lastChoice = 0;
int score = 0;
bool waiting = true;
void pickNext() {
lastChoice = random(0, 2);
waiting = true;
Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");
}
void check(int choice) {
waiting = false;
if (choice == lastChoice) {
score++;
digitalWrite(SCORE_LED, HIGH);
Serial.print("Correct! Score: ");
Serial.println(score);
} else {
score = 0;
digitalWrite(SCORE_LED, LOW);
Serial.println("Wrong — score reset");
}
delay(800);
digitalWrite(SCORE_LED, LOW);
delay(400);
pickNext();
while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
delay(10);
}
}
void setup() {
pinMode(BTN_A, INPUT_PULLUP);
pinMode(BTN_B, INPUT_PULLUP);
pinMode(SCORE_LED, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
Serial.println("Memory Game — match the hint!");
pickNext();
}
void loop() {
if (waiting && digitalRead(BTN_A) == LOW) {
check(0);
} else if (waiting && digitalRead(BTN_B) == LOW) {
check(1);
}
delay(20);
}
Q1. Where does work that repeats go?
Q2. What does the waiting box do?
Change the hint words on line 10 to include the word "Remember".
Hint: Edit the Serial.println inside pickNext().
Give more time after each answer — change delay(800) to delay(1200).
Hint: That's line 24 inside check().
A line-by-line tour of the sketch — the same steps as in Robot Gurukul Studio.
Program overview
Big idea
Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.
In this project
The screen says "press A" or "press B". Pick the right one and your score goes up.
Tip
Read from the top to the bottom. Tap any word or line if you need help!
const int BTN_A = 2;
const int BTN_B = 3;
const int SCORE_LED = 13;
int lastChoice = 0;
int score = 0;
bool waiting = true;
void pickNext() {
lastChoice = random(0, 2);
waiting = true;
Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");
}
void check(int choice) {
waiting = false;
if (choice == lastChoice) {
score++;
digitalWrite(SCORE_LED, HIGH);
Serial.print("Correct! Score: ");
Serial.println(score);
} else {
score = 0;
digitalWrite(SCORE_LED, LOW);
Serial.println("Wrong — score reset");
}
delay(800);
digitalWrite(SCORE_LED, LOW);
delay(400);
pickNext();
while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
delay(10);
}
}setup()
Big idea
setup() runs one time when the board turns on.
In this project
It gets both buttons and the score light ready, mixes up the randomness, and shows the first hint.
Why here
Things we do only once go inside setup().
void setup() {
pinMode(BTN_A, INPUT_PULLUP);
pinMode(BTN_B, INPUT_PULLUP);
pinMode(SCORE_LED, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
Serial.println("Memory Game — match the hint!");
pickNext();
}loop()
Big idea
loop() runs again and again, forever.
In this project
It waits for you to press A or B, then checks your answer.
Why here
Things that repeat go inside loop().
void loop() {
if (waiting && digitalRead(BTN_A) == LOW) {
check(0);
} else if (waiting && digitalRead(BTN_B) == LOW) {
check(1);
}
delay(20);
}
Try this: Change a number inside loop(), then press Run to see what happens.
pinMode
Big idea
pinMode tells a pin if it will listen or push power out.
In this project
It sets the button pins to listen and the score light pin to push power out.
Why here
It goes in setup() because we only set it once.
pinMode(BTN_A, INPUT_PULLUP);
digitalWrite
Big idea
digitalWrite turns a pin ON or OFF.
In this project
It lights the score LED when you answer right.
Why here
It is used inside check() to show how you did.
digitalWrite(SCORE_LED, HIGH);
digitalRead
Big idea
digitalRead checks if a pin is ON or OFF.
In this project
It checks which button you pressed.
Why here
It goes in loop() so we can react to your press.
while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {analogRead
Big idea
analogRead reads a number from a pin, from 0 up to 1023.
In this project
It reads a wiggly number from an empty pin to mix up the randomness.
Why here
In setup() so the hints are different each game.
randomSeed(analogRead(0));
delay
Big idea
delay means wait. Nothing else happens while it waits.
In this project
It pauses after each answer so you can see how you did.
Why here
Right after we show the score light.
delay(800);
random
Big idea
random picks a surprise number for you.
In this project
It secretly picks A or B for the next hint.
Why here
When we set up the next round.
lastChoice = random(0, 2);
begin
Big idea
Serial.begin opens a way for the board to send words to your computer screen.
In this project
It lets the program print the hints and your score.
Why here
It goes in setup() once, before we print anything.
Serial.begin(9600);
Big idea
print sends words to the screen but stays on the same line.
In this project
It prints "Correct! Score: " right before your number.
Why here
In check() so the label and number sit together.
Serial.print("Correct! Score: ");println
Big idea
println sends a line of words to the screen.
In this project
It prints the hint of which button to press.
Why here
In pickNext() so each hint is on its own line.
Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");