What it shows
The Arduino stores a growing random sequence in an array, plays it with lights and tones, and checks the player repeats it. It teaches arrays, loops and random numbers in a game.
The science
The sketch keeps an array of up to 20 numbers from 0 to 3 for the colours. Each round it adds a new random number, plays the whole list by lighting each LED with its own tone and then waits for the player. Each button press is compared with the stored value at that position. A wrong press plays a low tone and starts again; a full match adds another step.
Materials
Cost: under Rs 1,500. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- An Arduino Uno or compatible board
- Four LEDs of different colours and four 220 Ω resistors
- Four push buttons
- A small piezo buzzer
- A breadboard and jumper wires
- A cardboard box lid for the game panel
Safety
For this project: USB power only.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
Connect the LEDs to pins 2 to 5 through 220 Ω resistors, each to GND.
Connect the buttons between pins 8 to 11 and GND, using INPUT_PULLUP.
Connect the buzzer to pin 6 and GND.
Assign four tones: 262, 330, 392 and 523 Hz, one per colour, using the tone() function.
Call randomSeed(analogRead(A0)) in setup so that each game differs.
Write a function to play the first n steps of the sequence, with 400 ms for each light.
Write a function to read the player’s presses, compare each with the sequence and return true or false.
On success increase the length by one; on failure play a sad tone and reset the length to one.
What you should see
The game plays longer and longer sequences, and a wrong press ends the game, so the player’s memory limit can be recorded.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1What is an array?
A numbered list of values stored in order.
2Why seed the random generator with analogRead on an unused pin?
A floating pin gives unpredictable numbers, so the game differs each time.
3What does tone(pin, frequency) do?
It makes a square wave of that frequency on the pin for the buzzer.
4How does the game check the player?
It compares each press with the stored sequence in order.
Take it further
Add a second mode where two players take turns adding a step to the sequence.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Robotics
Arduino and Coding for Robots Study Book
The complete study notes, practice questions and glossary for Arduino and Coding for Robots
