A security system that detects the movement of an object in a box using IR distance sensors
Dependencies: 4DGL-uLCD-SE Camera_LS_Y201 SDFileSystem mbed wave_player
main.cpp@0:a729b4a52c27, 2014-12-05 (annotated)
- Committer:
- tdouglas6
- Date:
- Fri Dec 05 00:33:13 2014 +0000
- Revision:
- 0:a729b4a52c27
- Child:
- 1:44af7a01e1e0
gjgvjv
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tdouglas6 | 0:a729b4a52c27 | 1 | #include "mbed.h" |
tdouglas6 | 0:a729b4a52c27 | 2 | #include "mpr121.h" |
tdouglas6 | 0:a729b4a52c27 | 3 | #include "SDFileSystem.h" |
tdouglas6 | 0:a729b4a52c27 | 4 | #include "wave_player.h" |
tdouglas6 | 0:a729b4a52c27 | 5 | #include "uLCD_4DGL.h" |
tdouglas6 | 0:a729b4a52c27 | 6 | |
tdouglas6 | 0:a729b4a52c27 | 7 | |
tdouglas6 | 0:a729b4a52c27 | 8 | DigitalOut led1(LED1); |
tdouglas6 | 0:a729b4a52c27 | 9 | DigitalOut led2(LED2); |
tdouglas6 | 0:a729b4a52c27 | 10 | DigitalOut led3(LED3); |
tdouglas6 | 0:a729b4a52c27 | 11 | DigitalOut led4(LED4); |
tdouglas6 | 0:a729b4a52c27 | 12 | |
tdouglas6 | 0:a729b4a52c27 | 13 | Serial device(p13, p14); // tx, rx |
tdouglas6 | 0:a729b4a52c27 | 14 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
tdouglas6 | 0:a729b4a52c27 | 15 | //AnalogOut DACout(p18); |
tdouglas6 | 0:a729b4a52c27 | 16 | //wave_player waver(&DACout); |
tdouglas6 | 0:a729b4a52c27 | 17 | |
tdouglas6 | 0:a729b4a52c27 | 18 | //analog input for four motion sensors |
tdouglas6 | 0:a729b4a52c27 | 19 | AnalogIn s1(p19); |
tdouglas6 | 0:a729b4a52c27 | 20 | AnalogIn s2(p20); |
tdouglas6 | 0:a729b4a52c27 | 21 | |
tdouglas6 | 0:a729b4a52c27 | 22 | uLCD_4DGL uLCD(p28, p27, p11); // create a global lcd object |
tdouglas6 | 0:a729b4a52c27 | 23 | |
tdouglas6 | 0:a729b4a52c27 | 24 | // Create the interrupt receiver object on pin 26 |
tdouglas6 | 0:a729b4a52c27 | 25 | InterruptIn interrupt(p26); |
tdouglas6 | 0:a729b4a52c27 | 26 | // Setup the i2c bus on pins 9 and 10 |
tdouglas6 | 0:a729b4a52c27 | 27 | I2C i2c(p9, p10); |
tdouglas6 | 0:a729b4a52c27 | 28 | // Setup the Mpr121: |
tdouglas6 | 0:a729b4a52c27 | 29 | // constructor(i2c object, i2c address of the mpr121) |
tdouglas6 | 0:a729b4a52c27 | 30 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
tdouglas6 | 0:a729b4a52c27 | 31 | |
tdouglas6 | 0:a729b4a52c27 | 32 | int count; |
tdouglas6 | 0:a729b4a52c27 | 33 | int passcode=0; |
tdouglas6 | 0:a729b4a52c27 | 34 | int miss=0; |
tdouglas6 | 0:a729b4a52c27 | 35 | int strikes=0; |
tdouglas6 | 0:a729b4a52c27 | 36 | |
tdouglas6 | 0:a729b4a52c27 | 37 | // voice recognition interrupt |
tdouglas6 | 0:a729b4a52c27 | 38 | void microInterrupt() { |
tdouglas6 | 0:a729b4a52c27 | 39 | // FILE *alarm; |
tdouglas6 | 0:a729b4a52c27 | 40 | // FILE *tryagain; |
tdouglas6 | 0:a729b4a52c27 | 41 | // FILE *notaccept; |
tdouglas6 | 0:a729b4a52c27 | 42 | // alarm=fopen("/sd/wavfiles/BurglarAlarm.wav","r"); |
tdouglas6 | 0:a729b4a52c27 | 43 | // tryagain=fopen("/sd/wavfiles/tryagain.wav","r"); |
tdouglas6 | 0:a729b4a52c27 | 44 | // notaccept=fopen("/sd/wavfiles/notaccept.wav","r"); |
tdouglas6 | 0:a729b4a52c27 | 45 | |
tdouglas6 | 0:a729b4a52c27 | 46 | strikes=0; |
tdouglas6 | 0:a729b4a52c27 | 47 | char rchar=0; |
tdouglas6 | 0:a729b4a52c27 | 48 | //wake up device - needs more work and a timeout |
tdouglas6 | 0:a729b4a52c27 | 49 | device.putc('b'); |
tdouglas6 | 0:a729b4a52c27 | 50 | while (device.getc()!='o') { |
tdouglas6 | 0:a729b4a52c27 | 51 | device.putc('b'); |
tdouglas6 | 0:a729b4a52c27 | 52 | led1 = 1; |
tdouglas6 | 0:a729b4a52c27 | 53 | wait(0.2); |
tdouglas6 | 0:a729b4a52c27 | 54 | } |
tdouglas6 | 0:a729b4a52c27 | 55 | led2=1; |
tdouglas6 | 0:a729b4a52c27 | 56 | while (1) { |
tdouglas6 | 0:a729b4a52c27 | 57 | device.putc('i'); //Start Recognition |
tdouglas6 | 0:a729b4a52c27 | 58 | device.putc('B'); //Use Wordset 1 - words |
tdouglas6 | 0:a729b4a52c27 | 59 | //Use built-in speaker independent words and listen for words |
tdouglas6 | 0:a729b4a52c27 | 60 | while (device.readable()!=0) {} |
tdouglas6 | 0:a729b4a52c27 | 61 | if (device.getc()=='s') { |
tdouglas6 | 0:a729b4a52c27 | 62 | device.putc(' '); |
tdouglas6 | 0:a729b4a52c27 | 63 | rchar=device.getc(); |
tdouglas6 | 0:a729b4a52c27 | 64 | if (rchar=='H') { |
tdouglas6 | 0:a729b4a52c27 | 65 | led1=!led1; |
tdouglas6 | 0:a729b4a52c27 | 66 | uLCD.cls(); |
tdouglas6 | 0:a729b4a52c27 | 67 | uLCD.locate(1,8); |
tdouglas6 | 0:a729b4a52c27 | 68 | uLCD.printf("\nVOICE COMMAND CORRECT.\n"); |
tdouglas6 | 0:a729b4a52c27 | 69 | } else { |
tdouglas6 | 0:a729b4a52c27 | 70 | strikes++; |
tdouglas6 | 0:a729b4a52c27 | 71 | // waver.play(notaccept); |
tdouglas6 | 0:a729b4a52c27 | 72 | // fclose(notaccept); |
tdouglas6 | 0:a729b4a52c27 | 73 | // waver.play(tryagain); |
tdouglas6 | 0:a729b4a52c27 | 74 | // fclose(tryagain); |
tdouglas6 | 0:a729b4a52c27 | 75 | if (strikes == 3) { |
tdouglas6 | 0:a729b4a52c27 | 76 | //SOUND OFF ALARM |
tdouglas6 | 0:a729b4a52c27 | 77 | led2=0; |
tdouglas6 | 0:a729b4a52c27 | 78 | led3=0; |
tdouglas6 | 0:a729b4a52c27 | 79 | uLCD.locate(1,8); |
tdouglas6 | 0:a729b4a52c27 | 80 | uLCD.printf("\nToo many invalid passcodes.\n INTRUDER ALERT!\n"); |
tdouglas6 | 0:a729b4a52c27 | 81 | wait(1); |
tdouglas6 | 0:a729b4a52c27 | 82 | uLCD.cls(); |
tdouglas6 | 0:a729b4a52c27 | 83 | uLCD.text_width(3); //4X size text |
tdouglas6 | 0:a729b4a52c27 | 84 | uLCD.text_height(3); |
tdouglas6 | 0:a729b4a52c27 | 85 | uLCD.locate(0,0); |
tdouglas6 | 0:a729b4a52c27 | 86 | uLCD.printf("Alerting Authorities!"); |
tdouglas6 | 0:a729b4a52c27 | 87 | // waver.play(alarm); |
tdouglas6 | 0:a729b4a52c27 | 88 | // fclose(alarm); |
tdouglas6 | 0:a729b4a52c27 | 89 | } |
tdouglas6 | 0:a729b4a52c27 | 90 | } |
tdouglas6 | 0:a729b4a52c27 | 91 | } |
tdouglas6 | 0:a729b4a52c27 | 92 | } |
tdouglas6 | 0:a729b4a52c27 | 93 | } |
tdouglas6 | 0:a729b4a52c27 | 94 | |
tdouglas6 | 0:a729b4a52c27 | 95 | // Disarm alarm interrupt routine |
tdouglas6 | 0:a729b4a52c27 | 96 | void disarmInterrupt() { |
tdouglas6 | 0:a729b4a52c27 | 97 | //create wave file |
tdouglas6 | 0:a729b4a52c27 | 98 | // FILE *alarm; |
tdouglas6 | 0:a729b4a52c27 | 99 | // FILE *password; |
tdouglas6 | 0:a729b4a52c27 | 100 | // alarm=fopen("/sd/wavfiles/BurglarAlarm.wav","r"); |
tdouglas6 | 0:a729b4a52c27 | 101 | // password=fopen("/sd/wavfiles/pwaccepted.wav","r"); |
tdouglas6 | 0:a729b4a52c27 | 102 | uLCD.cls(); |
tdouglas6 | 0:a729b4a52c27 | 103 | int i=0; |
tdouglas6 | 0:a729b4a52c27 | 104 | int value=mpr121.read(0x00); |
tdouglas6 | 0:a729b4a52c27 | 105 | value +=mpr121.read(0x01)<<8; |
tdouglas6 | 0:a729b4a52c27 | 106 | // LED demo mod |
tdouglas6 | 0:a729b4a52c27 | 107 | i=0; |
tdouglas6 | 0:a729b4a52c27 | 108 | // puts key number out to LEDs for demo |
tdouglas6 | 0:a729b4a52c27 | 109 | for (i=0; i<12; i++) { |
tdouglas6 | 0:a729b4a52c27 | 110 | if (value & (1<<i)) |
tdouglas6 | 0:a729b4a52c27 | 111 | break; |
tdouglas6 | 0:a729b4a52c27 | 112 | } |
tdouglas6 | 0:a729b4a52c27 | 113 | |
tdouglas6 | 0:a729b4a52c27 | 114 | uLCD.text_width(4); //4X size text |
tdouglas6 | 0:a729b4a52c27 | 115 | uLCD.text_height(4); |
tdouglas6 | 0:a729b4a52c27 | 116 | uLCD.color(RED); |
tdouglas6 | 0:a729b4a52c27 | 117 | uLCD.locate(2,1); |
tdouglas6 | 0:a729b4a52c27 | 118 | |
tdouglas6 | 0:a729b4a52c27 | 119 | if (i == 0) { |
tdouglas6 | 0:a729b4a52c27 | 120 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 121 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 122 | } else if( i == 1) { |
tdouglas6 | 0:a729b4a52c27 | 123 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 124 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 125 | } else if( i == 2) { |
tdouglas6 | 0:a729b4a52c27 | 126 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 127 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 128 | } else if(i == 3) { |
tdouglas6 | 0:a729b4a52c27 | 129 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 130 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 131 | } else if(i == 4) { |
tdouglas6 | 0:a729b4a52c27 | 132 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 133 | miss++; |
tdouglas6 | 0:a729b4a52c27 | 134 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 135 | } else if(i == 5) { |
tdouglas6 | 0:a729b4a52c27 | 136 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 137 | miss++; |
tdouglas6 | 0:a729b4a52c27 | 138 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 139 | } else if(i == 6) { |
tdouglas6 | 0:a729b4a52c27 | 140 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 141 | miss++; |
tdouglas6 | 0:a729b4a52c27 | 142 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 143 | } else if(i == 7) { |
tdouglas6 | 0:a729b4a52c27 | 144 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 145 | miss++; |
tdouglas6 | 0:a729b4a52c27 | 146 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 147 | } else if(i == 8) { |
tdouglas6 | 0:a729b4a52c27 | 148 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 149 | miss++; |
tdouglas6 | 0:a729b4a52c27 | 150 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 151 | } else if(i == 9) { |
tdouglas6 | 0:a729b4a52c27 | 152 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 153 | miss++; |
tdouglas6 | 0:a729b4a52c27 | 154 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 155 | } else if(i == 10) { |
tdouglas6 | 0:a729b4a52c27 | 156 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 157 | miss++; |
tdouglas6 | 0:a729b4a52c27 | 158 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 159 | } else if(i == 11) { |
tdouglas6 | 0:a729b4a52c27 | 160 | uLCD.printf("%d", i); |
tdouglas6 | 0:a729b4a52c27 | 161 | miss++; |
tdouglas6 | 0:a729b4a52c27 | 162 | passcode++; |
tdouglas6 | 0:a729b4a52c27 | 163 | } |
tdouglas6 | 0:a729b4a52c27 | 164 | |
tdouglas6 | 0:a729b4a52c27 | 165 | if (passcode==4) { |
tdouglas6 | 0:a729b4a52c27 | 166 | if(miss==0) { |
tdouglas6 | 0:a729b4a52c27 | 167 | //CHECKPOINT |
tdouglas6 | 0:a729b4a52c27 | 168 | uLCD.cls(); |
tdouglas6 | 0:a729b4a52c27 | 169 | uLCD.text_width(1); //4X size text |
tdouglas6 | 0:a729b4a52c27 | 170 | uLCD.text_height(1); |
tdouglas6 | 0:a729b4a52c27 | 171 | uLCD.color(GREEN); |
tdouglas6 | 0:a729b4a52c27 | 172 | uLCD.locate(1,8); |
tdouglas6 | 0:a729b4a52c27 | 173 | uLCD.printf("\nPASSCODE CORRECT.\n"); |
tdouglas6 | 0:a729b4a52c27 | 174 | // waver.play(password); |
tdouglas6 | 0:a729b4a52c27 | 175 | // fclose(password); |
tdouglas6 | 0:a729b4a52c27 | 176 | wait(1); |
tdouglas6 | 0:a729b4a52c27 | 177 | uLCD.cls(); |
tdouglas6 | 0:a729b4a52c27 | 178 | uLCD.locate(1,2); |
tdouglas6 | 0:a729b4a52c27 | 179 | uLCD.printf("\nSay Voice Command:\n"); |
tdouglas6 | 0:a729b4a52c27 | 180 | microInterrupt(); |
tdouglas6 | 0:a729b4a52c27 | 181 | |
tdouglas6 | 0:a729b4a52c27 | 182 | } else { |
tdouglas6 | 0:a729b4a52c27 | 183 | // uLCD.cls(); |
tdouglas6 | 0:a729b4a52c27 | 184 | // uLCD.text_width(1); //4X size text |
tdouglas6 | 0:a729b4a52c27 | 185 | // uLCD.text_height(1); |
tdouglas6 | 0:a729b4a52c27 | 186 | // uLCD.locate(1,8); |
tdouglas6 | 0:a729b4a52c27 | 187 | // uLCD.printf("\nPasscode invalid. Try again\n"); |
tdouglas6 | 0:a729b4a52c27 | 188 | miss=0; |
tdouglas6 | 0:a729b4a52c27 | 189 | passcode=0; |
tdouglas6 | 0:a729b4a52c27 | 190 | strikes++; |
tdouglas6 | 0:a729b4a52c27 | 191 | wait(2); |
tdouglas6 | 0:a729b4a52c27 | 192 | |
tdouglas6 | 0:a729b4a52c27 | 193 | //ALARM GOES OFF |
tdouglas6 | 0:a729b4a52c27 | 194 | if(strikes==3) { |
tdouglas6 | 0:a729b4a52c27 | 195 | uLCD.text_width(1); //4X size text |
tdouglas6 | 0:a729b4a52c27 | 196 | uLCD.text_height(1); |
tdouglas6 | 0:a729b4a52c27 | 197 | uLCD.locate(1,8); |
tdouglas6 | 0:a729b4a52c27 | 198 | uLCD.printf("\nToo many invalid passcodes.\n INTRUDER ALERT!\n"); |
tdouglas6 | 0:a729b4a52c27 | 199 | wait(1); |
tdouglas6 | 0:a729b4a52c27 | 200 | uLCD.cls(); |
tdouglas6 | 0:a729b4a52c27 | 201 | uLCD.text_width(3); //4X size text |
tdouglas6 | 0:a729b4a52c27 | 202 | uLCD.text_height(3); |
tdouglas6 | 0:a729b4a52c27 | 203 | uLCD.locate(0,0); |
tdouglas6 | 0:a729b4a52c27 | 204 | uLCD.printf("Alerting Authorities!"); |
tdouglas6 | 0:a729b4a52c27 | 205 | // waver.play(alarm); |
tdouglas6 | 0:a729b4a52c27 | 206 | // fclose(alarm); |
tdouglas6 | 0:a729b4a52c27 | 207 | } |
tdouglas6 | 0:a729b4a52c27 | 208 | } |
tdouglas6 | 0:a729b4a52c27 | 209 | } |
tdouglas6 | 0:a729b4a52c27 | 210 | } |
tdouglas6 | 0:a729b4a52c27 | 211 | |
tdouglas6 | 0:a729b4a52c27 | 212 | int main() { |
tdouglas6 | 0:a729b4a52c27 | 213 | // FILE *alarm; |
tdouglas6 | 0:a729b4a52c27 | 214 | // alarm=fopen("/sd/wavfiles/BurglarAlarm.wav","r"); |
tdouglas6 | 0:a729b4a52c27 | 215 | uLCD.printf("WARNING:\nThis security system is currently on. To turn off the alarm..."); |
tdouglas6 | 0:a729b4a52c27 | 216 | wait(3); |
tdouglas6 | 0:a729b4a52c27 | 217 | uLCD.cls(); |
tdouglas6 | 0:a729b4a52c27 | 218 | uLCD.locate(1,2); |
tdouglas6 | 0:a729b4a52c27 | 219 | uLCD.printf("\nEnter passcode:\n"); |
tdouglas6 | 0:a729b4a52c27 | 220 | |
tdouglas6 | 0:a729b4a52c27 | 221 | interrupt.fall(&disarmInterrupt); |
tdouglas6 | 0:a729b4a52c27 | 222 | interrupt.mode(PullUp); |
tdouglas6 | 0:a729b4a52c27 | 223 | |
tdouglas6 | 0:a729b4a52c27 | 224 | while (1) { |
tdouglas6 | 0:a729b4a52c27 | 225 | if(s1 > 0.3) { |
tdouglas6 | 0:a729b4a52c27 | 226 | led4 = 1; |
tdouglas6 | 0:a729b4a52c27 | 227 | } else { |
tdouglas6 | 0:a729b4a52c27 | 228 | //ALARM GOES OFF |
tdouglas6 | 0:a729b4a52c27 | 229 | led4 = 0; |
tdouglas6 | 0:a729b4a52c27 | 230 | // waver.play(alarm); |
tdouglas6 | 0:a729b4a52c27 | 231 | // fclose(alarm); |
tdouglas6 | 0:a729b4a52c27 | 232 | } |
tdouglas6 | 0:a729b4a52c27 | 233 | } |
tdouglas6 | 0:a729b4a52c27 | 234 | } |