Components and supplies
3x4 matrix keypad
Standard LCD - 16x2 White on Blue
Arduino UNO
Tools and machines
Soldering iron (generic)
Project description
Code
bomb_project.ino
c_cpp
1//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2// Prop bomb for paintball/airsoft objective game, bomb uses a 12 armcode keypad, a LCD screen, 3 LED's and a speaker. 3// Code reads chip input voltage (after the regulator) and if voltage is above minimum required to run it starts the bomb code. 4// If voltage is low it will tell you to replace the battery and stays on that screen. 5// User sets the disarm code (4 digits). 6// Then user sets timer ( HH:MM:SS ) 7// You have 3 tries to get the code entered coorectly, 1st wrong attempt cuts the time in half, 8// 2nd wrong attempt cuts time in half again and speeds time up by a multiple 10, 9// The 3rd wrong attempt detonates the bomb. 10// Must press * to enter the disarm code. 11// If you mess up entering your disarm code press "#" to reset your try. 12//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 13#include <Keypad.h> 14#include <LiquidCrystal.h> 15 16int Hours = 00; 17int Minutes = 00; 18int Seconds = 00; 19int trycount = 0; 20int keycount = 0; 21int i = 0; 22 23int redled = A4; 24int yellowled = A5; 25int greenled = A3; 26 27int hourstenscode; 28int hoursonescode; 29int mintenscode; 30int minonescode; 31int sectenscode; 32int seconescode; 33 34long secMillis = 0; 35long interval = 1000; 36 37char password[4]; 38char entered[4]; 39 40int readVcc() { 41 // Read 1.1V reference against AVcc 42 // set the reference to Vcc and the measurement to the internal 1.1V reference 43#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 44 ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 45#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) 46 ADMUX = _BV(MUX5) | _BV(MUX0) ; 47#else 48 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 49#endif 50 51 delay(2); // Wait for Vref to settle 52 ADCSRA |= _BV(ADSC); // Start conversion 53 while (bit_is_set(ADCSRA, ADSC)); // measuring 54 55 uint8_t low = ADCL; // must read ADCL first - it then locks ADCH 56 uint8_t high = ADCH; // unlocks both 57 58 long result = (high << 8) | low; 59 60 result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 61 return result; // Vcc in millivolts 62} 63 64LiquidCrystal lcd(8, 9, 10, 11, 12, 13); // the pins we use on the LCD 65// pin 8 to RS, pin 9 to E, pin 10 to D4, pin 11 to D5, pin 12 to D6, pin 13 to D7. 66// Ground to pins VSS,RW,K. 67//+5vdc to pin VDD. 68//220 ohm resistor between pins A and VDD. 69//Potentiometer center pin to V0, positive pin to VDD, Negative pin to VSS. 70 71const byte ROWS = 4; 72const byte COLS = 3; 73char keys[ROWS][COLS] = { 74 {'1', '2', '3'}, 75 {'4', '5', '6'}, 76 {'7', '8', '9'}, 77 {'*', '0', '#'} 78}; 79byte rowPins[ROWS] = {5, 4, 3, 2}; 80byte colPins[COLS] = {A0, A1, A2}; 81 82Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 83 84void setup() { 85 pinMode(redled, OUTPUT); 86 pinMode(yellowled, OUTPUT); 87 pinMode(greenled, OUTPUT); 88 lcd.begin(16, 2); 89 Serial.begin(9600); 90 91 readVcc(); 92 if (readVcc() >= 3800) 93 { 94 lcd.setCursor(0, 0); 95 lcd.print("voltage is:"); 96 lcd.print(readVcc() / 1000.00); 97 lcd.print("v"); 98 lcd.setCursor(0, 1); 99 lcd.print("voltage good!"); 100 delay(3000); 101 } 102 103 if (readVcc() <= 3800) 104 { 105 lcd.setCursor(0, 0); 106 lcd.print("voltage is:"); 107 lcd.print(readVcc() / 1000.00); 108 lcd.print("v"); 109 lcd.setCursor(0, 1); 110 lcd.print("replace battery!"); 111 delay(1000000); 112 } 113 114 lcd.clear(); 115 lcd.setCursor(0, 0); 116 lcd.print("Bomb activated!"); 117 lcd.setCursor(0, 1); 118 lcd.print("Enter Code:"); 119 while (keycount < 4) 120 { 121 lcd.setCursor(keycount + 12, 1); 122 lcd.blink(); 123 char armcode = keypad.getKey(); 124 armcode == NO_KEY; 125 if (armcode != NO_KEY) 126 { 127 if ((armcode != '*') && (armcode != '#')) 128 { 129 lcd.print(armcode); 130 tone(7, 5000, 100); 131 password[keycount] = armcode; 132 keycount++; 133 } 134 } 135 } 136 137 if (keycount == 4) 138 { 139 delay(500); 140 lcd.noBlink(); 141 lcd.clear(); 142 lcd.home(); 143 lcd.print("Disarm Code is: "); 144 lcd.setCursor(6, 1); 145 lcd.print(password[0]); 146 lcd.print(password[1]); 147 lcd.print(password[2]); 148 lcd.print(password[3]); 149 delay(3000); 150 lcd.clear(); 151 } 152 lcd.setCursor(0, 0); 153 lcd.print("Timer: HH:MM:SS"); 154 lcd.setCursor(0, 1); 155 lcd.print("SET: : :"); 156 keycount = 5; 157 158 while (keycount == 5) 159 { 160 char hourstens = keypad.getKey(); 161 lcd.setCursor(5, 1); 162 lcd.blink(); 163 if (hourstens >= '0' && hourstens <= '9') 164 { 165 hourstenscode = hourstens - '0'; 166 tone(7, 5000, 100); 167 lcd.print(hourstens); 168 keycount++; 169 } 170 } 171 172 while (keycount == 6) 173 { 174 char hoursones = keypad.getKey(); 175 lcd.setCursor(6, 1); 176 lcd.blink(); 177 if (hoursones >= '0' && hoursones <= '9') 178 { 179 hoursonescode = hoursones - '0'; 180 tone(7, 5000, 100); 181 lcd.print(hoursones); 182 keycount++; 183 } 184 } 185 186 while (keycount == 7) 187 { 188 char mintens = keypad.getKey(); 189 lcd.setCursor(8, 1); 190 lcd.blink(); 191 if (mintens >= '0' && mintens <= '9') 192 { 193 mintenscode = mintens - '0'; 194 tone(7, 5000, 100); 195 lcd.print(mintens); 196 keycount++; 197 } 198 } 199 200 while (keycount == 8) 201 { 202 char minones = keypad.getKey(); 203 lcd.setCursor(9, 1); 204 lcd.blink(); 205 if (minones >= '0' && minones <= '9') 206 { 207 minonescode = minones - '0'; 208 tone(7, 5000, 100); 209 lcd.print(minones); 210 keycount++; 211 } 212 } 213 214 while (keycount == 9) 215 { 216 char sectens = keypad.getKey(); 217 lcd.setCursor(11, 1); 218 lcd.blink(); 219 if (sectens >= '0' && sectens <= '9') 220 { 221 sectenscode = sectens - '0'; 222 tone(7, 5000, 100); 223 lcd.print(sectens); 224 keycount = 10; 225 } 226 } 227 228 while (keycount == 10) 229 { 230 char secones = keypad.getKey(); 231 lcd.setCursor(12, 1); 232 lcd.blink(); 233 if (secones >= '0' && secones <= '9') 234 { 235 seconescode = secones - '0'; 236 tone(7, 5000, 100); 237 lcd.print(secones); 238 keycount = 11; 239 } 240 } 241 242 if (keycount == 11); 243 { 244 Hours = (hourstenscode * 10) + hoursonescode; 245 Minutes = (mintenscode * 10) + minonescode; 246 Seconds = (sectenscode * 10) + seconescode; 247 delay(100); 248 lcd.noBlink(); 249 lcd.clear(); 250 lcd.setCursor(0, 0); 251 lcd.print("Timer set at:"); 252 if (Hours >= 10) 253 { 254 lcd.setCursor (7, 1); 255 lcd.print (Hours); 256 } 257 if (Hours < 10) 258 { 259 lcd.setCursor (7, 1); 260 lcd.write ("0"); 261 lcd.setCursor (8, 1); 262 lcd.print (Hours); 263 } 264 lcd.print (":"); 265 266 if (Minutes >= 10) 267 { 268 lcd.setCursor (10, 1); 269 lcd.print (Minutes); 270 } 271 if (Minutes < 10) 272 { 273 lcd.setCursor (10, 1); 274 lcd.write ("0"); 275 lcd.setCursor (11, 1); 276 lcd.print (Minutes); 277 } 278 lcd.print (":"); 279 280 if (Seconds >= 10) 281 { 282 lcd.setCursor (13, 1); 283 lcd.print (Seconds); 284 } 285 286 if (Seconds < 10) 287 { 288 lcd.setCursor (13, 1); 289 lcd.write ("0"); 290 lcd.setCursor (14, 1); 291 lcd.print (Seconds); 292 } 293 delay(3000); 294 lcd.clear(); 295 lcd.setCursor(0, 0); 296 lcd.print("Press # to arm"); 297 delay (50); 298 keycount = 12; 299 } 300 301 while (keycount == 12) 302 { 303 char armkey = keypad.getKey(); 304 305 if (armkey == '#') 306 { 307 tone(7, 5000, 100); 308 delay(50); 309 tone(7, 0, 100); 310 delay(50); 311 tone(7, 5000, 100); 312 delay(50); 313 tone(7, 0, 100); 314 delay(50); 315 tone(7, 5000, 100); 316 delay(50); 317 tone(7, 0, 100); 318 lcd.clear(); 319 lcd.print ("Bomb Armed!"); 320 lcd.setCursor(0, 1); 321 lcd.print("Countdown start"); 322 delay(3000); 323 lcd.clear(); 324 keycount = 0; 325 } 326 } 327} 328void loop() 329{ 330 timer(); 331 char disarmcode = keypad.getKey(); 332 333 if (disarmcode == '*') 334 { 335 tone(7, 5000, 100); 336 lcd.clear(); 337 lcd.setCursor(0, 0); 338 lcd.print("Code: "); 339 340 while (keycount < 4) 341 { 342 timer(); 343 344 char disarmcode = keypad.getKey(); 345 if (disarmcode == '#') 346 { 347 tone(7, 5000, 100); 348 keycount = 0; 349 lcd.clear(); 350 lcd.setCursor(0, 0); 351 lcd.print("Code: "); 352 } 353 else if (disarmcode != NO_KEY) 354 { 355 lcd.setCursor(keycount + 7, 0); 356 lcd.blink(); 357 lcd.print(disarmcode); 358 entered[keycount] = disarmcode; 359 keycount++; 360 tone(7, 5000, 100); 361 delay(100); 362 lcd.noBlink(); 363 lcd.setCursor(keycount + 6, 0); 364 lcd.print("*"); 365 lcd.setCursor(keycount + 7, 0); 366 lcd.blink(); 367 } 368 } 369 370 if (keycount == 4) 371 { 372 if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3]) 373 { 374 lcd.noBlink(); 375 lcd.clear(); 376 lcd.home(); 377 lcd.print("Bomb Defused!"); 378 lcd.setCursor(0, 1); 379 lcd.print("Great job!"); 380 keycount = 0; 381 digitalWrite(greenled, HIGH); 382 delay(15000); 383 lcd.clear(); 384 lcd.setCursor(0, 1); 385 lcd.print("Reset the Bomb"); 386 delay(1000000); 387 } 388 else 389 { 390 lcd.noBlink(); 391 lcd.clear(); 392 lcd.home(); 393 lcd.print("Wrong Password!"); 394 trycount++; 395 396 if (Hours > 0) 397 { 398 Hours = Hours / 2; 399 } 400 401 if (Minutes > 0) 402 { 403 Minutes = Minutes / 2; 404 } 405 if (Seconds > 0) 406 { 407 Seconds = Seconds / 2; 408 } 409 if (trycount == 2) 410 { 411 interval = interval / 10; 412 } 413 if (trycount == 3) 414 { 415 Minutes = Minutes - 59; 416 Hours = Hours - 59; 417 Seconds = Seconds - 59; 418 } 419 delay(1000); 420 keycount = 0; 421 } 422 } 423 } 424} 425 426void timer() 427{ 428 Serial.print(Seconds); 429 Serial.println(); 430 431 if (Hours <= 0) 432 { 433 if ( Minutes < 0 ) 434 { 435 lcd.noBlink(); 436 lcd.clear(); 437 lcd.home(); 438 lcd.print("The Bomb Has "); 439 lcd.setCursor (0, 1); 440 lcd.print("Exploded!"); 441 442 while (Minutes < 0) 443 { 444 digitalWrite(redled, HIGH); 445 tone(7, 7000, 100); 446 delay(100); 447 digitalWrite(redled, LOW); 448 tone(7, 7000, 100); 449 delay(100); 450 digitalWrite(yellowled, HIGH); 451 tone(7, 7000, 100); 452 delay(100); 453 digitalWrite(yellowled, LOW); 454 tone(7, 7000, 100); 455 delay(100); 456 digitalWrite(greenled, HIGH); 457 tone(7, 7000, 100); 458 delay(100); 459 digitalWrite(greenled, LOW); 460 tone(7, 7000, 100); 461 delay(100); 462 } 463 } 464 } 465 lcd.setCursor (0, 1); 466 lcd.print ("Timer:"); 467 468 if (Hours >= 10) 469 { 470 lcd.setCursor (7, 1); 471 lcd.print (Hours); 472 } 473 if (Hours < 10) 474 { 475 lcd.setCursor (7, 1); 476 lcd.write ("0"); 477 lcd.setCursor (8, 1); 478 lcd.print (Hours); 479 } 480 lcd.print (":"); 481 482 if (Minutes >= 10) 483 { 484 lcd.setCursor (10, 1); 485 lcd.print (Minutes); 486 } 487 if (Minutes < 10) 488 { 489 lcd.setCursor (10, 1); 490 lcd.write ("0"); 491 lcd.setCursor (11, 1); 492 lcd.print (Minutes); 493 } 494 lcd.print (":"); 495 496 if (Seconds >= 10) 497 { 498 lcd.setCursor (13, 1); 499 lcd.print (Seconds); 500 } 501 502 if (Seconds < 10) 503 { 504 lcd.setCursor (13, 1); 505 lcd.write ("0"); 506 lcd.setCursor (14, 1); 507 lcd.print (Seconds); 508 } 509 510 if (Hours < 0) 511 { 512 Hours = 0; 513 } 514 515 if (Minutes < 0) 516 { 517 Hours --; 518 Minutes = 59; 519 } 520 521 if (Seconds < 1) 522 { 523 Minutes --; 524 Seconds = 59; 525 } 526 527 if (Seconds > 0) 528 { 529 unsigned long currentMillis = millis(); 530 531 if (currentMillis - secMillis > interval) 532 { 533 tone(7, 7000, 50); 534 secMillis = currentMillis; 535 Seconds --; 536 digitalWrite(yellowled, HIGH); 537 delay(10); 538 digitalWrite(yellowled, LOW); 539 delay(10); 540 } 541 } 542} 543
Comments
Only logged in users can leave comments