Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- #include <LiquidCrystal.h>
- #include <Tone.h>
- #include <Timer.h>
- #define pound 14
- Timer counter;
- //LiquidCrystal lcd(A5,A4,A3,A2,A1,A0);
- LiquidCrystal lcd(7,8,9,10,11,12);
- int DefuseTimer = 0; // set timer to 0
- Tone tone1;
- int oldS = 0;
- int nbBoucle = 0;
- int delayTimer = 500; //definicao para deixar o tempo mais rapido
- const int pinSpeedBoom = 3;
- const int pinReadDefused = 4;
- const int pinReadBoom = 5;
- char password[10]; // number of characters in our password
- int currentLength = 0; //defines which number we are currently writing
- int i = 0;
- char entered[10];
- int passwordLength;
- int nbtry = 3;
- int currentTry = nbtry;
- const int Buzzer = 13;
- const byte ROWS = 4; //four rows
- const byte COLS = 3; //three columns
- char keys[ROWS][COLS] = {
- {'1','2','3'},
- {'4','5','6'},
- {'7','8','9'},
- {'*','0','#'}
- };
- //Pour les keypad avec la fiche de connection vers le HAUT du clavier :
- //byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
- //byte colPins[COLS] = {13, 12, 11}; //connect to the column pinouts of the keypad
- //Pour les keypad avec la fiche de connection vers le BAS du clavier :
- byte rowPins[ROWS] = {A5, A4, A2, A0}; //connect to the row pinouts of the keypad
- byte colPins[COLS] = {1, A3, A1}; //connect to the column pinouts of the keypad
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- boolean b600s = true;
- boolean b300s = true;
- boolean b120s = true;
- boolean b60s = true;
- boolean b30s = true;
- boolean b10s = true;
- void setup()
- {
- Serial.end();//esse comando serve para liberar as portas tx e rx para uso como digitais, para que ela funcione precisa tirar o comando serial.begin(9600)
- int hours, minutes, seconds;
- lcd.begin(16, 2);
- tone1.begin(Buzzer);
- //pinMode(Buzzer, OUTPUT);
- //digitalWrite(Buzzer, LOW);
- char key;
- boolean cancel;
- do
- {
- cancel = false;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Time ?");
- lcd.setCursor(4,1);
- //Heures
- hours = readHours();
- if (hours == -1)
- cancel = true;
- else
- lcd.print(":");
- //Minutes
- if (cancel == false)
- {
- minutes = readMinutes();
- if (minutes == -1)
- cancel = true;
- else
- lcd.print(":");
- }
- //Secondes
- if (cancel == false)
- {
- seconds = readSeconds();
- if (seconds == -1)
- cancel = true;
- }
- if (cancel == false)
- {
- do
- key = keypad.waitForKey();
- while (key != '#' && key != '*');
- buttonBip(100);
- if (key == '*')
- cancel = true;
- }
- }
- while (cancel == true);
- delay (500);
- counter.setTime(hours, minutes, seconds);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Enter Code: ");
- while ((currentLength < 1 || key != '#'))
- {
- lcd.setCursor(currentLength, 1);
- lcd.cursor();
- key = keypad.getKey();
- if (key != NO_KEY)
- {
- buttonBip(100);
- if (key != '#' && key != '*' && currentLength < 10)
- {
- lcd.print(key);
- password[currentLength] = key;
- ++currentLength;
- passwordLength = currentLength;
- }
- else if (key == '*')
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Enter Code: ");
- currentLength = 0;
- passwordLength = currentLength;
- }
- }
- }
- Serial.print("rad:bomb:planted");
- delay(500);
- lcd.clear();
- currentLength = 0;
- }
- void loop()
- {
- timer();
- currentLength = 0;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Code: ");
- boolean lec_code = true;
- char code[10];
- int i = 0;
- char key2;
- while (lec_code == true)
- {
- timer();
- key2 = keypad.getKey();
- if (key2 != NO_KEY)
- buttonBip(100);
- if (key2 == '#' || i >= 10)
- lec_code = false;
- else if (key2 == '*')
- {
- currentLength = 0;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Code: ");
- }
- else
- if (key2 != NO_KEY && currentLength < 10)
- {
- entered[currentLength] = key2;
- currentLength++;
- delay(100);
- lcd.noCursor();
- lcd.setCursor(currentLength + 4, 0);
- lcd.print(key2);
- lcd.setCursor(currentLength + 5, 0);
- lcd.cursor();
- }
- }
- boolean correct = true;
- for (int j = 0; j < currentLength; ++j)
- if (entered[j] != password[j])
- correct = false;
- if (correct == true && passwordLength == currentLength)
- {
- Defused();
- }
- else if (currentLength >= 1)
- {
- lcd.noCursor();
- lcd.clear();
- lcd.home();
- lcd.print("Wrong Password!");
- buttonBip(100);
- delay(200);
- buttonBip(100);
- --currentTry;
- if (currentTry == 0)
- Boom();
- lcd.setCursor(0, 1);
- lcd.print("Try : ");
- lcd.print(currentTry);
- delay(1500);
- currentLength = 0;
- }
- }
- //--------------------------------- Timer --------------------------------
- void timer()
- {
- if (counter.seconds == 0 && counter.minutes == 10 && counter.hours == 0 && b600s == true) {Serial.print("rad:bomb:600s"); b600s = false;}
- if (counter.seconds == 0 && counter.minutes == 5 && counter.hours == 0 && b300s == true) {Serial.print("rad:bomb:300s"); b300s = false;}
- if (counter.seconds == 0 && counter.minutes == 2 && counter.hours == 0 && b120s == true) {Serial.print("rad:bomb:120s"); b120s = false;}
- if (counter.seconds == 0 && counter.minutes == 1 && counter.hours == 0 && b60s == true) {Serial.print("rad:bomb:60s"); b60s = false;}
- if (counter.seconds == 30 && counter.minutes == 0 && counter.hours == 0 && b30s == true) {Serial.print("rad:bomb:30s"); b30s = false;}
- if (counter.seconds == 10 && counter.minutes == 0 && counter.hours == 0 && b10s == true) {Serial.print("rad:bomb:10s"); b10s = false;}
- ++nbBoucle;
- if (((counter.seconds > 10 && counter.minutes < 1 && counter.hours < 1) || (counter.minutes >= 1 || counter.hours >= 1)) && oldS != counter.seconds)
- {
- oldS = counter.seconds;
- timeBip(100);
- }
- else if (counter.seconds <= 10 && counter.seconds > 5 && counter.minutes < 1 && counter.hours < 1 && nbBoucle >= 15)
- {
- nbBoucle = 0;
- timeBip(100);
- }
- else if (counter.seconds <= 5 && counter.seconds > 3 && counter.minutes < 1 && counter.hours < 1 && nbBoucle >= 10)
- {
- nbBoucle = 0;
- timeBip(100);
- }
- else if (counter.seconds <= 3 && counter.minutes < 1 && counter.hours < 1 && nbBoucle >= 5)
- {
- nbBoucle = 0;
- timeBip(100);
- }
- refreshTimeLcd();
- if (counter.decrement(millis()))
- {
- Boom ();
- }
- readPort();
- lcd.noCursor();
- delay(10); // waits for a second
- delay(10); // waits for a second
- }
- //--------------------------------- LCD --------------------------------
- void refreshTimeLcd()
- {
- lcd.setCursor (0,1); // sets cursor to 2nd line
- lcd.print ("Timer:");
- if (counter.hours >= 10)
- {
- lcd.setCursor (7,1);
- lcd.print (counter.hours);
- }
- if (counter.hours < 10)
- {
- lcd.setCursor (7,1);
- lcd.write ("0");
- lcd.setCursor (8,1);
- lcd.print (counter.hours);
- }
- lcd.print (":");
- if (counter.minutes >= 10)
- {
- lcd.setCursor (10,1);
- lcd.print (counter.minutes);
- }
- if (counter.minutes < 10)
- {
- lcd.setCursor (10,1);
- lcd.write ("0");
- lcd.setCursor (11,1);
- lcd.print (counter.minutes);
- }
- lcd.print (":");
- if (counter.seconds >= 10)
- {
- lcd.setCursor (13,1);
- lcd.print (counter.seconds);
- }
- if (counter.seconds < 10)
- {
- lcd.setCursor (13,1);
- lcd.write ("0");
- lcd.setCursor (14,1);
- lcd.print (counter.seconds);
- }
- }
- //--------------------------------- Heures --------------------------------
- int readHours ()
- {
- char key;
- char hour[3];
- int hours;
- for (int i=0; i<2; ++i)
- {
- do
- {
- key = keypad.waitForKey();
- if (key != NO_KEY)
- buttonBip(100);
- }
- while (key == NO_KEY || key == '#');
- if (key == '*')
- return -1;
- hour[i] = key;
- lcd.print(key);
- }
- hour[2] = '\0';
- hours = atoi(hour);
- return hours;
- }
- //--------------------------------- Minutes --------------------------------
- int readMinutes ()
- {
- char stTemp[2];
- char key;
- char minute[3];
- int minutes;
- for (int i=0; i<2; ++i)
- {
- do
- {
- key = keypad.waitForKey();
- if (key != NO_KEY)
- buttonBip(100);
- if (key == '*')
- return -1;
- sprintf(stTemp,"%c",key);
- }
- while (key == NO_KEY || key == '*' || key == '#' || ((atoi(stTemp)) >= 6 && i == 0));
- minute[i] = key;
- lcd.print(key);
- }
- minute[2] = '\0';
- minutes = atoi(minute);
- return minutes;
- }
- //--------------------------------- Secondes --------------------------------
- int readSeconds ()
- {
- char stTemp[2];
- char key;
- char second[3];
- int seconds;
- for (int i=0; i<2; ++i)
- {
- do
- {
- key = keypad.waitForKey();
- if (key != NO_KEY)
- buttonBip(100);
- if (key == '*')
- return -1;
- sprintf(stTemp,"%c",key);
- }
- while (key == NO_KEY || key == '*' || key == '#' || (atoi(stTemp) >= 6 && i == 0));
- second[i] = key;
- lcd.print(key);
- }
- second[2] = '\0';
- seconds = atoi(second);
- return seconds;
- }
- //--------------------------------- Boom -------------------------------------
- void Boom ()
- {
- lcd.noCursor();
- lcd.clear();
- lcd.home();
- lcd.print("The Bomb Has ");
- lcd.setCursor (0,1);
- lcd.print("Exploded!");
- tone1.play(NOTE_F2, 3000);
- delay(8000);
- while (1)
- delay(100000);
- }
- //--------------------------------- Defused ----------------------------------
- void Defused ()
- {
- lcd.noCursor();
- lcd.clear();
- lcd.home();
- lcd.print("Bomb Defused");
- refreshTimeLcd();
- buttonBip(200);
- delay(300);
- buttonBip(200);
- delay(300);
- buttonBip(200);
- currentLength = 0;
- while (1)
- delay(1000000);
- }
- //--------------------------------- SpeedBoom -------------------------------------
- void SpeeBoom()
- {
- lcd.blink();
- buttonBip(600);
- delay(700);
- buttonBip(100);
- delay(700);
- }
- //--------------------------------- readPort ----------------------------------
- void readPort()
- {
- pinMode(pinSpeedBoom, INPUT);
- pinMode(pinReadDefused, INPUT);
- pinMode(pinReadBoom, INPUT);
- byte SpeedBoom = digitalRead(pinSpeedBoom);
- byte defused = digitalRead(pinReadDefused);
- byte boom = digitalRead(pinReadBoom);
- if (SpeedBoom == HIGH)
- SpeeBoom();
- if (defused == HIGH)
- Defused();
- if (boom == HIGH)
- Boom();
- }
- //---------------------------------- timeBip ---------------------------------
- void timeBip (int mills)
- {
- tone1.play(NOTE_F7, mills);
- }
- //---------------------------------- buttonBip ---------------------------------
- void buttonBip (int mills)
- {
- tone1.play(NOTE_A7, mills);
- }
Advertisement
Add Comment
Please, Sign In to add comment