Completo

Dependencies:   mbed Servo KeypadLib TextLCD

Committer:
Virgi97
Date:
Sun Dec 09 22:01:57 2018 +0000
Revision:
1:0661b733846e
Child:
2:ca52e00afb99
Il codice implementa il buzzer attivo monotonale

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Virgi97 1:0661b733846e 1 #include "Cassaforte.h"
Virgi97 1:0661b733846e 2 #include "TextLCD.h"
Virgi97 1:0661b733846e 3 #include "Servo.h"
Virgi97 1:0661b733846e 4 #include "keypad.h"
Virgi97 1:0661b733846e 5 I2C i2c(D14, D15);
Virgi97 1:0661b733846e 6 TextLCD_I2C lcd(&i2c, 0x4E, TextLCD_I2C::LCD16x2, TextLCD_I2C::HD44780); //comunicazione Nucleo-display
Virgi97 1:0661b733846e 7 Keypad keypad(D6,D5,D4,D3,D10,D9,D8,D7); //porte del tastierino
Virgi97 1:0661b733846e 8 Servo servo(D11); //porta servo
Virgi97 1:0661b733846e 9 //PwmOut buzzer(D2);
Virgi97 1:0661b733846e 10 //Ticker t;
Virgi97 1:0661b733846e 11 Timeout t1;
Virgi97 1:0661b733846e 12 DigitalOut led_verde(D13); //led apertura
Virgi97 1:0661b733846e 13 DigitalOut led_rosso(D12); //led chiusura
Virgi97 1:0661b733846e 14 DigitalOut buz(D2); //porta buz
Virgi97 1:0661b733846e 15 DigitalIn mag(PC_13); //porta del magnete
Virgi97 1:0661b733846e 16 int dim=4;
Virgi97 1:0661b733846e 17 //int buz=0;
Virgi97 1:0661b733846e 18 char pass[4]={'1','2','3','4'};//password di default (è possibile cambiarla dal menu)
Virgi97 1:0661b733846e 19 bool alonoff=true; //variabile booleana per abilitare o disabilitare l'allarme in caso di apertura e chiusura (?)
Virgi97 1:0661b733846e 20 bool jump = false;
Virgi97 1:0661b733846e 21
Virgi97 1:0661b733846e 22 void inizializza(){
Virgi97 1:0661b733846e 23 lcd.setMode(TextLCD_I2C::DispOn);
Virgi97 1:0661b733846e 24 lcd.setBacklight(TextLCD_I2C::LightOff);
Virgi97 1:0661b733846e 25 buz=0;
Virgi97 1:0661b733846e 26
Virgi97 1:0661b733846e 27 if(mag == 1){
Virgi97 1:0661b733846e 28 chiuso();
Virgi97 1:0661b733846e 29 //wait(0.3);
Virgi97 1:0661b733846e 30 } else aperto();
Virgi97 1:0661b733846e 31 //lcd.setCursor(TextLCD_I2C::CurOn_BlkOn);
Virgi97 1:0661b733846e 32 }
Virgi97 1:0661b733846e 33
Virgi97 1:0661b733846e 34 void scrivi(char parola[]){
Virgi97 1:0661b733846e 35 lcd.cls();
Virgi97 1:0661b733846e 36 lcd.printf("%s\n", parola);
Virgi97 1:0661b733846e 37 }
Virgi97 1:0661b733846e 38
Virgi97 1:0661b733846e 39 void chiuso(){
Virgi97 1:0661b733846e 40 servo.position(90);
Virgi97 1:0661b733846e 41 wait(0.3);
Virgi97 1:0661b733846e 42 led_rosso = 1;
Virgi97 1:0661b733846e 43 led_verde = 0;
Virgi97 1:0661b733846e 44 }
Virgi97 1:0661b733846e 45
Virgi97 1:0661b733846e 46 void aperto(){
Virgi97 1:0661b733846e 47 servo.position(-90);
Virgi97 1:0661b733846e 48 wait(0.3);
Virgi97 1:0661b733846e 49 led_rosso = 0;
Virgi97 1:0661b733846e 50 led_verde = 1;
Virgi97 1:0661b733846e 51 }
Virgi97 1:0661b733846e 52 void inserimento(char a[],int n,bool vis){
Virgi97 1:0661b733846e 53 int i=0;
Virgi97 1:0661b733846e 54 jump = false;
Virgi97 1:0661b733846e 55 /*Timer t;
Virgi97 1:0661b733846e 56 t.reset();
Virgi97 1:0661b733846e 57 t.start();*/
Virgi97 1:0661b733846e 58 t1.attach(&salta, 20);
Virgi97 1:0661b733846e 59 do{
Virgi97 1:0661b733846e 60 keypad.enablePullUp();
Virgi97 1:0661b733846e 61 char val = keypad.getKey();
Virgi97 1:0661b733846e 62 if(/*t.read()>20*/jump == true && buz==0){ //CHIUSURA dopo 20 secondi dal mancato inserimento
Virgi97 1:0661b733846e 63 main();
Virgi97 1:0661b733846e 64 }
Virgi97 1:0661b733846e 65 if(val != KEY_RELEASED){
Virgi97 1:0661b733846e 66 if(vis==true){
Virgi97 1:0661b733846e 67 lcd.putc(val);
Virgi97 1:0661b733846e 68 lcd.setAddress(i+1,1);
Virgi97 1:0661b733846e 69 }
Virgi97 1:0661b733846e 70 a[i]=val;
Virgi97 1:0661b733846e 71 i++;
Virgi97 1:0661b733846e 72 wait(0.4);
Virgi97 1:0661b733846e 73
Virgi97 1:0661b733846e 74 }
Virgi97 1:0661b733846e 75 }while(i<n);
Virgi97 1:0661b733846e 76
Virgi97 1:0661b733846e 77 }
Virgi97 1:0661b733846e 78
Virgi97 1:0661b733846e 79 bool confronto(char v[],int dim){
Virgi97 1:0661b733846e 80 int cont=0;
Virgi97 1:0661b733846e 81 for(int i=0;i<dim;i++){
Virgi97 1:0661b733846e 82 if (v[i]==pass[i]){
Virgi97 1:0661b733846e 83 cont++;
Virgi97 1:0661b733846e 84 }
Virgi97 1:0661b733846e 85 }
Virgi97 1:0661b733846e 86 if (cont==4){
Virgi97 1:0661b733846e 87 return true;
Virgi97 1:0661b733846e 88 }
Virgi97 1:0661b733846e 89 else{
Virgi97 1:0661b733846e 90 return false;
Virgi97 1:0661b733846e 91 }
Virgi97 1:0661b733846e 92 }
Virgi97 1:0661b733846e 93
Virgi97 1:0661b733846e 94 void menu(){
Virgi97 1:0661b733846e 95 scrivi("A Apri B BloccaC Cambia codice");
Virgi97 1:0661b733846e 96 char val;
Virgi97 1:0661b733846e 97 do{
Virgi97 1:0661b733846e 98 inserimento(&val,1,false);
Virgi97 1:0661b733846e 99
Virgi97 1:0661b733846e 100 }while(val!='A' && val!='B' && val!='C');
Virgi97 1:0661b733846e 101
Virgi97 1:0661b733846e 102 if(val=='A'){
Virgi97 1:0661b733846e 103 //if(mag==1){
Virgi97 1:0661b733846e 104 scrivi("Apertura...");
Virgi97 1:0661b733846e 105 alonoff=false;
Virgi97 1:0661b733846e 106 aperto();
Virgi97 1:0661b733846e 107 wait(0.3); //2
Virgi97 1:0661b733846e 108 menu();
Virgi97 1:0661b733846e 109 // }
Virgi97 1:0661b733846e 110 }
Virgi97 1:0661b733846e 111 if(val=='B'){
Virgi97 1:0661b733846e 112 if(mag==0){
Virgi97 1:0661b733846e 113 scrivi("Chiusura...");
Virgi97 1:0661b733846e 114 alonoff=true;
Virgi97 1:0661b733846e 115 chiuso();
Virgi97 1:0661b733846e 116 wait(0.3); //2
Virgi97 1:0661b733846e 117 main();
Virgi97 1:0661b733846e 118 }
Virgi97 1:0661b733846e 119 }
Virgi97 1:0661b733846e 120 if(val=='C'){
Virgi97 1:0661b733846e 121 char v[dim];
Virgi97 1:0661b733846e 122 scrivi("Nuovo codice:");
Virgi97 1:0661b733846e 123 inserimento(v,dim,true);
Virgi97 1:0661b733846e 124 scrivi("Codice: ");
Virgi97 1:0661b733846e 125 for(int i=0;i<4;i++){
Virgi97 1:0661b733846e 126 lcd.setAddress(i+12,0);
Virgi97 1:0661b733846e 127 lcd.putc(v[i]);
Virgi97 1:0661b733846e 128 }
Virgi97 1:0661b733846e 129 lcd.printf("A:ok C:annulla");
Virgi97 1:0661b733846e 130 do{
Virgi97 1:0661b733846e 131 inserimento(&val,1,false);
Virgi97 1:0661b733846e 132 if(val=='A'){
Virgi97 1:0661b733846e 133 scrivi("Codice cambiato.");
Virgi97 1:0661b733846e 134 wait(1);
Virgi97 1:0661b733846e 135 for(int k=0;k<dim;k++){
Virgi97 1:0661b733846e 136 pass[k]=v[k];
Virgi97 1:0661b733846e 137 }
Virgi97 1:0661b733846e 138 main();
Virgi97 1:0661b733846e 139 }
Virgi97 1:0661b733846e 140 if(val=='C'){
Virgi97 1:0661b733846e 141 menu();
Virgi97 1:0661b733846e 142 }
Virgi97 1:0661b733846e 143
Virgi97 1:0661b733846e 144 }while(1);
Virgi97 1:0661b733846e 145 }
Virgi97 1:0661b733846e 146 }
Virgi97 1:0661b733846e 147
Virgi97 1:0661b733846e 148 void allarme(){
Virgi97 1:0661b733846e 149 bool esci = true;
Virgi97 1:0661b733846e 150 char insa[4];
Virgi97 1:0661b733846e 151 //t.attach(&suona, 1);
Virgi97 1:0661b733846e 152 do{
Virgi97 1:0661b733846e 153 buz=1;
Virgi97 1:0661b733846e 154 scrivi(" Allarme!");
Virgi97 1:0661b733846e 155 inserimento(insa,4,true);
Virgi97 1:0661b733846e 156
Virgi97 1:0661b733846e 157
Virgi97 1:0661b733846e 158 if(confronto(insa,4)==true){
Virgi97 1:0661b733846e 159 buz=0;
Virgi97 1:0661b733846e 160 esci = false;
Virgi97 1:0661b733846e 161 //t.detach;
Virgi97 1:0661b733846e 162 }
Virgi97 1:0661b733846e 163 }while(esci);
Virgi97 1:0661b733846e 164 //scrivi("Attendere...");
Virgi97 1:0661b733846e 165 t1.attach(&salta, 0.5);
Virgi97 1:0661b733846e 166 }
Virgi97 1:0661b733846e 167
Virgi97 1:0661b733846e 168 void controllo(){
Virgi97 1:0661b733846e 169 if (alonoff == true){
Virgi97 1:0661b733846e 170 allarme();
Virgi97 1:0661b733846e 171 }
Virgi97 1:0661b733846e 172 }
Virgi97 1:0661b733846e 173
Virgi97 1:0661b733846e 174 /*void suona (float freq1, float freq2, float t1, float t2 float v[]){
Virgi97 1:0661b733846e 175 buzzer.period_ms(3);
Virgi97 1:0661b733846e 176 buzzer.write(0.1);
Virgi97 1:0661b733846e 177 wait(0.5);
Virgi97 1:0661b733846e 178 buzzer.write(0.5);
Virgi97 1:0661b733846e 179 wait(0.5);
Virgi97 1:0661b733846e 180 }*/
Virgi97 1:0661b733846e 181
Virgi97 1:0661b733846e 182 void salta(){
Virgi97 1:0661b733846e 183 jump = true;
Virgi97 1:0661b733846e 184 }