Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////
  2. //      Airsoft Bomb       //
  3. // by Malthe Falkenstjerne //
  4. /////////////////////////////
  5.  
  6. #include <Keypad.h>
  7. #include <LiquidCrystal.h>
  8. #define pound 14
  9.  
  10.  
  11. #define NOTE_B0  31
  12. #define NOTE_C1  33
  13. #define NOTE_CS1 35
  14. #define NOTE_D1  37
  15. #define NOTE_DS1 39
  16. #define NOTE_E1  41
  17. #define NOTE_F1  44
  18. #define NOTE_FS1 46
  19. #define NOTE_G1  49
  20. #define NOTE_GS1 52
  21. #define NOTE_A1  55
  22. #define NOTE_AS1 58
  23. #define NOTE_B1  62
  24. #define NOTE_C2  65
  25. #define NOTE_CS2 69
  26. #define NOTE_D2  73
  27. #define NOTE_DS2 78
  28. #define NOTE_E2  82
  29. #define NOTE_F2  87
  30. #define NOTE_FS2 93
  31. #define NOTE_G2  98
  32. #define NOTE_GS2 104
  33. #define NOTE_A2  110
  34. #define NOTE_AS2 117
  35. #define NOTE_B2  123
  36. #define NOTE_C3  131
  37. #define NOTE_CS3 139
  38. #define NOTE_D3  147
  39. #define NOTE_DS3 156
  40. #define NOTE_E3  165
  41. #define NOTE_F3  175
  42. #define NOTE_FS3 185
  43. #define NOTE_G3  196
  44. #define NOTE_GS3 208
  45. #define NOTE_A3  220
  46. #define NOTE_AS3 233
  47. #define NOTE_B3  247
  48. #define NOTE_C4  262
  49. #define NOTE_CS4 277
  50. #define NOTE_D4  294
  51. #define NOTE_DS4 311
  52. #define NOTE_E4  330
  53. #define NOTE_F4  349
  54. #define NOTE_FS4 370
  55. #define NOTE_G4  392
  56. #define NOTE_GS4 415
  57. #define NOTE_A4  440
  58. #define NOTE_AS4 466
  59. #define NOTE_B4  494
  60. #define NOTE_C5  523
  61. #define NOTE_CS5 554
  62. #define NOTE_D5  587
  63. #define NOTE_DS5 622
  64. #define NOTE_E5  659
  65. #define NOTE_F5  698
  66. #define NOTE_FS5 740
  67. #define NOTE_G5  784
  68. #define NOTE_GS5 831
  69. #define NOTE_A5  880
  70. #define NOTE_AS5 932
  71. #define NOTE_B5  988
  72. #define NOTE_C6  1047
  73. #define NOTE_CS6 1109
  74. #define NOTE_D6  1175
  75. #define NOTE_DS6 1245
  76. #define NOTE_E6  1319
  77. #define NOTE_F6  1397
  78. #define NOTE_FS6 1480
  79. #define NOTE_G6  1568
  80. #define NOTE_GS6 1661
  81. #define NOTE_A6  1760
  82. #define NOTE_AS6 1865
  83. #define NOTE_B6  1976
  84. #define NOTE_C7  2093
  85. #define NOTE_CS7 2217
  86. #define NOTE_D7  2349
  87. #define NOTE_DS7 2489
  88. #define NOTE_E7  2637
  89. #define NOTE_F7  2794
  90. #define NOTE_FS7 2960
  91. #define NOTE_G7  3136
  92. #define NOTE_GS7 3322
  93. #define NOTE_A7  3520
  94. #define NOTE_AS7 3729
  95. #define NOTE_B7  3951
  96. #define NOTE_C8  4186
  97. #define NOTE_CS8 4435
  98. #define NOTE_D8  4699
  99. #define NOTE_DS8 4978
  100.  
  101. int Scount = 12; // count seconds
  102. int Mcount = 10; // count minutes
  103. int Hcount = 0; // count hours
  104. int DefuseTimer = 0; // set timer to 0
  105.  
  106. long secMillis = 0; // store last time for second add
  107. long interval = 1000; // interval for seconds
  108.  
  109. char password[4]; // number of characters in our password
  110. int currentLength = 0; //defines which number we are currently writing
  111. int i = 0;
  112. char entered[4];
  113.  
  114. int ledPin = 4; //red led
  115. int ledPin2 = 3; //yellow led
  116. int ledPin3 = 2; //green led
  117.  
  118. LiquidCrystal lcd(7,8,10,11,12,13); // the pins we use on the LCD
  119.  
  120. const byte ROWS = 4; //four rows
  121. const byte COLS = 3; //three columns
  122. char keys[ROWS][COLS] = {
  123.   {'1','2','3'},
  124.   {'4','5','6'},
  125.   {'7','8','9'},
  126.   {'*','0','#'}
  127. };
  128. byte rowPins[ROWS] = {5, A5, A4, A2}; //connect to the row pinouts of the keypad
  129. byte colPins[COLS] = {A1, A0, A3}; //connect to the column pinouts of the keypad
  130.  
  131. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  132.  
  133.  
  134. void setup(){
  135.   pinMode(ledPin, OUTPUT); // sets the digital pin as output
  136.   pinMode(ledPin2, OUTPUT); // sets the digital pin as output
  137.   pinMode(ledPin3, OUTPUT); // sets the digital pin as output
  138.   pinMode(9,OUTPUT);
  139.   lcd.begin(16, 2);
  140.   Serial.begin(9600);
  141.   lcd.clear();
  142.   lcd.setCursor(0,0);
  143.   lcd.print("Enter Code: ");
  144.   while (currentLength < 4)
  145.   {
  146.     lcd.setCursor(currentLength + 6, 1);
  147.     lcd.cursor();
  148.     char key = keypad.getKey();
  149.     key == NO_KEY;
  150.     if (key != NO_KEY)
  151.     {
  152.       if ((key != '*')&&(key != '#'))
  153.       {
  154.       lcd.print(key);
  155.       password[currentLength] = key;
  156.       currentLength++;
  157.       tone(NOTE_C6, 200);
  158.       }
  159.     }
  160.   }
  161.  
  162.   if (currentLength == 4)
  163.   {
  164.     delay(500);
  165.     lcd.noCursor();
  166.     lcd.clear();
  167.     lcd.home();
  168.     lcd.print("You've Entered: ");
  169.     lcd.setCursor(6,1);
  170.     lcd.print(password[0]);
  171.     lcd.print(password[1]);
  172.     lcd.print(password[2]);
  173.     lcd.print(password[3]);
  174.  
  175.     tone(NOTE_E6, 200);
  176.     delay(3000);
  177.     lcd.clear();
  178.     currentLength = 0;
  179.   }
  180. }
  181.  
  182. void loop()
  183. {
  184.   timer();
  185.   char key2 = keypad.getKey(); // get the key
  186.  
  187.   if (key2 == '*')
  188.     {
  189.       lcd.clear();
  190.       lcd.setCursor(0,0);
  191.       lcd.print("Code: ");
  192.      
  193.       while (currentLength < 4)
  194.         {
  195.          
  196.           timer();
  197.          
  198.           char key2 = keypad.getKey();
  199.           if (key2 == '#')
  200.             {
  201.               currentLength = 0;
  202.               lcd.clear();
  203.               lcd.setCursor(0,0);
  204.               lcd.print("Code: ");
  205.             }
  206.           else                  
  207.           if (key2 != NO_KEY)    
  208.             {
  209.              
  210.               lcd.setCursor(currentLength + 7, 0);
  211.               lcd.cursor();
  212.              
  213.               lcd.print(key2);
  214.               entered[currentLength] = key2;
  215.               currentLength++;
  216.               tone(NOTE_C6, 200);
  217.               delay(100);
  218.               lcd.noCursor();
  219.               lcd.setCursor(currentLength + 6, 0);
  220.               lcd.print("*");
  221.               lcd.setCursor(currentLength + 7, 0);
  222.               lcd.cursor();
  223.             }
  224.         }
  225.  
  226.       if (currentLength == 4)
  227.         {
  228.           if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3])
  229.             {
  230.               lcd.noCursor();
  231.               lcd.clear();
  232.               lcd.home();
  233.               lcd.print("Bomb Defused");
  234.               currentLength = 0;
  235.               digitalWrite(ledPin3, HIGH);
  236.               delay(2500);
  237.               lcd.setCursor(0,1);
  238.               lcd.print("Reset the Bomb");
  239.               delay(1000000);
  240.             }
  241.       else
  242.         {
  243.           lcd.noCursor();
  244.           lcd.clear();
  245.           lcd.home();
  246.           lcd.print("Wrong Password!");
  247.    
  248.           if (Hcount > 0)
  249.             {
  250.               Hcount = Hcount - 1;
  251.             }
  252.    
  253.           if (Mcount > 0)
  254.             {
  255.               Mcount = Mcount - 59;
  256.             }
  257.           if (Scount > 0)
  258.             {
  259.               Scount = Scount - 59;
  260.             }
  261.         delay(1500);
  262.         currentLength = 0;
  263.  
  264.         }
  265.       }  
  266.     }
  267. }
  268.  
  269. void timer()
  270. {
  271.   Serial.print(Scount);
  272.   Serial.println();
  273.  
  274.   if (Hcount <= 0)
  275.   {
  276.     if ( Mcount < 0 )
  277.     {
  278.       lcd.noCursor();
  279.       lcd.clear();
  280.       lcd.home();
  281.       lcd.print("The Bomb Has ");
  282.       lcd.setCursor (0,1);
  283.       lcd.print("Exploded!");
  284.      
  285.       while (Mcount < 0)
  286.       {
  287.         digitalWrite(ledPin, HIGH); // sets the LED on
  288.         tone(NOTE_A2, 90);
  289.         delay(100);
  290.         digitalWrite(ledPin, LOW); // sets the LED off
  291.         tone(NOTE_A2, 90);
  292.         delay(100);
  293.         digitalWrite(ledPin2, HIGH); // sets the LED on
  294.         tone(NOTE_A2, 90);
  295.         delay(100);
  296.         digitalWrite(ledPin2, LOW); // sets the LED off
  297.         tone(NOTE_A2, 90);
  298.         delay(100);
  299.         digitalWrite(ledPin3, HIGH); // sets the LED on
  300.         tone(NOTE_A2, 90);
  301.         delay(100);
  302.         digitalWrite(ledPin3, LOW); // sets the LED off
  303.         tone(NOTE_A2, 90);
  304.         delay(100);
  305.       }
  306.     }
  307.   }
  308.  
  309.   lcd.setCursor (0,1); // sets cursor to 2nd line
  310.   lcd.print ("Timer:");
  311.  
  312.   if (Hcount >= 10)
  313.     {
  314.       lcd.setCursor (7,1);
  315.       lcd.print (Hcount);
  316.     }
  317.   if (Hcount < 10)
  318.     {
  319.       lcd.setCursor (7,1);
  320.       lcd.write ("0");
  321.       lcd.setCursor (8,1);
  322.       lcd.print (Hcount);
  323.     }
  324.  
  325.   lcd.print (":");
  326.  
  327.   if (Mcount >= 10)
  328.     {
  329.       lcd.setCursor (10,1);
  330.       lcd.print (Mcount);
  331.     }
  332.   if (Mcount < 10)
  333.     {
  334.       lcd.setCursor (10,1);
  335.       lcd.write ("0");
  336.       lcd.setCursor (11,1);
  337.       lcd.print (Mcount);
  338.     }
  339.    
  340.   lcd.print (":");
  341.  
  342.   if (Scount >= 10)
  343.     {
  344.       lcd.setCursor (13,1);
  345.       lcd.print (Scount);
  346.     }
  347.   if (Scount < 10)
  348.     {
  349.       lcd.setCursor (13,1);
  350.       lcd.write ("0");
  351.       lcd.setCursor (14,1);
  352.       lcd.print (Scount);
  353.     }
  354.  
  355.   if (Hcount <0)
  356.     {
  357.       Hcount = 0;
  358.     }
  359.  
  360.   if (Mcount <0)
  361.     {
  362.       Hcount --;
  363.       Mcount = 59;
  364.     }
  365.  
  366.   if (Scount <1) // if 60 do this operation
  367.     {
  368.       Mcount --; // add 1 to Mcount
  369.       Scount = 59; // reset Scount
  370.     }
  371.  
  372.   if (Scount > 0) // do this oper. 59 times
  373.     {
  374.       unsigned long currentMillis = millis();
  375.  
  376.       if(currentMillis - secMillis > interval)
  377.         {
  378.           tone(NOTE_G5, 200);
  379.           secMillis = currentMillis;
  380.           Scount --; // add 1 to Scount
  381.           digitalWrite(ledPin2, HIGH); // sets the LED on
  382.           delay(10); // waits for a second
  383.           digitalWrite(ledPin2, LOW); // sets the LED off
  384.           delay(10); // waits for a second
  385.           //lcd.clear();
  386.         }
  387.     }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement