Guest User

Untitled

a guest
May 23rd, 2016
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <LiquidCrystal.h>
  3. #include <Tone.h>
  4. #include <Timer.h>
  5. #define pound 14
  6.  
  7. Timer counter;
  8. //LiquidCrystal lcd(A5,A4,A3,A2,A1,A0);
  9. LiquidCrystal lcd(7,8,9,10,11,12);
  10.  
  11. int DefuseTimer = 0; // set timer to 0
  12. Tone tone1;
  13. int oldS = 0;
  14. int nbBoucle = 0;
  15.  
  16. int delayTimer = 500; //definicao para deixar o tempo mais rapido
  17.  
  18. const int pinSpeedBoom = 3;
  19. const int pinReadDefused = 4;
  20. const int pinReadBoom = 5;
  21.  
  22. char password[10]; // number of characters in our password
  23. int currentLength = 0; //defines which number we are currently writing
  24. int i = 0;
  25. char entered[10];
  26. int passwordLength;
  27. int nbtry = 3;
  28. int currentTry = nbtry;
  29. const int Buzzer = 13;
  30.  
  31. const byte ROWS = 4; //four rows
  32. const byte COLS = 3; //three columns
  33. char keys[ROWS][COLS] = {
  34. {'1','2','3'},
  35. {'4','5','6'},
  36. {'7','8','9'},
  37. {'*','0','#'}
  38. };
  39.  
  40. //Pour les keypad avec la fiche de connection vers le HAUT du clavier :
  41. //byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
  42. //byte colPins[COLS] = {13, 12, 11}; //connect to the column pinouts of the keypad
  43.  
  44. //Pour les keypad avec la fiche de connection vers le BAS du clavier :
  45. byte rowPins[ROWS] = {A5, A4, A2, A0}; //connect to the row pinouts of the keypad
  46. byte colPins[COLS] = {1, A3, A1}; //connect to the column pinouts of the keypad
  47.  
  48. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  49. boolean b600s = true;
  50. boolean b300s = true;
  51. boolean b120s = true;
  52. boolean b60s = true;
  53. boolean b30s = true;
  54. boolean b10s = true;
  55.  
  56.  
  57. void setup()
  58. {
  59. 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)
  60. int hours, minutes, seconds;
  61. lcd.begin(16, 2);
  62. tone1.begin(Buzzer);
  63. //pinMode(Buzzer, OUTPUT);
  64. //digitalWrite(Buzzer, LOW);
  65. char key;
  66. boolean cancel;
  67. do
  68. {
  69. cancel = false;
  70. lcd.clear();
  71. lcd.setCursor(0,0);
  72. lcd.print("Time ?");
  73. lcd.setCursor(4,1);
  74. //Heures
  75. hours = readHours();
  76. if (hours == -1)
  77. cancel = true;
  78. else
  79. lcd.print(":");
  80.  
  81. //Minutes
  82. if (cancel == false)
  83. {
  84. minutes = readMinutes();
  85. if (minutes == -1)
  86. cancel = true;
  87. else
  88. lcd.print(":");
  89. }
  90.  
  91. //Secondes
  92. if (cancel == false)
  93. {
  94. seconds = readSeconds();
  95. if (seconds == -1)
  96. cancel = true;
  97. }
  98.  
  99. if (cancel == false)
  100. {
  101. do
  102. key = keypad.waitForKey();
  103. while (key != '#' && key != '*');
  104. buttonBip(100);
  105. if (key == '*')
  106. cancel = true;
  107. }
  108.  
  109. }
  110. while (cancel == true);
  111. delay (500);
  112. counter.setTime(hours, minutes, seconds);
  113.  
  114. lcd.clear();
  115. lcd.setCursor(0,0);
  116. lcd.print("Enter Code: ");
  117. while ((currentLength < 1 || key != '#'))
  118. {
  119. lcd.setCursor(currentLength, 1);
  120. lcd.cursor();
  121. key = keypad.getKey();
  122. if (key != NO_KEY)
  123. {
  124. buttonBip(100);
  125. if (key != '#' && key != '*' && currentLength < 10)
  126. {
  127. lcd.print(key);
  128. password[currentLength] = key;
  129. ++currentLength;
  130. passwordLength = currentLength;
  131. }
  132. else if (key == '*')
  133. {
  134. lcd.clear();
  135. lcd.setCursor(0,0);
  136. lcd.print("Enter Code: ");
  137. currentLength = 0;
  138. passwordLength = currentLength;
  139. }
  140. }
  141. }
  142. Serial.print("rad:bomb:planted");
  143. delay(500);
  144. lcd.clear();
  145. currentLength = 0;
  146. }
  147.  
  148. void loop()
  149. {
  150. timer();
  151. currentLength = 0;
  152. lcd.clear();
  153. lcd.setCursor(0,0);
  154. lcd.print("Code: ");
  155.  
  156. boolean lec_code = true;
  157. char code[10];
  158. int i = 0;
  159. char key2;
  160.  
  161. while (lec_code == true)
  162. {
  163. timer();
  164.  
  165. key2 = keypad.getKey();
  166. if (key2 != NO_KEY)
  167. buttonBip(100);
  168.  
  169. if (key2 == '#' || i >= 10)
  170. lec_code = false;
  171. else if (key2 == '*')
  172. {
  173. currentLength = 0;
  174. lcd.clear();
  175. lcd.setCursor(0,0);
  176. lcd.print("Code: ");
  177. }
  178. else
  179. if (key2 != NO_KEY && currentLength < 10)
  180. {
  181. entered[currentLength] = key2;
  182. currentLength++;
  183. delay(100);
  184. lcd.noCursor();
  185. lcd.setCursor(currentLength + 4, 0);
  186. lcd.print(key2);
  187. lcd.setCursor(currentLength + 5, 0);
  188. lcd.cursor();
  189. }
  190. }
  191.  
  192. boolean correct = true;
  193. for (int j = 0; j < currentLength; ++j)
  194. if (entered[j] != password[j])
  195. correct = false;
  196.  
  197. if (correct == true && passwordLength == currentLength)
  198. {
  199. Defused();
  200. }
  201. else if (currentLength >= 1)
  202. {
  203. lcd.noCursor();
  204. lcd.clear();
  205. lcd.home();
  206. lcd.print("Wrong Password!");
  207. buttonBip(100);
  208. delay(200);
  209. buttonBip(100);
  210. --currentTry;
  211. if (currentTry == 0)
  212. Boom();
  213. lcd.setCursor(0, 1);
  214. lcd.print("Try : ");
  215. lcd.print(currentTry);
  216. delay(1500);
  217. currentLength = 0;
  218.  
  219. }
  220. }
  221.  
  222. //--------------------------------- Timer --------------------------------
  223. void timer()
  224. {
  225. if (counter.seconds == 0 && counter.minutes == 10 && counter.hours == 0 && b600s == true) {Serial.print("rad:bomb:600s"); b600s = false;}
  226. if (counter.seconds == 0 && counter.minutes == 5 && counter.hours == 0 && b300s == true) {Serial.print("rad:bomb:300s"); b300s = false;}
  227. if (counter.seconds == 0 && counter.minutes == 2 && counter.hours == 0 && b120s == true) {Serial.print("rad:bomb:120s"); b120s = false;}
  228. if (counter.seconds == 0 && counter.minutes == 1 && counter.hours == 0 && b60s == true) {Serial.print("rad:bomb:60s"); b60s = false;}
  229. if (counter.seconds == 30 && counter.minutes == 0 && counter.hours == 0 && b30s == true) {Serial.print("rad:bomb:30s"); b30s = false;}
  230. if (counter.seconds == 10 && counter.minutes == 0 && counter.hours == 0 && b10s == true) {Serial.print("rad:bomb:10s"); b10s = false;}
  231.  
  232. ++nbBoucle;
  233. if (((counter.seconds > 10 && counter.minutes < 1 && counter.hours < 1) || (counter.minutes >= 1 || counter.hours >= 1)) && oldS != counter.seconds)
  234. {
  235. oldS = counter.seconds;
  236. timeBip(100);
  237. }
  238. else if (counter.seconds <= 10 && counter.seconds > 5 && counter.minutes < 1 && counter.hours < 1 && nbBoucle >= 15)
  239. {
  240. nbBoucle = 0;
  241. timeBip(100);
  242. }
  243. else if (counter.seconds <= 5 && counter.seconds > 3 && counter.minutes < 1 && counter.hours < 1 && nbBoucle >= 10)
  244. {
  245. nbBoucle = 0;
  246. timeBip(100);
  247. }
  248. else if (counter.seconds <= 3 && counter.minutes < 1 && counter.hours < 1 && nbBoucle >= 5)
  249. {
  250. nbBoucle = 0;
  251. timeBip(100);
  252. }
  253.  
  254. refreshTimeLcd();
  255.  
  256. if (counter.decrement(millis()))
  257. {
  258. Boom ();
  259. }
  260. readPort();
  261. lcd.noCursor();
  262. delay(10); // waits for a second
  263. delay(10); // waits for a second
  264. }
  265.  
  266. //--------------------------------- LCD --------------------------------
  267. void refreshTimeLcd()
  268. {
  269. lcd.setCursor (0,1); // sets cursor to 2nd line
  270. lcd.print ("Timer:");
  271. if (counter.hours >= 10)
  272. {
  273. lcd.setCursor (7,1);
  274. lcd.print (counter.hours);
  275. }
  276. if (counter.hours < 10)
  277. {
  278. lcd.setCursor (7,1);
  279. lcd.write ("0");
  280. lcd.setCursor (8,1);
  281. lcd.print (counter.hours);
  282. }
  283.  
  284. lcd.print (":");
  285.  
  286. if (counter.minutes >= 10)
  287. {
  288. lcd.setCursor (10,1);
  289. lcd.print (counter.minutes);
  290. }
  291. if (counter.minutes < 10)
  292. {
  293. lcd.setCursor (10,1);
  294. lcd.write ("0");
  295. lcd.setCursor (11,1);
  296. lcd.print (counter.minutes);
  297. }
  298.  
  299. lcd.print (":");
  300.  
  301. if (counter.seconds >= 10)
  302. {
  303. lcd.setCursor (13,1);
  304. lcd.print (counter.seconds);
  305. }
  306. if (counter.seconds < 10)
  307. {
  308. lcd.setCursor (13,1);
  309. lcd.write ("0");
  310. lcd.setCursor (14,1);
  311. lcd.print (counter.seconds);
  312. }
  313. }
  314.  
  315. //--------------------------------- Heures --------------------------------
  316. int readHours ()
  317. {
  318. char key;
  319. char hour[3];
  320. int hours;
  321. for (int i=0; i<2; ++i)
  322. {
  323. do
  324. {
  325. key = keypad.waitForKey();
  326. if (key != NO_KEY)
  327. buttonBip(100);
  328. }
  329. while (key == NO_KEY || key == '#');
  330.  
  331. if (key == '*')
  332. return -1;
  333.  
  334. hour[i] = key;
  335. lcd.print(key);
  336. }
  337. hour[2] = '\0';
  338. hours = atoi(hour);
  339. return hours;
  340. }
  341.  
  342. //--------------------------------- Minutes --------------------------------
  343. int readMinutes ()
  344. {
  345. char stTemp[2];
  346. char key;
  347. char minute[3];
  348. int minutes;
  349. for (int i=0; i<2; ++i)
  350. {
  351. do
  352. {
  353. key = keypad.waitForKey();
  354. if (key != NO_KEY)
  355. buttonBip(100);
  356. if (key == '*')
  357. return -1;
  358. sprintf(stTemp,"%c",key);
  359. }
  360. while (key == NO_KEY || key == '*' || key == '#' || ((atoi(stTemp)) >= 6 && i == 0));
  361. minute[i] = key;
  362. lcd.print(key);
  363. }
  364. minute[2] = '\0';
  365. minutes = atoi(minute);
  366. return minutes;
  367. }
  368.  
  369. //--------------------------------- Secondes --------------------------------
  370. int readSeconds ()
  371. {
  372. char stTemp[2];
  373. char key;
  374. char second[3];
  375. int seconds;
  376. for (int i=0; i<2; ++i)
  377. {
  378. do
  379. {
  380. key = keypad.waitForKey();
  381. if (key != NO_KEY)
  382. buttonBip(100);
  383. if (key == '*')
  384. return -1;
  385. sprintf(stTemp,"%c",key);
  386. }
  387. while (key == NO_KEY || key == '*' || key == '#' || (atoi(stTemp) >= 6 && i == 0));
  388. second[i] = key;
  389. lcd.print(key);
  390. }
  391. second[2] = '\0';
  392. seconds = atoi(second);
  393. return seconds;
  394. }
  395.  
  396. //--------------------------------- Boom -------------------------------------
  397. void Boom ()
  398. {
  399. lcd.noCursor();
  400. lcd.clear();
  401. lcd.home();
  402. lcd.print("The Bomb Has ");
  403. lcd.setCursor (0,1);
  404. lcd.print("Exploded!");
  405. tone1.play(NOTE_F2, 3000);
  406. delay(8000);
  407. while (1)
  408. delay(100000);
  409. }
  410.  
  411. //--------------------------------- Defused ----------------------------------
  412. void Defused ()
  413. {
  414. lcd.noCursor();
  415. lcd.clear();
  416. lcd.home();
  417. lcd.print("Bomb Defused");
  418. refreshTimeLcd();
  419. buttonBip(200);
  420. delay(300);
  421. buttonBip(200);
  422. delay(300);
  423. buttonBip(200);
  424. currentLength = 0;
  425. while (1)
  426. delay(1000000);
  427. }
  428.  
  429. //--------------------------------- SpeedBoom -------------------------------------
  430. void SpeeBoom()
  431. {
  432. lcd.blink();
  433. buttonBip(600);
  434. delay(700);
  435. buttonBip(100);
  436. delay(700);
  437.  
  438. }
  439.  
  440. //--------------------------------- readPort ----------------------------------
  441. void readPort()
  442. {
  443. pinMode(pinSpeedBoom, INPUT);
  444. pinMode(pinReadDefused, INPUT);
  445. pinMode(pinReadBoom, INPUT);
  446. byte SpeedBoom = digitalRead(pinSpeedBoom);
  447. byte defused = digitalRead(pinReadDefused);
  448. byte boom = digitalRead(pinReadBoom);
  449. if (SpeedBoom == HIGH)
  450. SpeeBoom();
  451. if (defused == HIGH)
  452. Defused();
  453. if (boom == HIGH)
  454. Boom();
  455. }
  456.  
  457. //---------------------------------- timeBip ---------------------------------
  458. void timeBip (int mills)
  459. {
  460. tone1.play(NOTE_F7, mills);
  461. }
  462.  
  463. //---------------------------------- buttonBip ---------------------------------
  464. void buttonBip (int mills)
  465. {
  466. tone1.play(NOTE_A7, mills);
  467. }
Advertisement
Add Comment
Please, Sign In to add comment