Remember the colours – a game
The following article is based on this entry on Newton’s blog (in Polish). All the pictures, program code and the video clip came from there.
This project’s goal was to use as many parts from the Arduino Starter Kit, as possible. So, what is this project exactly? As the title itself suggests, it’s a game, whose object is to remember a colour sequence and recreate it. At the very beginning the sequence consists of 3 colours only, hovever, the difficulty level increases – an extra colour is added each time. Every colour has a different tone assigned. This tone is emitted from a buzzer when the corresponding colour is displayed, so the game involves two senses, both sight and hearing. User interface consists of: RGB LED diode displaying the colours, 8 red LED diodes arranged in a row to display which colour in turn we are setting and two microswitch type buttons. By pushing both in the same time we are starting the game, the left button chooses the colour, the right – accepts. There are two more LED diodes, indicating whether we have recreated the sequence properly (left LED), or not (right LED).
Whole device mounted on a breadboard looks like the following:
You can see (and hear) how it works on this video clip. Hereunder is the circuit schematic made in Fritzing:
All the resistors used are 220Ω and the IC in the centre of the breadboard is 74HC595 shift register, which allows to drive eight LEDs using only three Arduino digital pins. This was described in “Shifted LEDs” article.
When everything is connected, we should take care of the code. At the beginning we must declare where we have connected what (to which input/output). The following code is responsible of it:
int latchPin = 8, clockPin = 12, dataPin = 3; //shift register byte data = 0; //register data int led8 = 7, led9 = 6; //additional LEDs int ledR = 11, ledG = 10, ledB = 9; //LED RGB int buttonLeft = 5, buttonRight = 4; //buttons int speakerPin = 13; //buzzer
Here comes the initialization part – setting pin as input/output, turning internal pull-up resistors on and so on:
void setup() { Serial.begin(57600); for(int i=3;i<=13;i++) // setting all the pins pinMode(i, OUTPUT); // from 3 to 13 as outputs pinMode(buttonLeft, INPUT); // setting the button pins pinMode(buttonRight, INPUT); // as inputs digitalWrite(buttonLeft, HIGH); // turning internal pull-up digitalWrite(buttonRight, HIGH); // resistors on randomSeed(analogRead(0)); // initializing the pseudorandom number generator // with the value read from analog input no. 0 // - each time it's different clearLeds(); }
If you are wondering what stands for such a strange notation:
a ? b : c
here is the explanation.
The main loop of our program looks like the following:
void loop() { effect(); clearLeds(); if(waitForButton() == 3) return; for(int j=3;j<=8;j++) if(!playLevel(7, j)) return; }
Consecutively: at the beginning, effect() function turns on visual and light effects, clearLeds turns all the LEDs off. After that, unless the user pushes both buttons at the same time, what will cause a reset, playLevel function starts successive levels of the game, increasing the second parameter (number of the lights to remember) by 1. The first parameter denotes the total number of colours (from 0 to 7). Of course, we can lower it, but then the game will become too easy. It’s better to add another colours using setColor function (changing parameters in setRGB) and upgrade it. Then, we can put the total number of colours (remember – the numeration starts from 0!) as the first parameter of playLevel function to make the game more difficult and hence – more addicting ;).
The whole program code with comments is available here. I think it’s clear enough and doesn’t require any additional explanations.