Components and supplies
Standard LCD - 16x2 White on Blue
Arduino Nano R3
Any Remote Control
Jumper wires (generic)
IR receiver (generic)
Apps and platforms
Arduino IDE
Project description
Code
Code for Arduino IDE
arduino
For the code you need the libraries for the IR sensor and the LCD display. For the IR sensor you also need the Wire.h library. Just copy the code, paste it to the Arduino IDE and upload it to the board.
1/* 2 Fast IR Game by mgbig V1.0 3 */ 4#include <IRremote.h> 5#include <Wire.h> 6#include <LiquidCrystal_I2C.h> 7 8char up = 3; 9char down = 1; 10char ok = 2; 11 12// Define 3 characters for the LCD Display 13byte up1[8] = { 14 B00100, 15 B01110, 16 B10101, 17 B00100, 18 B00100, 19 B00100, 20 B00100, 21 B00100 22}; 23 24byte down1[8] = { 25 B00100, 26 B00100, 27 B00100, 28 B00100, 29 B00100, 30 B10101, 31 B01110, 32 B00100 33}; 34 35byte ok1[8] = { 36 B01110, 37 B01010, 38 B01110, 39 B10000, 40 B10100, 41 B11000, 42 B10100, 43 B10010 44}; 45 46int RECV_PIN = 11; // IR Sensor on Pin 11 47 48char* howtoplay[23] = { 49 "> FAST IR GAME <", 50 " Press key ", 51 "You have to", // if you don't want this howToPlay you can delete from here ... 52 "press the key,", 53 "that you see on", 54 "the screen, when", 55 "it is beetween", 56 "the fences (#).", 57 "It ist getting", 58 "faster and there", 59 "will be symbols", 60 "that you do not", 61 "have to hit!", 62 "Try to get to", 63 "Level 10 and win", 64 "the game. Before", 65 "you start, you", 66 "have to define", 67 "the keys on your", 68 "remote control", 69 "to play the game", // ... until here. Leave the last lines for propper work! 70 " Have fun !!", 71 "" 72}; 73 74String keyNames[] = {"up", "down", "right", "left", "ok", "+", "-", "#", "*"}; 75String keySymb[] = {"", "", "", "", "", "", "", "", ""}; 76long keys[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 77int keycount = 7; 78 79int lev; 80int xpos = 1; 81int xadd = 1; 82int xleft; 83int xright; 84int xstart = 0; 85int xend = 15; 86 87int actSym = 0; 88int score; // All together score 89int scorePerLev; // Symbols in actual level 90int scoreNextLev = 50; // Symbols until next level 91 92int gameState = -1; 93 94bool pressed = false; 95 96IRrecv irrecv(RECV_PIN); 97decode_results results; 98 99LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display 100 101void setup() 102{ 103 Serial.begin(9600); 104 lcd.init(); // initialize the lcd 105 lcd.backlight(); 106 lcd.createChar(up, up1); 107 lcd.createChar(down, down1); 108 lcd.createChar(ok, ok1); 109 keySymb[0] = "\\3"; 110 keySymb[1] = "\\1"; 111 keySymb[2] = "\\176"; 112 keySymb[3] = "\\177"; 113 keySymb[4] = "\\2"; 114 keySymb[5] = "+"; 115 keySymb[6] = "-"; 116 keySymb[7] = "#"; 117 keySymb[8] = "*"; 118 119 irrecv.enableIRIn(); // Start the receiver 120 121 info(); //howtoplay show once at the start, can be removed when you know how to play 122 randomSeed(analogRead(1)); 123} 124 125 126void loop() 127{ 128 129 // define keys only once 130 // gameState= -1 131 // ################################ 132 if (gameState == -1) { 133 lcd.clear(); 134 lcd.setCursor(0, 0); 135 lcd.print("Define Keys to"); 136 lcd.setCursor(0, 1); 137 lcd.print("play the game..."); 138 delay(3000); 139 pressed = false; 140 for (int i = 0; i < keycount; i++) { // 0 und 5 141 pressed = false; 142 lcd.clear(); 143 lcd.setCursor(0, 0); 144 lcd.print("Key for:"); 145 lcd.print(keyNames[i]); 146 while (pressed != true) { 147 if (irrecv.decode(&results)) { 148 keys[i] = results.value; 149 lcd.setCursor(0, 1); 150 lcd.print(" ok!"); 151 Serial.println(keys[i]); 152 delay(500); 153 irrecv.resume(); // Receive the next value 154 pressed = true; 155 } 156 } 157 pressed = false; 158 lcd.clear(); 159 lcd.setCursor(0, 0); 160 lcd.print("Repaet key!"); 161 lcd.print(keyNames[i]); 162 irrecv.resume(); 163 while (pressed != true) { 164 if (irrecv.decode(&results)) { 165 if (keys[i] == results.value) { 166 lcd.setCursor(0, 1); 167 lcd.print("is the same!"); 168 delay(500); 169 pressed = true; 170 } else { 171 lcd.setCursor(0, 1); 172 lcd.print("wrong!"); 173 delay(500); 174 lcd.setCursor(0, 1); 175 lcd.print(" "); 176 } 177 irrecv.resume(); 178 } 179 } 180 } 181 gameState = 0; // ok to select level 182 lcd.clear(); 183 lcd.setCursor(0, 0); 184 lcd.print("Keys done!"); 185 delay(2000); 186 } 187 188 // Select Level 189 // gameState=0 190 //########################### 191 if (gameState == 0) { 192 lev = 1; 193 lcd.clear(); 194 lcd.setCursor(0, 0); 195 lcd.print("Select Level+ok"); 196 lcd.setCursor(0, 1); 197 lcd.print("Level: "); 198 lcd.print(lev); 199 irrecv.resume(); 200 pressed = false; 201 Serial.println("Level"); 202 Serial.println(pressed); 203 while (pressed != true) { 204 if (irrecv.decode(&results)) { 205 Serial.println(results.value); 206 if (results.value == keys[0]) lev++; 207 if (results.value == keys[1]) lev--; 208 if (results.value == keys[4]) pressed = true; 209 if (lev < 1) lev = 1; 210 if (lev > 10) lev = 10; 211 lcd.setCursor(7, 1); 212 lcd.print(lev); 213 lcd.print(" "); 214 irrecv.resume(); 215 } 216 delay(250); 217 } 218 lcd.setCursor(0, 0); 219 lcd.print("Ok! Play in "); 220 delay(2000); 221 lcd.clear(); 222 gameState = 1; // Main gameplay 223 score = 0; 224 scorePerLev = 0; 225 keycount = 7; 226 xleft = 4; 227 xright = 11; 228 drawField(""); 229 irrecv.resume(); 230 Serial.println("Level Set"); 231 } 232 233 // Main Game 234 // gameState=1 235 //############################### 236 if (gameState == 1) { 237 xpos = 0; 238 xadd = 1; 239 int k = 0; 240 bool rightkey = false; 241 pressed = false; 242 actSym = floor(random(0, keycount)); 243 while (pressed != true) { 244 Serial.println(xpos); 245 if (irrecv.decode(&results)) { 246 for (int i = 0; i < keycount; i++) { 247 if (results.value == keys[i]) { 248 rightkey = true; 249 k = i; 250 } 251 } 252 if (rightkey == true) { 253 scorePerLev++; 254 if (xpos <= xleft || xpos >= xright) { 255 score = score - (4 + lev); 256 // negativ sound 257 } 258 if (actSym == k) { 259 lcd.setCursor(xpos, 1); 260 lcd.print(" "); 261 score++; 262 drawField(""); 263 changeDirection(); 264 } else { 265 score = score - (2 + lev); 266 drawField(" :( "); 267 // negativ sound 268 } 269 actSym = floor(random(0, keycount)); 270 rightkey = false; 271 } 272 delay(10); 273 irrecv.resume(); 274 if (scorePerLev == scoreNextLev) { 275 scorePerLev = 0; 276 lev++; 277 drawField(""); 278 if (lev < 11) { 279 lcd.setCursor(0, 1); 280 lcd.print("Next level!"); 281 waitForOK(); 282 // Check for score and display message here later 283 lcd.setCursor(0, 1); 284 lcd.print(" "); 285 } else { 286 gameState = 5; 287 pressed = true; 288 } 289 } 290 } 291 lcd.setCursor(xpos, 1); 292 lcd.print(" "); 293 xpos = xpos + xadd; 294 if (xpos == xend + 1 || xpos == xstart - 1) { 295 if (actSym < 7) { 296 score = score - (2 * (lev + 5)); 297 drawField(" :( "); 298 } else { 299 drawField(""); 300 } 301 changeDirection(); 302 actSym = floor(random(0, keycount)); 303 // negativ sound 304 } 305 lcd.setCursor(xpos, 1); 306 lcd.print(keySymb[actSym]); 307 delay(200 - (lev * 10)); 308 if (score < 0) { 309 gameState = 9; 310 pressed = true; 311 } 312 } // Main Game loop End 313 314 } 315 316 // Win 317 // ################## 318 if (gameState == 5) { 319 // positiv sound 320 lcd.setCursor(0, 1); 321 lcd.print("You win the Game"); 322 lcd.setCursor(0, 0); 323 lcd.print("Bravo! "); 324 waitForOK(); 325 gameState = 0; 326 } 327 328 // Game Over 329 // ################## 330 if (gameState == 9) { 331 // negativ sound 332 for (int i = 0; i < 5; i++) { 333 lcd.setCursor(0, 1); 334 lcd.print(" "); 335 delay(200); 336 lcd.setCursor(0, 1); 337 lcd.print(" Game over! "); 338 delay(300); 339 } 340 waitForOK(); 341 gameState = 0; 342 } 343 344} 345 346void info () { 347 int i = 0; 348 while (howtoplay[i] != "") { 349 lcd.clear(); 350 lcd.setCursor(0, 0); 351 lcd.print(howtoplay[i]); 352 lcd.setCursor(0, 1); 353 lcd.print(howtoplay[i + 1]); 354 delay(300); 355 waitForKey(); 356 i++; 357 } 358 irrecv.resume(); 359} 360 361void drawField(String empty) { 362 Serial.println("drawField"); 363 int SCur; 364 if (empty == "") empty = " "; 365 lcd.setCursor(0, 0); 366 lcd.print("################"); 367 if (lev > 3) { 368 xleft = floor(random(4, 7)); 369 xright = xleft + 7; 370 if (lev > 6) xright = xleft + 6; 371 if (lev > 9) xright = xleft + 5; 372 } 373 if (lev > 4) keycount = 8; 374 if (lev > 6) keycount = 9; 375 lcd.setCursor(xleft + 1, 0); 376 lcd.print(empty.substring(0, xright - xleft - 1)); 377 lcd.setCursor(0, 0); 378 lcd.print("L"); 379 lcd.print(lev); 380 if (score < 1000) SCur = 13; 381 if (score < 100) SCur = 14; 382 if (score < 10) SCur = 15; 383 if (score < 0) SCur = 14; 384 lcd.setCursor(SCur, 0); 385 lcd.print(score); 386} 387 388void changeDirection() { 389 xpos = xstart; 390 xadd = 1; 391 if (lev > 3) { 392 int dir = floor(random(0, 2)); 393 if (dir == 1) { 394 xpos = xend; 395 xadd = -1; 396 } 397 } 398} 399 400void waitForKey () { 401 bool press = false; 402 irrecv.resume(); 403 while (press == false) { 404 if (irrecv.decode(&results)) { 405 if (results.value != 0) press = true; 406 irrecv.resume(); 407 delay(200); 408 } 409 } 410} 411 412void waitForOK() { 413 delay(1000); 414 bool press = false; 415 irrecv.resume(); 416 while (press == false) { 417 if (irrecv.decode(&results)) { 418 if (results.value == keys[4]) press = true; 419 irrecv.resume(); 420 delay(200); 421 } 422 } 423}
Downloadable files
Sheamtic for Fast IR game
I did not find an LCD with I2C board (LCD 1602 + I2C HD44780 Modul). So I just made a shematic. My IR Diode (VS1838B LIRC) is connected like in this sheme, but make shure you make the right connection!! (Data: orange, GND: black, VCC: red). You can use an Arduino Nano or Uno.
Sheamtic for Fast IR game
Sheamtic for Fast IR game
I did not find an LCD with I2C board (LCD 1602 + I2C HD44780 Modul). So I just made a shematic. My IR Diode (VS1838B LIRC) is connected like in this sheme, but make shure you make the right connection!! (Data: orange, GND: black, VCC: red). You can use an Arduino Nano or Uno.
Sheamtic for Fast IR game
Comments
Only logged in users can leave comments
mgbig
0 Followers
•0 Projects
Table of contents
Intro
7
0