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