Advertisement
RuiViana

Controle Cofre

Mar 15th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 KB | None | 0 0
  1. //FALTA SOH JOGAR NA MEMORIA EEPROM
  2. // inclusão de bilibotecas
  3. #include <EEPROM.h>
  4. const int releTempo = 12;
  5. int releTempoState = LOW;
  6. long previousMillis = 0;
  7. long releTempoInterval = 7000;
  8.  
  9. #include <Wire.h>
  10. //#include <LiquidCrystal_I2C.h>
  11. #include <LiquidCrystal.h>
  12. // LiquidCrystal lcd (9, 8, 7, 6, 5, 4);
  13. #include <Keypad.h>
  14. #include <Servo.h>
  15. Servo servo1;
  16. //LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3, POSITIVE);
  17. // declarção de variáveis
  18. int count = 0;
  19. char passwordMaster [4] = {'1', '2', '3', '4'}; // senha padrão
  20. char passwordEeprom [4] ;
  21. // char passwordEeprom [4] = {'5', '6', '7', '8'};
  22. // const int audioPin = 9;
  23. const int audioPin = 11;
  24. const int duration = 200;
  25. const byte ROWS = 4; //Quatro linhas
  26. const byte COLS = 3; //Três colunas
  27. char keys[ROWS][COLS] = {
  28. {'1','2','3'},
  29. {'4','5','6'},
  30. {'7','8','9'},
  31. {'*','0','#'}
  32. }; // deinição do layout do teclado
  33. byte rowPins[ROWS] = {7, 6, 5, 4}; // pinagem para as linhas do teclado
  34. byte colPins[COLS] = {10, 9, 8}; // pinagem para as colunas do teclado
  35. // byte rowPins[ROWS] = {8, 7, 6, 5}; // pinagem para as linhas do teclado
  36. // byte colPins[COLS] = {4, 3, 2}; // pinagem para as colunas do teclado
  37. // mapeamento do teclado
  38. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  39. int address = 0; // endereço de escrita na memória
  40. int addr = 0;
  41. byte value; // valor lido da EEPROM
  42. //---------------------------------------------------
  43. void setup()
  44. {
  45. Serial.begin(9600);
  46. pinMode(releTempo, OUTPUT);
  47. servo1.attach(10);
  48. servo1.write(0);
  49. // lcd.begin (16,2);
  50. // lcd.setBacklight(HIGH);
  51. Serial.begin(9600); // inicializa serial
  52. // modo dos pinos de audio e LEDs
  53. pinMode(audioPin, OUTPUT);
  54. // lcd.clear();// limpa LCD
  55. key_init();// inicializa processo
  56. LeSenhaGravada();
  57. }
  58. //---------------------------------------------------
  59. void loop()
  60. {
  61. //Temporizador para Desligar o Relé
  62. unsigned long currentMillis = millis(); //Tempo atual em ms
  63. if (currentMillis - previousMillis > releTempoInterval) //Lógica de verificação do tempo
  64. {
  65. previousMillis = currentMillis; // Salva o tempo atual
  66. if (releTempoState == LOW) //E aqui muda o estado do led //verificando como ele estava anteriormente
  67. {
  68. releTempoState = HIGH;
  69. }
  70. else
  71. {
  72. releTempoState = LOW;
  73. }
  74. digitalWrite(releTempo, releTempoState);
  75. }
  76. char key = keypad.getKey(); // obtém informação do teclado
  77. if (key != NO_KEY)
  78. { // se foi teclado algo
  79. Serial.print("key Inicial ");
  80. Serial.println(key);
  81. if (key == '#')
  82. { // se foi teclado #
  83. code_entry_init(); // mensagem, som e LED
  84. int entrada = 0; // variável de apoio; números de entradas feitas via teclado
  85. while (count < 4 )
  86. { // enquanto não entrou os 4 números necessários para a senha
  87. char key = keypad.getKey(); // obtém informação do teclado
  88. if (key != NO_KEY)
  89. { // se foi teclado algo
  90. Serial.print("key 2 ");
  91. Serial.println(key);
  92. entrada += 1; // aumenta contrador de entradas
  93. tone(audioPin, 1080, 100); // sinal audível
  94. delay(duration);
  95. noTone(audioPin);
  96. if (key == passwordEeprom[count])count += 1 ; // verifica na sequencia da senha, se correto aumenta contador
  97. if ( count == 4 && passwordEeprom)
  98. {
  99. unlocked(); // chegou a 4 digitos corretos, libera acesso
  100. key_init(); // inicializa
  101. break; // interrompe loop
  102. }
  103. if((key == '#') || (entrada == 4))
  104. { // foi teclado # ou 4 entradas incorretas
  105. locked();
  106. key_init(); // inicializa
  107. break; // interrompe loop
  108. }
  109. }
  110. }
  111. }
  112. if(key == '*')
  113. { // se foi teclado *
  114. old_pass_check(); // mensagem para entrar a senha antiga
  115. int entrada = 0;
  116. while (count < 4 )
  117. {
  118. char key = keypad.getKey();
  119. if (key != NO_KEY)
  120. {
  121. entrada += 1;
  122. tone(audioPin, 1080, 100);
  123. delay(duration);
  124. noTone(audioPin);
  125. if (key == passwordMaster[count])count += 1;
  126. if ( count == 4 )
  127. { // foi teclado a senha antiga corretamente
  128. Serial.print("Count ");
  129. Serial.println(count);
  130. get_new_pass(); // chama função para entrada da nova senha
  131. }
  132. if ((key == '*') || (entrada == 4))
  133. { // foi teclado * ou entrou 4 números errados
  134. key_init();// inicializa
  135. break; // interrompe loop
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. //---------------------------------------------------
  143. void locked()
  144. {
  145. // lcd.clear();
  146. // lcd.print(" ACESSO,");
  147. // lcd.setCursor(0,1);
  148. // lcd.print(" NEGADO!");
  149.  
  150. Serial.println(" ACESSO,");
  151. Serial.println(" NEGADO!");
  152. for(int i=0; i<5; i++)
  153. {
  154. tone(audioPin, 1080, 100);
  155. delay(duration);
  156. noTone(audioPin);
  157. delay(100);
  158. }
  159. delay(1000);
  160. }
  161. //---------------------------------------------------
  162. void get_new_pass()
  163. {
  164. new_pass_entry();
  165. int entrada = 0; // inicializa entrada
  166. while(count < 4) // enquanto contrador for menor que 4
  167. {
  168. char key = keypad.getKey(); // obtem informação do teclado
  169. if(key != NO_KEY) // se algo foi teclado
  170. {
  171. Serial.println("rotina get_new_pass");
  172. entrada += 1; // aumenta contador de entrada
  173. tone(audioPin, 1080, 100); // tom para cada dígito
  174. delay(duration);
  175. noTone(audioPin);
  176. passwordEeprom[count] = key; // aramazena o novo dígito
  177. count += 1; // próximo dígito
  178. if(count == 4)
  179. Serial.print("addr ");
  180. Serial.println(addr);
  181. Serial.print("key ");
  182. Serial.println(key);
  183. // EEPROM.write(addr, key-48);
  184. EEPROM.write(addr, key);
  185.  
  186. addr += 1;
  187.  
  188. if((key == '*') || (entrada == 4))
  189. {
  190. delay(500);
  191. // lcd.clear();
  192. // lcd.print(" Senha OK");// foi telcado * 4 entradas
  193. Serial.println(" Senha OK");
  194. delay(500);
  195. }
  196. }
  197. }
  198. }
  199. //---------------------------------------------------
  200. void new_pass_entry()
  201. {
  202. // mensagem no display
  203. // lcd.clear();
  204. // lcd.print(" Nova Senha");
  205. Serial.println(" Senha OK");
  206. count = 0;
  207. // lcd.setCursor(0,1);
  208. // lcd.print(" Tecle *");
  209. Serial.println(" Tecle *");
  210. // gera sinal audível
  211. tone(audioPin, 1500, 100);
  212. delay(duration);
  213. noTone(audioPin);
  214. tone(audioPin, 1500, 100);
  215. delay(duration);
  216. noTone(audioPin);
  217. tone(audioPin, 1500, 100);
  218. delay(duration);
  219. noTone(audioPin);
  220. }
  221. //---------------------------------------------------
  222. void old_pass_check()
  223. {
  224. // mensagem no display
  225. // lcd.clear();
  226. // lcd.print(" Senha antiga?");
  227. Serial.println(" Senha antiga?");
  228. count = 0;
  229. // lcd.setCursor(0,1);
  230. // lcd.print(" Tecle *");
  231. Serial.println(" Tecle *");
  232. // gera tom audível
  233. tone(audioPin, 1500, 100);
  234. delay(duration);
  235. noTone(audioPin);
  236. tone(audioPin, 1500, 100);
  237. delay(duration);
  238. noTone(audioPin);
  239. tone(audioPin, 1500, 100);
  240. delay(duration);
  241. noTone(audioPin);
  242. }
  243. //---------------------------------------------------
  244. void key_init ()
  245. {
  246. // mensagem no display
  247. // lcd.clear();
  248. // lcd.print(" Bem vindo...");
  249. Serial.println(" Bem vindo...");
  250. // lcd.setCursor(0,1);
  251. // lcd.print(" Tecle: # ou *");
  252. Serial.println(" Tecle: # ou *");
  253. count = 0;// contador para zero
  254. tone(audioPin, 1080, 100);
  255. delay(duration);
  256. noTone(audioPin);
  257. tone(audioPin, 980, 100);
  258. delay(duration);
  259. noTone(audioPin);
  260. tone(audioPin, 770, 100);
  261. delay(duration);
  262. noTone(audioPin);
  263. }
  264. //---------------------------------------------------
  265. void code_entry_init()
  266. {
  267. // mensagem no display
  268. // lcd.clear();
  269. // lcd.print(" Entre a Senha:");
  270. Serial.println(" Entre a Senha:");
  271. count = 0; // contador para zero
  272. // gera sinal audível
  273. tone(audioPin, 1500, 100);
  274. delay(duration);
  275. noTone(audioPin);
  276. tone(audioPin, 1500, 100);
  277. delay(duration);
  278. noTone(audioPin);
  279. tone(audioPin, 1500, 100);
  280. delay(duration);
  281. noTone(audioPin);
  282. }
  283. //---------------------------------------------------
  284. void unlocked()
  285. {
  286. // mensagem no display
  287. // lcd.clear();
  288. // lcd.print("Acesso Liberado!");
  289. Serial.println("Acesso Liberado!");
  290. servo1.write(25);
  291. // pisca LED verde e gera sinal audível
  292. for (int x = 0; x < 5; x++)
  293. {
  294. //digitalWrite(greenPin, HIGH);
  295. tone(audioPin, 2000, 100);
  296. delay(duration);
  297. noTone(audioPin);
  298. //digitalWrite(greenPin, LOW);
  299. tone(audioPin, 2000, 100);
  300. delay(duration);
  301. noTone(audioPin);
  302. delay(250);
  303. }
  304. }
  305. //----------------------------------------------
  306. void LeSenhaGravada()
  307. {
  308. for (int i = 0 ; i < 4; i ++)
  309. {
  310. //Serial.println(EEPROM.read(i));
  311. passwordEeprom[i] = EEPROM.read(i);
  312. }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement