Components and supplies
Arduino UNO
BreadBoard (Full size)
Mini speaker
Assorted LEDs
Jumper wires (generic)
330 ohm resistors
10k ohm resistors
Apps and platforms
Arduino IDE
Project description
Code
Memory Game Code
arduino
This is the entire code for the game. Simply copy and paste it in your favorite IDE and upload it to the Arduino.
1#define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s 2 3byte sequence[100]; // Storage for the light sequence 4byte curLen = 0; // Current length of the sequence 5byte inputCount = 0; // The number of times that the player has pressed a (correct) button in a given turn 6byte lastInput = 0; // Last input from the player 7byte expRd = 0; // The LED that's suppose to be lit by the player 8bool btnDwn = false; // Used to check if a button is pressed 9bool wait = false; // Is the program waiting for the user to press a button 10bool resetFlag = false; // Used to indicate to the program that once the player lost 11 12byte soundPin = 5; // Speaker output 13 14byte noPins = 4; // Number of buttons/LEDs (While working on this, I was using only 2 LEDs) 15 // You could make the game harder by adding an additional LED/button/resistors combination. 16byte pins[] = {2, 13, 10, 8}; // Button input pins and LED ouput pins - change these vaules if you wwant to connect yourbuttons to other pins 17 // The number of elements must match noPins below 18 19long inputTime = 0; // Timer variable for the delay between user inputs 20 21void setup() { 22 delay(3000); // This is to give me time to breathe after connection the arduino - can be removed if you want 23 Serial.begin(9600); // Start Serial monitor. This can be removed too as long as you remove all references to Serial below 24 Reset(); 25} 26 27/// 28/// Sets all the pins as either INPUT or OUTPUT based on the value of 'dir' 29/// 30void setPinDirection(byte dir){ 31 for(byte i = 0; i < noPins; i++){ 32 pinMode(pins[i], dir); 33 } 34} 35 36//send the same value to all the LED pins 37void writeAllPins(byte val){ 38 for(byte i = 0; i < noPins; i++){ 39 digitalWrite(pins[i], val); 40 } 41} 42 43//Makes a (very annoying :) beep sound 44void beep(byte freq){ 45 analogWrite(soundPin, 2); 46 delay(freq); 47 analogWrite(soundPin, 0); 48 delay(freq); 49} 50 51/// 52/// Flashes all the LEDs together 53/// freq is the blink speed - small number -> fast | big number -> slow 54/// 55void flash(short freq){ 56 setPinDirection(OUTPUT); /// We're activating the LEDS now 57 for(int i = 0; i < 5; i++){ 58 writeAllPins(HIGH); 59 beep(50); 60 delay(freq); 61 writeAllPins(LOW); 62 delay(freq); 63 } 64} 65 66/// 67///This function resets all the game variables to their default values 68/// 69void Reset(){ 70 flash(500); 71 curLen = 0; 72 inputCount = 0; 73 lastInput = 0; 74 expRd = 0; 75 btnDwn = false; 76 wait = false; 77 resetFlag = false; 78} 79 80/// 81/// User lost 82/// 83void Lose(){ 84 flash(50); 85} 86 87/// 88/// The arduino shows the user what must be memorized 89/// Also called after losing to show you what you last sequence was 90/// 91void playSequence(){ 92 //Loop through the stored sequence and light the appropriate LEDs in turn 93 for(int i = 0; i < curLen; i++){ 94 Serial.print("Seq: "); 95 Serial.print(i); 96 Serial.print("Pin: "); 97 Serial.println(sequence[i]); 98 digitalWrite(sequence[i], HIGH); 99 delay(500); 100 digitalWrite(sequence[i], LOW); 101 delay(250); 102 } 103} 104 105/// 106/// The events that occur upon a loss 107/// 108void DoLoseProcess(){ 109 Lose(); // Flash all the LEDS quickly (see Lose function) 110 delay(1000); 111 playSequence(); // Shows the user the last sequence - So you can count remember your best score - Mine's 22 by the way :) 112 delay(1000); 113 Reset(); // Reset everything for a new game 114} 115 116/// 117/// Where the magic happens 118/// 119void loop() { 120 if(!wait){ 121 //****************// 122 // Arduino's turn // 123 //****************// 124 setPinDirection(OUTPUT); // We're using the LEDs 125 126 randomSeed(analogRead(A0)); // https://www.arduino.cc/en/Reference/RandomSeed 127 sequence[curLen] = pins[random(0,noPins)]; // Put a new random value in the next position in the sequence - https://www.arduino.cc/en/Reference/random 128 curLen++; // Set the new Current length of the sequence 129 130 playSequence(); // Show the sequence to the player 131 beep(50); // Make a beep for the player to be aware 132 133 wait = true; // Set Wait to true as it's now going to be the turn of the player 134 inputTime = millis(); // Store the time to measure the player's response time 135 }else{ 136 //***************// 137 // Player's turn // 138 //***************// 139 setPinDirection(INPUT); // We're using the buttons 140 141 if(millis() - inputTime > PLAYER_WAIT_TIME){ // If the player takes more than the allowed time, 142 DoLoseProcess(); // All is lost :( 143 return; 144 } 145 146 if(!btnDwn){ // 147 expRd = sequence[inputCount]; // Find the value we expect from the player 148 Serial.print("Expected: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above 149 Serial.println(expRd); // Serial Monitor Output - Should be removed if you removed the Serial.begin above 150 151 for(int i = 0; i < noPins; i++){ // Loop through the all the pins 152 if(pins[i]==expRd) 153 continue; // Ignore the correct pin 154 if(digitalRead(pins[i]) == HIGH){ // Is the buttong pressed 155 lastInput = pins[i]; 156 resetFlag = true; // Set the resetFlag - this means you lost 157 btnDwn = true; // This will prevent the program from doing the same thing over and over again 158 Serial.print("Read: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above 159 Serial.println(lastInput); // Serial Monitor Output - Should be removed if you removed the Serial.begin above 160 } 161 } 162 } 163 164 if(digitalRead(expRd) == 1 && !btnDwn) // The player pressed the right button 165 { 166 inputTime = millis(); // 167 lastInput = expRd; 168 inputCount++; // The user pressed a (correct) button again 169 btnDwn = true; // This will prevent the program from doing the same thing over and over again 170 Serial.print("Read: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above 171 Serial.println(lastInput); // Serial Monitor Output - Should be removed if you removed the Serial.begin above 172 }else{ 173 if(btnDwn && digitalRead(lastInput) == LOW){ // Check if the player released the button 174 btnDwn = false; 175 delay(20); 176 if(resetFlag){ // If this was set to true up above, you lost 177 DoLoseProcess(); // So we do the losing sequence of events 178 } 179 else{ 180 if(inputCount == curLen){ // Has the player finished repeating the sequence 181 wait = false; // If so, this will make the next turn the program's turn 182 inputCount = 0; // Reset the number of times that the player has pressed a button 183 delay(1500); 184 } 185 } 186 } 187 } 188 } 189} 190
Downloadable files
Diagram
Diagram
Fritzing Diagram
Fritzing Diagram
Diagram
Diagram
Fritzing Diagram
Fritzing Diagram
Comments
Only logged in users can leave comments