Introduction: Arduino - Memory Game
A simpel memory-game using the Arduino Uno.
Step 1: Step 1: Parts and Tools
4 Buttons
4 LED's
1 74HC595 Shift Register
1 Piezo
2 (maybe one bigger) Breadboard
1 Arduino Uno
1 220 Ω Res.
Step 2: Step 2: Tips
Try to put the arduino and the breadboard(s) together early on. It's much easier to work with.
I used a bit of catron that I glued the breadboards on and used two scews and screw-nuts to attach the arduino.
When you are transporting your project (maybe in a bad), make sure that the arduino is well protected. The cords can easily be messed up.
Step 3: Step 3: the Code
//Pin connected to 12(ST_CP) of 74HC595 Shift Register
int latchPin = 8; //clockPin connected to 11(SH_CP) on 74HC595 Shift Register int clockPin = 12; ////Pin connected to DS of 74HC595 Shift Register int dataPin = 11;
// Variabel if the game is over boolean gameover=false;
// Array for shiftregister int ledarray [8] = { 1,2,4,8,16,32,64,128}; // Variabel for current level int level=4; // Reserved array of the correct LEDs on each level int randarray1[8]; // Number of LEDs available int min=0; int max=4; // Variabel for wich one of the button pressed int buttpr; // Variabel for all LED pins int buttonpins[8]={ 2,3,4,5,6,7,A0,A1};
void setup() { // Ställ in pinnarna till shift registret pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT);
// Set all LEDs in PULLUP mode for (int buttpin = 0; buttpin < 8; buttpin++){ pinMode(buttonpins[buttpin] , INPUT_PULLUP); } // Arduino ready for seriell comunication Serial.begin(9600); }
void loop() { // Raise game level if (level<8){ level++; } // Do the level as long as there's no Gameover if(!gameover){ // Generate new LED combination for the level for (int i = 0; i< level; i++) { randarray1[i]=random(max); } // Loop to light all LEDs on the current level for (int numberToDisplay = 0; numberToDisplay < level; numberToDisplay++) { Serial.println(ledarray[randarray1[numberToDisplay]]); // Shut down power to the shiftregister readingPin before we choose new pin digitalWrite(latchPin, LOW); // Mark next LED with the shiftregister shiftOut(dataPin, clockPin, MSBFIRST, ledarray[randarray1[numberToDisplay]]); // Light the marked LED in a half of a second digitalWrite(latchPin, HIGH); delay(500); // Kill the light again digitalWrite(latchPin, LOW); // Mark the first LED shiftOut(dataPin, clockPin, MSBFIRST, 0); // Turn on the first LED digitalWrite(latchPin, HIGH); // Paus one second before next LED delay(1000); }
// Loop to read buttonpressed for(int lednr = 0; lednr < level; lednr++){ // Write out wich one is the next correct LED Serial.print("Next led: "); Serial.println(lednr); // Wait one second before next read delay(1000); // Read the buttons until one is pressed do{ buttpr=whichbuttonpressed(); } while(buttpr==9); // Write out the current pressed button and wich button that's are correct Serial.print(buttpr); Serial.println(randarray1[lednr]); // Play sound for buttonpressed tone(13,660,200); // If its the wrong button, its Gameover if(buttpr!=randarray1[lednr]){ gameover=true; // Play Gameover sound tone(13,440,1000); Serial.println("Gameover"); // Cancel the loop for reading of buttons break; } } Serial.println("Level ended"); } }
int whichbuttonpressed(){ // Repeat over all buttons and read for (int buttpin = 0; buttpin < 8; buttpin++){ // If the button is pressed, return the value of the button if (digitalRead(buttonpins[buttpin])==0){ return(buttpin); break; } } // If no button is pressed, return 9 to continue to read the buttons in the loop return 9; }
Step 4: Step 4: Function of the Game
This little game is pertty easy to make. I'm sure that you enthusiasts will be done with this in probably under an hour.
But the game is actually pretty funny and it's very usefull game in brain-gymnastics.
The four LED's in the game is flashing in a random combination. When the combination ends, you press it in order using the four buttons. Everytime you press a button, you can hear a sound. Pressing the wrong button and you get a clearly sound that indicates that it is GAME OVER! But if you manage to press the right combination, you get to a new level. This time the combination is five flashes. Next level six, and so on.
I personally think this kind of games is usefull to elder people. Maybe people with a illness that are affecting their memory?