Components and supplies
Alphanumeric LCD, 16 x 2
5 mm LED: Green
Jumper wires (generic)
Arduino UNO
5 mm LED: Red
Speaker: 0.25W, 8 ohms
Breadboard (generic)
Resistor 220 ohm
5 mm LED: Yellow
Pushbutton Switch, Pushbutton
High Brightness LED, White
Resistor 100 ohm
Tools and machines
Plier, Long Nose
Apps and platforms
Arduino IDE
Project description
Code
Code
arduino
1#include <Wire.h> 2#include <LiquidCrystal.h> 3#include<Tone.h> 4 5LiquidCrystal lcd( A0, A1, A2, A3, A4, A5); 6Tone speakerpin; 7int starttune[] = {NOTE_E4, NOTE_E4, NOTE_C4, NOTE_E4, NOTE_G4, NOTE_G4}; 8int duration2[] = {100, 200, 100, 200, 100, 400}; 9int note[] = {NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4}; 10int duration[] = {100, 100, 100, 300, 100, 300}; 11int button[] = {2, 3, 4, 5}; //The four button input pins 12int ledpin[] = {8, 9, 10, 11}; // LED pins 13int turn = 0; // turn counter 14int buttonstate = 0; // button state checker 15int randomArray[50]; //long to store up to 50 inputs 16int inputArray[50]; 17int highscore = 0; 18int MyScore; 19 20 21void setup() 22{ 23 24Serial.begin(9600); 25lcd.begin(16,2); 26 27lcd.setCursor(0,0); 28lcd.print("LCD Memory Game"); 29lcd.setCursor(0,1); 30lcd.print(" SOHAIL "); 31delay(3000); 32lcd.clear(); 33 34 lcd.setCursor(0, 1); 35 lcd.print("Your Score: 0"); 36 lcd.setCursor(0, 0); 37 lcd.print("High Score: 0"); 38 Serial.begin(9600); 39 speakerpin.begin(6); // speaker is on pin 6 40 41 for(int x=0; x<4; x++) // LED pins are outputs 42 { 43 pinMode(ledpin[x], OUTPUT); 44 } 45 46 for(int x=0; x<4; x++) 47 { 48 pinMode(button[x],INPUT); 49 digitalWrite(button[x], HIGH); 50 } 51 52 randomSeed(analogRead(0)); 53 for (int thisNote = 0; thisNote < 6; thisNote ++) { 54 55 speakerpin.play(starttune[thisNote]); 56 57 if (thisNote==0 || thisNote==2) 58 { 59 digitalWrite(ledpin[0], HIGH); 60 } 61 if (thisNote==1 || thisNote==3 ) 62 { 63 digitalWrite(ledpin[1], HIGH); 64 } 65 if (thisNote==4 || thisNote==5) 66 { 67 digitalWrite(ledpin[2], HIGH); 68 } 69 if (thisNote==6) 70 { 71 digitalWrite(ledpin[3], HIGH); 72 } 73 delay(duration2[thisNote]); 74 75 speakerpin.stop(); 76 digitalWrite(ledpin[0], LOW); 77 digitalWrite(ledpin[1], LOW); 78 digitalWrite(ledpin[2], LOW); 79 digitalWrite(ledpin[3], LOW); 80 delay(25); 81 } 82 delay(1000); 83} 84 85void loop() 86{ 87 for (int y=0; y<=2; y++) 88 { 89 90 digitalWrite(ledpin[0], HIGH); 91 digitalWrite(ledpin[1], HIGH); 92 digitalWrite(ledpin[2], HIGH); 93 digitalWrite(ledpin[3], HIGH); 94 95 for (int thisNote = 0; thisNote < 6; thisNote ++) { 96 97 speakerpin.play(note[thisNote]); 98 99 delay(duration[thisNote]); 100 101 speakerpin.stop(); 102 delay(25); 103 } 104 105 digitalWrite(ledpin[0], LOW); 106 digitalWrite(ledpin[1], LOW); 107 digitalWrite(ledpin[2], LOW); 108 digitalWrite(ledpin[3], LOW); 109 delay(1000); 110 111 for (int y=turn; y <= turn; y++) 112 { 113 Serial.println(""); 114 Serial.print("Turn: "); 115 Serial.print(y); 116 Serial.println(""); 117 lcd.clear(); 118 MyScore=y; 119 lcd.setCursor(0, 1); //(Column,Row) 120 lcd.print("Your Score: "); 121 lcd.setCursor(12, 1); 122 lcd.print(MyScore); 123 if(MyScore > highscore) { 124 highscore = y; 125 } 126 lcd.setCursor(0, 0); 127 lcd.print("High Score: " + (String)(highscore)); 128 Serial.print(y); 129 Serial.println(""); 130 delay(1000); 131 randomArray[y] = random(1, 5); 132 for (int x=0; x <= turn; x++) 133 { 134 Serial.print(randomArray[x]); 135 136 for(int y=0; y<4; y++) 137 { 138 139 if (randomArray[x] == 1 && ledpin[y] == 8) 140 { 141 digitalWrite(ledpin[y], HIGH); 142 speakerpin.play(NOTE_G3, 100); 143 delay(400); 144 digitalWrite(ledpin[y], LOW); 145 delay(100); 146 } 147 148 if (randomArray[x] == 2 && ledpin[y] == 9) 149 { 150 digitalWrite(ledpin[y], HIGH); 151 speakerpin.play(NOTE_A3, 100); 152 delay(400); 153 digitalWrite(ledpin[y], LOW); 154 delay(100); 155 } 156 157 if (randomArray[x] == 3 && ledpin[y] == 10) 158 { 159 digitalWrite(ledpin[y], HIGH); 160 speakerpin.play(NOTE_B3, 100); 161 delay(400); 162 digitalWrite(ledpin[y], LOW); 163 delay(100); 164 } 165 166 if (randomArray[x] == 4 && ledpin[y] == 11) 167 { 168 digitalWrite(ledpin[y], HIGH); 169 speakerpin.play(NOTE_C4, 100); 170 delay(400); 171 digitalWrite(ledpin[y], LOW); 172 delay(100); 173 } 174 } 175 } 176 } 177 input(); 178 179 } 180 181 182} 183 184 185 186void input() { 187 188 for (int x=0; x <= turn;) 189 { //Statement controlled by turn count 190 191 for(int y=0; y<4; y++) 192 { 193 194 buttonstate = digitalRead(button[y]); 195 196 if (buttonstate == LOW && button[y] == 2) 197 { 198 digitalWrite(ledpin[0], HIGH); 199 speakerpin.play(NOTE_G3, 100); 200 delay(200); 201 digitalWrite(ledpin[0], LOW); 202 inputArray[x] = 1; 203 delay(250); 204 Serial.print(" "); 205 Serial.print(1); 206 if (inputArray[x] != randomArray[x]) { 207 fail(); 208 } 209 x++; 210 } 211 if (buttonstate == LOW && button[y] == 3) 212 { 213 digitalWrite(ledpin[1], HIGH); 214 speakerpin.play(NOTE_A3, 100); 215 delay(200); 216 digitalWrite(ledpin[1], LOW); 217 inputArray[x] = 2; 218 delay(250); 219 Serial.print(" "); 220 Serial.print(2); 221 if (inputArray[x] != randomArray[x]) { 222 fail(); 223 } 224 x++; 225 } 226 227 if (buttonstate == LOW && button[y] == 4) 228 { 229 digitalWrite(ledpin[2], HIGH); 230 speakerpin.play(NOTE_B3, 100); 231 delay(200); 232 digitalWrite(ledpin[2], LOW); 233 inputArray[x] = 3; 234 delay(250); 235 Serial.print(" "); 236 Serial.print(3); 237 if (inputArray[x] != randomArray[x]) { 238 fail(); 239 } 240 x++; 241 } 242 243 if (buttonstate == LOW && button[y] == 5) 244 { 245 digitalWrite(ledpin[3], HIGH); 246 speakerpin.play(NOTE_C4, 100); 247 delay(200); 248 digitalWrite(ledpin[3], LOW); 249 inputArray[x] = 4; 250 delay(250); 251 Serial.print(" "); 252 Serial.print(4); 253 if (inputArray[x] != randomArray[x]) 254 { 255 fail(); 256 } 257 x++; 258 } 259 } 260 } 261 delay(500); 262 turn++; 263} 264 265 266 267void fail() { 268 269 for (int y=0; y<=3; y++) 270 { 271 272 digitalWrite(ledpin[0], HIGH); 273 digitalWrite(ledpin[1], HIGH); 274 digitalWrite(ledpin[2], HIGH); 275 digitalWrite(ledpin[3], HIGH); 276 speakerpin.play(NOTE_G2, 300); 277 delay(200); 278 digitalWrite(ledpin[0], LOW); 279 digitalWrite(ledpin[1], LOW); 280 digitalWrite(ledpin[2], LOW); 281 digitalWrite(ledpin[3], LOW); 282 speakerpin.play(NOTE_C2, 300); 283 delay(200); 284 lcd.clear(); 285 lcd.setCursor(0, 0); 286 lcd.print(" You Lose"); 287 lcd.setCursor(0, 1); 288 lcd.print(" Game Over"); 289 } 290 delay(2000); 291 lcd.clear(); 292 lcd.setCursor(0, 0); 293 lcd.print("High: " + (String) (highscore)); 294 295 296 lcd.setCursor(9, 0); 297 lcd.print("You: "); 298 lcd.setCursor(14, 0); 299 lcd.print(MyScore); 300 301 lcd.setCursor(0, 1); 302 lcd.print("<-Press to play again!"); 303 { 304 305 while (digitalRead(button[0]) > 0 &&digitalRead(button[1]) > 0 && digitalRead(button[2]) > 0 && digitalRead(button[3]) > 0 ){ 306digitalWrite(ledpin[0], HIGH); 307digitalWrite(ledpin[1], HIGH); 308digitalWrite(ledpin[2], HIGH); 309digitalWrite(ledpin[3], HIGH); 310delay(100); 311digitalWrite(ledpin[0], LOW); 312digitalWrite(ledpin[1], LOW); 313digitalWrite(ledpin[2], LOW); 314digitalWrite(ledpin[3], LOW); 315delay(100); 316 } 317 delay(1000); 318 319 turn = -1; 320} 321} 322
Code
arduino
1#include <Wire.h> 2#include <LiquidCrystal.h> 3#include<Tone.h> 4 5LiquidCrystal lcd( A0, A1, A2, A3, A4, A5); 6Tone speakerpin; 7int starttune[] = {NOTE_E4, NOTE_E4, NOTE_C4, NOTE_E4, NOTE_G4, NOTE_G4}; 8int duration2[] = {100, 200, 100, 200, 100, 400}; 9int note[] = {NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4}; 10int duration[] = {100, 100, 100, 300, 100, 300}; 11int button[] = {2, 3, 4, 5}; //The four button input pins 12int ledpin[] = {8, 9, 10, 11}; // LED pins 13int turn = 0; // turn counter 14int buttonstate = 0; // button state checker 15int randomArray[50]; //long to store up to 50 inputs 16int inputArray[50]; 17int highscore = 0; 18int MyScore; 19 20 21void setup() 22{ 23 24Serial.begin(9600); 25lcd.begin(16,2); 26 27lcd.setCursor(0,0); 28lcd.print("LCD Memory Game"); 29lcd.setCursor(0,1); 30lcd.print(" SOHAIL "); 31delay(3000); 32lcd.clear(); 33 34 lcd.setCursor(0, 1); 35 lcd.print("Your Score: 0"); 36 lcd.setCursor(0, 0); 37 lcd.print("High Score: 0"); 38 Serial.begin(9600); 39 speakerpin.begin(6); // speaker is on pin 6 40 41 for(int x=0; x<4; x++) // LED pins are outputs 42 { 43 pinMode(ledpin[x], OUTPUT); 44 } 45 46 for(int x=0; x<4; x++) 47 { 48 pinMode(button[x],INPUT); 49 digitalWrite(button[x], HIGH); 50 } 51 52 randomSeed(analogRead(0)); 53 for (int thisNote = 0; thisNote < 6; thisNote ++) { 54 55 speakerpin.play(starttune[thisNote]); 56 57 if (thisNote==0 || thisNote==2) 58 { 59 digitalWrite(ledpin[0], HIGH); 60 } 61 if (thisNote==1 || thisNote==3 ) 62 { 63 digitalWrite(ledpin[1], HIGH); 64 } 65 if (thisNote==4 || thisNote==5) 66 { 67 digitalWrite(ledpin[2], HIGH); 68 } 69 if (thisNote==6) 70 { 71 digitalWrite(ledpin[3], HIGH); 72 } 73 delay(duration2[thisNote]); 74 75 speakerpin.stop(); 76 digitalWrite(ledpin[0], LOW); 77 digitalWrite(ledpin[1], LOW); 78 digitalWrite(ledpin[2], LOW); 79 digitalWrite(ledpin[3], LOW); 80 delay(25); 81 } 82 delay(1000); 83} 84 85void loop() 86{ 87 for (int y=0; y<=2; y++) 88 { 89 90 digitalWrite(ledpin[0], HIGH); 91 digitalWrite(ledpin[1], HIGH); 92 digitalWrite(ledpin[2], HIGH); 93 digitalWrite(ledpin[3], HIGH); 94 95 for (int thisNote = 0; thisNote < 6; thisNote ++) { 96 97 speakerpin.play(note[thisNote]); 98 99 delay(duration[thisNote]); 100 101 speakerpin.stop(); 102 delay(25); 103 } 104 105 digitalWrite(ledpin[0], LOW); 106 digitalWrite(ledpin[1], LOW); 107 digitalWrite(ledpin[2], LOW); 108 digitalWrite(ledpin[3], LOW); 109 delay(1000); 110 111 for (int y=turn; y <= turn; y++) 112 { 113 Serial.println(""); 114 Serial.print("Turn: "); 115 Serial.print(y); 116 Serial.println(""); 117 lcd.clear(); 118 MyScore=y; 119 lcd.setCursor(0, 1); //(Column,Row) 120 lcd.print("Your Score: "); 121 lcd.setCursor(12, 1); 122 lcd.print(MyScore); 123 if(MyScore > highscore) { 124 highscore = y; 125 } 126 lcd.setCursor(0, 0); 127 lcd.print("High Score: " + (String)(highscore)); 128 Serial.print(y); 129 Serial.println(""); 130 delay(1000); 131 randomArray[y] = random(1, 5); 132 for (int x=0; x <= turn; x++) 133 { 134 Serial.print(randomArray[x]); 135 136 for(int y=0; y<4; y++) 137 { 138 139 if (randomArray[x] == 1 && ledpin[y] == 8) 140 { 141 digitalWrite(ledpin[y], HIGH); 142 speakerpin.play(NOTE_G3, 100); 143 delay(400); 144 digitalWrite(ledpin[y], LOW); 145 delay(100); 146 } 147 148 if (randomArray[x] == 2 && ledpin[y] == 9) 149 { 150 digitalWrite(ledpin[y], HIGH); 151 speakerpin.play(NOTE_A3, 100); 152 delay(400); 153 digitalWrite(ledpin[y], LOW); 154 delay(100); 155 } 156 157 if (randomArray[x] == 3 && ledpin[y] == 10) 158 { 159 digitalWrite(ledpin[y], HIGH); 160 speakerpin.play(NOTE_B3, 100); 161 delay(400); 162 digitalWrite(ledpin[y], LOW); 163 delay(100); 164 } 165 166 if (randomArray[x] == 4 && ledpin[y] == 11) 167 { 168 digitalWrite(ledpin[y], HIGH); 169 speakerpin.play(NOTE_C4, 100); 170 delay(400); 171 digitalWrite(ledpin[y], LOW); 172 delay(100); 173 } 174 } 175 } 176 } 177 input(); 178 179 } 180 181 182} 183 184 185 186void input() { 187 188 for (int x=0; x <= turn;) 189 { //Statement controlled by turn count 190 191 for(int y=0; y<4; y++) 192 { 193 194 buttonstate = digitalRead(button[y]); 195 196 if (buttonstate == LOW && button[y] == 2) 197 { 198 digitalWrite(ledpin[0], HIGH); 199 speakerpin.play(NOTE_G3, 100); 200 delay(200); 201 digitalWrite(ledpin[0], LOW); 202 inputArray[x] = 1; 203 delay(250); 204 Serial.print(" "); 205 Serial.print(1); 206 if (inputArray[x] != randomArray[x]) { 207 fail(); 208 } 209 x++; 210 } 211 if (buttonstate == LOW && button[y] == 3) 212 { 213 digitalWrite(ledpin[1], HIGH); 214 speakerpin.play(NOTE_A3, 100); 215 delay(200); 216 digitalWrite(ledpin[1], LOW); 217 inputArray[x] = 2; 218 delay(250); 219 Serial.print(" "); 220 Serial.print(2); 221 if (inputArray[x] != randomArray[x]) { 222 fail(); 223 } 224 x++; 225 } 226 227 if (buttonstate == LOW && button[y] == 4) 228 { 229 digitalWrite(ledpin[2], HIGH); 230 speakerpin.play(NOTE_B3, 100); 231 delay(200); 232 digitalWrite(ledpin[2], LOW); 233 inputArray[x] = 3; 234 delay(250); 235 Serial.print(" "); 236 Serial.print(3); 237 if (inputArray[x] != randomArray[x]) { 238 fail(); 239 } 240 x++; 241 } 242 243 if (buttonstate == LOW && button[y] == 5) 244 { 245 digitalWrite(ledpin[3], HIGH); 246 speakerpin.play(NOTE_C4, 100); 247 delay(200); 248 digitalWrite(ledpin[3], LOW); 249 inputArray[x] = 4; 250 delay(250); 251 Serial.print(" "); 252 Serial.print(4); 253 if (inputArray[x] != randomArray[x]) 254 { 255 fail(); 256 } 257 x++; 258 } 259 } 260 } 261 delay(500); 262 turn++; 263} 264 265 266 267void fail() { 268 269 for (int y=0; y<=3; y++) 270 { 271 272 digitalWrite(ledpin[0], HIGH); 273 digitalWrite(ledpin[1], HIGH); 274 digitalWrite(ledpin[2], HIGH); 275 digitalWrite(ledpin[3], HIGH); 276 speakerpin.play(NOTE_G2, 300); 277 delay(200); 278 digitalWrite(ledpin[0], LOW); 279 digitalWrite(ledpin[1], LOW); 280 digitalWrite(ledpin[2], LOW); 281 digitalWrite(ledpin[3], LOW); 282 speakerpin.play(NOTE_C2, 300); 283 delay(200); 284 lcd.clear(); 285 lcd.setCursor(0, 0); 286 lcd.print(" You Lose"); 287 lcd.setCursor(0, 1); 288 lcd.print(" Game Over"); 289 } 290 delay(2000); 291 lcd.clear(); 292 lcd.setCursor(0, 0); 293 lcd.print("High: " + (String) (highscore)); 294 295 296 lcd.setCursor(9, 0); 297 lcd.print("You: "); 298 lcd.setCursor(14, 0); 299 lcd.print(MyScore); 300 301 lcd.setCursor(0, 1); 302 lcd.print("<-Press to play again!"); 303 { 304 305 while (digitalRead(button[0]) > 0 &&digitalRead(button[1]) > 0 && digitalRead(button[2]) > 0 && digitalRead(button[3]) > 0 ){ 306digitalWrite(ledpin[0], HIGH); 307digitalWrite(ledpin[1], HIGH); 308digitalWrite(ledpin[2], HIGH); 309digitalWrite(ledpin[3], HIGH); 310delay(100); 311digitalWrite(ledpin[0], LOW); 312digitalWrite(ledpin[1], LOW); 313digitalWrite(ledpin[2], LOW); 314digitalWrite(ledpin[3], LOW); 315delay(100); 316 } 317 delay(1000); 318 319 turn = -1; 320} 321} 322
Downloadable files
Circuit
Circuit
Comments
Only logged in users can leave comments