Advertisement
Guest User

Untitled

a guest
Jul 8th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.57 KB | None | 0 0
  1. /////////////////////////////
  2. //      Airsoft Bomb       //
  3. // by Malthe Falkenstjerne //
  4. /////////////////////////////
  5.  
  6. #include <Keypad.h>
  7. //#include <LiquidCrystal.h>
  8. #include <Wire.h>   // standardowa biblioteka Arduino
  9. #include <LiquidCrystal_I2C.h> // dolaczenie pobranej biblioteki I2C dla LCD
  10. //#include <Tone.h>
  11. #define pound 14
  12.  
  13. //Tone tone1;
  14.  
  15. int ticker = 0;
  16. int a = 1;
  17. int Scount = 12; // count seconds
  18. int Mcount = 10; // count minutes
  19. int Hcount = 0; // count hours
  20. int DefuseTimer = 0; // set timer to 0
  21.  
  22. long secMillis = 0; // store last time for second add
  23. long interval = 1000; // interval for seconds
  24.  
  25. char password[4]; // number of characters in our password
  26. int currentLength = 0; //defines which number we are currently writing
  27. int i = 0;
  28. char entered[4];
  29.  
  30. int ledPin = A0; //red led
  31. int ledPin2 = A1; //yelHIGH led
  32. int ledPin3 = A2; //green led
  33.  
  34. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Ustawienie adresu ukladu na 0x27
  35.  
  36. const byte ROWS = 4; //four rows
  37. const byte COLS = 3; //three columns
  38. char keys[ROWS][COLS] = {
  39.   {'1', '2', '3',},
  40.   {'4', '5', '6',},
  41.   {'7', '8', '9',},
  42.   {'*', '0', '#',}
  43. };
  44.  
  45. //Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  46. byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
  47. // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  48. byte colPins[COLS] = {6, 5, 4}; //connect to the column pinouts of the keypad
  49.  
  50. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  51.  
  52.  
  53. void setup() {
  54.   pinMode(ledPin, OUTPUT); // sets the digital pin as output
  55.   pinMode(ledPin2, OUTPUT); // sets the digital pin as output
  56.   pinMode(ledPin3, OUTPUT); // sets the digital pin as output
  57.   //  tone1.begin(9);
  58.   lcd.begin(16, 2);
  59.   Serial.begin(9600);
  60.   lcd.clear();
  61.   lcd.setCursor(0, 0);
  62.   lcd.print("Enter Code: ");
  63.   while (currentLength < 4)
  64.   {
  65.     lcd.setCursor(currentLength + 6, 1);
  66.     lcd.cursor();
  67.     char key = keypad.getKey();
  68.     key == NO_KEY;
  69.     if (key != NO_KEY)
  70.     {
  71.       if ((key != '*') && (key != '#'))
  72.       {
  73.         lcd.print(key);
  74.         password[currentLength] = key;
  75.         currentLength++;
  76.         //      tone1.play(NOTE_C6, 200);
  77.       }
  78.     }
  79.   }
  80.  
  81.   if (currentLength == 4)
  82.   {
  83.     delay(500);
  84.     lcd.noCursor();
  85.     lcd.clear();
  86.     lcd.home();
  87.     lcd.print("You've Entered: ");
  88.     lcd.setCursor(6, 1);
  89.     lcd.print(password[0]);
  90.     lcd.print(password[1]);
  91.     lcd.print(password[2]);
  92.     lcd.print(password[3]);
  93.  
  94.     //    tone1.play(NOTE_E6, 200);
  95.     delay(3000);
  96.     lcd.clear();
  97.     currentLength = 0;
  98.   }
  99. }
  100.  
  101. void loop()
  102. {
  103.   timer();
  104.   char key2 = keypad.getKey(); // get the key
  105.  
  106.   if (key2 == '*')
  107.   {
  108.     lcd.clear();
  109.     lcd.setCursor(0, 0);
  110.     lcd.print("Code: ");
  111.  
  112.     while (currentLength < 4)
  113.     {
  114.  
  115.       timer();
  116.  
  117.       char key2 = keypad.getKey();
  118.       if (key2 == '#')
  119.       {
  120.         currentLength = 0;
  121.         lcd.clear();
  122.         lcd.setCursor(0, 0);
  123.         lcd.print("Code: ");
  124.       }
  125.       else if (key2 != NO_KEY)
  126.       {
  127.  
  128.         lcd.setCursor(currentLength + 7, 0);
  129.         lcd.cursor();
  130.  
  131.         lcd.print(key2);
  132.         entered[currentLength] = key2;
  133.         currentLength++;
  134.         //              tone1.play(NOTE_C6, 200);
  135.         delay(100);
  136.         lcd.noCursor();
  137.         lcd.setCursor(currentLength + 6, 0);
  138.         lcd.print("*");
  139.         lcd.setCursor(currentLength + 7, 0);
  140.         lcd.cursor();
  141.       }
  142.     }
  143.  
  144.     if (currentLength == 4)
  145.     {
  146.       if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3])
  147.       {
  148.         lcd.noCursor();
  149.         lcd.clear();
  150.         lcd.home();
  151.         lcd.print("Bomb Defused");
  152.         currentLength = 0;
  153.         digitalWrite(ledPin3, LOW);
  154.         delay(2500);
  155.         lcd.setCursor(0, 1);
  156.         lcd.print("Reset the Bomb");
  157.         delay(1000000);
  158.       }
  159.       else
  160.       {
  161.         lcd.noCursor();
  162.         lcd.clear();
  163.         lcd.home();
  164.         lcd.print("Wrong Password!");
  165.  
  166.         if (Hcount > 0)
  167.         {
  168.           Hcount = Hcount - 1;
  169.         }
  170.  
  171.         if (Mcount > 0)
  172.         {
  173.           Mcount = Mcount - 59;
  174.         }
  175.         if (Scount > 0)
  176.         {
  177.           Scount = Scount - 59;
  178.         }
  179.         delay(1500);
  180.         currentLength = 0;
  181.  
  182.       }
  183.     }
  184.   }
  185. }
  186.  
  187. void timer()
  188. {
  189.   Serial.print(Scount);
  190.   Serial.println();
  191.  
  192.   if (Hcount <= 0)
  193.   {
  194.     if ( Mcount < 0 )
  195.     {
  196.       lcd.noCursor();
  197.       lcd.clear();
  198.       lcd.home();
  199.       lcd.print("The Bomb Has ");
  200.       lcd.setCursor (0, 1);
  201.       lcd.print("Exploded!");
  202.  
  203.       while (Mcount < 0)
  204.       {
  205.  
  206.  
  207.  
  208.         delay(100);
  209.       }
  210.     }
  211.   }
  212.  
  213.   lcd.setCursor (0, 1); // sets cursor to 2nd line
  214.   lcd.print ("Timer:");
  215.  
  216.   if (Hcount >= 10)
  217.   {
  218.     lcd.setCursor (7, 1);
  219.     lcd.print (Hcount);
  220.   }
  221.   if (Hcount < 10)
  222.   {
  223.     lcd.setCursor (7, 1);
  224.     lcd.write ("0");
  225.     lcd.setCursor (8, 1);
  226.     lcd.print (Hcount);
  227.   }
  228.  
  229.   lcd.print (":");
  230.  
  231.   if (Mcount >= 10)
  232.   {
  233.     lcd.setCursor (10, 1);
  234.     lcd.print (Mcount);
  235.   }
  236.   if (Mcount < 10)
  237.   {
  238.     lcd.setCursor (10, 1);
  239.     lcd.write ("0");
  240.     lcd.setCursor (11, 1);
  241.     lcd.print (Mcount);
  242.   }
  243.  
  244.   lcd.print (":");
  245.  
  246.   if (Scount >= 10)
  247.   {
  248.     lcd.setCursor (13, 1);
  249.     lcd.print (Scount);
  250.   }
  251.   if (Scount < 10)
  252.   {
  253.     lcd.setCursor (13, 1);
  254.     lcd.write ("0");
  255.     lcd.setCursor (14, 1);
  256.     lcd.print (Scount);
  257.   }
  258.  
  259.   if (Hcount < 0)
  260.   {
  261.     Hcount = 0;
  262.   }
  263.  
  264.   if (Mcount < 0)
  265.   {
  266.     Hcount --;
  267.     Mcount = 59;
  268.   }
  269.  
  270.   if (Scount < 1) // if 60 do this operation
  271.   {
  272.     Mcount --; // add 1 to Mcount
  273.     Scount = 59; // reset Scount
  274.   }
  275.  
  276.   if (Scount > 0) // do this oper. 59 times
  277.   {
  278.     unsigned long currentMillis = millis();
  279.  
  280.     if (currentMillis - secMillis > interval)
  281.     {
  282.          if (a == 1) { ///// biaΕ‚y
  283.         digitalWrite(ledPin, LOW);
  284.         digitalWrite(ledPin2, LOW);
  285.         digitalWrite(ledPin3, LOW);
  286.        
  287.       //  delay(10);
  288.       }
  289.       if (a == 2) {   //////jasny niebieski
  290.         digitalWrite(ledPin, LOW);
  291.         digitalWrite(ledPin2, HIGH);
  292.         digitalWrite(ledPin3, LOW);
  293.        
  294.        // delay(10);
  295.       }
  296.          if (a == 1) {   ///ciemny niebieski
  297.         digitalWrite(ledPin, HIGH);
  298.         digitalWrite(ledPin2, LOW);
  299.         digitalWrite(ledPin3, HIGH);
  300.         //a++;
  301.       //  delay(10);
  302.       }
  303.       if (a == 2) {    //////////////zielony
  304.         digitalWrite(ledPin, LOW);
  305.         digitalWrite(ledPin2, HIGH);
  306.         digitalWrite(ledPin3, LOW);
  307.    
  308.        // delay(10);
  309.       }
  310.        if (a == 3) {
  311.         digitalWrite(ledPin, HIGH);
  312.         digitalWrite(ledPin2, HIGH);
  313.         digitalWrite(ledPin3, LOW);
  314.    
  315.        // delay(10);
  316.       }
  317.        if (a == 4) {
  318.         digitalWrite(ledPin, LOW);
  319.         digitalWrite(ledPin2, HIGH);
  320.         digitalWrite(ledPin3, HIGH);
  321.    
  322.        // delay(10);
  323.       }
  324.            a++;
  325.            if (a == 5){
  326.             a =1;
  327.            }
  328.            
  329.       //          tone1.play(NOTE_G5, 200);
  330.       secMillis = currentMillis;
  331.       Scount --; // add 1 to Scount
  332.       ////////////////////////////////////////////////////////////////////////////////
  333.       ///////////////////////////////////////////////////////////////////////////////
  334.    
  335.  
  336.       //  digitalWrite(ledPin2, HIGH); // sets the LED on
  337.       //  delay(10); // waits for a second
  338.       //  digitalWrite(ledPin2, LOW); // sets the LED off
  339.       //   delay(10); // waits for a second
  340.       //lcd.clear();
  341.     }
  342.   }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement