IR Sensor Security System using a touch keypad, micro SD reader and a speaker.

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Committer:
coconnell7
Date:
Fri Mar 13 03:49:26 2015 +0000
Revision:
0:038ed656f129
1st Revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coconnell7 0:038ed656f129 1 #include "mbed.h"
coconnell7 0:038ed656f129 2 #include "rtos.h"
coconnell7 0:038ed656f129 3
coconnell7 0:038ed656f129 4 #include "uLCD_4DGL.h" // uLCD
coconnell7 0:038ed656f129 5 #include "wave_player.h" // Speaker
coconnell7 0:038ed656f129 6 #include "SDFileSystem.h" // SD card
coconnell7 0:038ed656f129 7 #include <mpr121.h> // Touchpad
coconnell7 0:038ed656f129 8
coconnell7 0:038ed656f129 9
coconnell7 0:038ed656f129 10 bool ArmedOnOff; // Global variable to Arm the alarm;
coconnell7 0:038ed656f129 11 int CurrentKey; // Global variable to display the current key value to the LCD
coconnell7 0:038ed656f129 12 int OldCurrentKey;
coconnell7 0:038ed656f129 13 int KeyCount = 0; //Keeps track of how many times in a row the key has been pressed
coconnell7 0:038ed656f129 14 int Trigger = 1; //Determining if the alarm has been triggered
coconnell7 0:038ed656f129 15 DigitalOut led1(LED1);
coconnell7 0:038ed656f129 16 DigitalOut led2(LED2);
coconnell7 0:038ed656f129 17 DigitalOut led3(LED3);
coconnell7 0:038ed656f129 18 DigitalOut led4(LED4);
coconnell7 0:038ed656f129 19
coconnell7 0:038ed656f129 20 int num1; //The four digits of the passcode
coconnell7 0:038ed656f129 21 int num2;
coconnell7 0:038ed656f129 22 int num3;
coconnell7 0:038ed656f129 23 int num4;
coconnell7 0:038ed656f129 24
coconnell7 0:038ed656f129 25 int Safety = 0; //Counter to check if correct passcode is entered
coconnell7 0:038ed656f129 26
coconnell7 0:038ed656f129 27
coconnell7 0:038ed656f129 28 SDFileSystem sd(p5, p6, p7, p8, "sd"); // MicroSD card
coconnell7 0:038ed656f129 29
coconnell7 0:038ed656f129 30 AnalogIn IRout(p20); // IR Module
coconnell7 0:038ed656f129 31
coconnell7 0:038ed656f129 32 AnalogOut DACout(p18); // Speaker / Wave Player
coconnell7 0:038ed656f129 33 wave_player waver(&DACout); // Speaker / Wave player
coconnell7 0:038ed656f129 34
coconnell7 0:038ed656f129 35 uLCD_4DGL uLCD(p28, p27, p29); // used to create LCD object
coconnell7 0:038ed656f129 36
coconnell7 0:038ed656f129 37 Mutex stdio_mutex; // Mutex for LCD thread
coconnell7 0:038ed656f129 38
coconnell7 0:038ed656f129 39 InterruptIn interrupt(p26); // Create interrupt receiver object on pin 26
coconnell7 0:038ed656f129 40
coconnell7 0:038ed656f129 41 I2C i2c(p9, p10); // Setup i2c bus on pins 9 and 10
coconnell7 0:038ed656f129 42
coconnell7 0:038ed656f129 43 // constructor(i2c object, i2c address of the mpr121)
coconnell7 0:038ed656f129 44 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); // Mpr121 constructor
coconnell7 0:038ed656f129 45
coconnell7 0:038ed656f129 46 // mpr121 keypad interrupt
coconnell7 0:038ed656f129 47 void fallInterrupt(){
coconnell7 0:038ed656f129 48 OldCurrentKey = CurrentKey;
coconnell7 0:038ed656f129 49 int key_code=0;
coconnell7 0:038ed656f129 50 int i=0;
coconnell7 0:038ed656f129 51 int value=mpr121.read(0x00);
coconnell7 0:038ed656f129 52 value +=mpr121.read(0x01)<<8;
coconnell7 0:038ed656f129 53 i=0;
coconnell7 0:038ed656f129 54 for (i=0; i<12; i++) {
coconnell7 0:038ed656f129 55 if (((value>>i)&0x01)==1) key_code=i+1;
coconnell7 0:038ed656f129 56 }
coconnell7 0:038ed656f129 57 led4=key_code & 0x01;
coconnell7 0:038ed656f129 58 led3=(key_code>>1) & 0x01;
coconnell7 0:038ed656f129 59 led2=(key_code>>2) & 0x01;
coconnell7 0:038ed656f129 60 led1=(key_code>>3) & 0x01;
coconnell7 0:038ed656f129 61 if(led1==0 & led2==0 & led3==0 & led4==1) //Convert recieved value from keypad
coconnell7 0:038ed656f129 62 CurrentKey = 0;
coconnell7 0:038ed656f129 63 if(led1==0 & led2==0 & led3==1 & led4==0)
coconnell7 0:038ed656f129 64 CurrentKey = 1;
coconnell7 0:038ed656f129 65 if(led1==0 & led2==0 & led3==1 & led4==1)
coconnell7 0:038ed656f129 66 CurrentKey = 2;
coconnell7 0:038ed656f129 67 if(led1==0 & led2==1 & led3==0 & led4==0)
coconnell7 0:038ed656f129 68 CurrentKey = 3;
coconnell7 0:038ed656f129 69 if(led1==0 & led2==1 & led3==0 & led4==1)
coconnell7 0:038ed656f129 70 CurrentKey = 4;
coconnell7 0:038ed656f129 71 if(led1==0 & led2==1 & led3==1 & led4==0)
coconnell7 0:038ed656f129 72 CurrentKey = 5;
coconnell7 0:038ed656f129 73 if(led1==0 & led2==1 & led3==1 & led4==1)
coconnell7 0:038ed656f129 74 CurrentKey = 6;
coconnell7 0:038ed656f129 75 if(led1==1 & led2==0 & led3==0 & led4==0)
coconnell7 0:038ed656f129 76 CurrentKey = 7;
coconnell7 0:038ed656f129 77 if(led1==1 & led2==0 & led3==0 & led4==1)
coconnell7 0:038ed656f129 78 CurrentKey = 8;
coconnell7 0:038ed656f129 79 if(led1==1 & led2==0 & led3==1 & led4==0)
coconnell7 0:038ed656f129 80 CurrentKey = 9;
coconnell7 0:038ed656f129 81 if(led1==1 & led2==0 & led3==1 & led4==1)
coconnell7 0:038ed656f129 82 CurrentKey = 10;
coconnell7 0:038ed656f129 83 if(led1==1 & led2==1 & led3==0 & led4==0)
coconnell7 0:038ed656f129 84 CurrentKey = 11;
coconnell7 0:038ed656f129 85 if(CurrentKey != OldCurrentKey & Trigger == 1){ //Ensures that only four numbers are used at a time
coconnell7 0:038ed656f129 86 KeyCount = KeyCount + 1;
coconnell7 0:038ed656f129 87 if(KeyCount == 1)
coconnell7 0:038ed656f129 88 num1 = CurrentKey;
coconnell7 0:038ed656f129 89 if(KeyCount == 2)
coconnell7 0:038ed656f129 90 num2 = CurrentKey;
coconnell7 0:038ed656f129 91 if(KeyCount == 3)
coconnell7 0:038ed656f129 92 num3 = CurrentKey;
coconnell7 0:038ed656f129 93 if(KeyCount == 4){
coconnell7 0:038ed656f129 94 num4 = CurrentKey;
coconnell7 0:038ed656f129 95 Trigger = 0;
coconnell7 0:038ed656f129 96 KeyCount = 0;
coconnell7 0:038ed656f129 97 }
coconnell7 0:038ed656f129 98 }
coconnell7 0:038ed656f129 99 }
coconnell7 0:038ed656f129 100
coconnell7 0:038ed656f129 101
coconnell7 0:038ed656f129 102 // uLCD thread
coconnell7 0:038ed656f129 103 void Armed(void const *args){ //Thread for initial state of the alarm
coconnell7 0:038ed656f129 104 interrupt.fall(&fallInterrupt);
coconnell7 0:038ed656f129 105 interrupt.mode(PullUp);
coconnell7 0:038ed656f129 106 while(1){
coconnell7 0:038ed656f129 107 if(Safety == 0){
coconnell7 0:038ed656f129 108 stdio_mutex.lock(); // Mutex lock for LCD screen
coconnell7 0:038ed656f129 109 if(!ArmedOnOff){
coconnell7 0:038ed656f129 110 uLCD.locate(1,1); //The default state for the alarm
coconnell7 0:038ed656f129 111 uLCD.printf("ALARM: ARMED ");
coconnell7 0:038ed656f129 112 uLCD.locate(1,5);
coconnell7 0:038ed656f129 113 uLCD.printf("Enter Passcode: ");
coconnell7 0:038ed656f129 114 }
coconnell7 0:038ed656f129 115 stdio_mutex.unlock(); // Unlock mutex
coconnell7 0:038ed656f129 116 }
coconnell7 0:038ed656f129 117 }
coconnell7 0:038ed656f129 118 }
coconnell7 0:038ed656f129 119
coconnell7 0:038ed656f129 120 void PassCode(void const *args){ //Thread to check passcode
coconnell7 0:038ed656f129 121 interrupt.fall(&fallInterrupt);
coconnell7 0:038ed656f129 122 interrupt.mode(PullUp);
coconnell7 0:038ed656f129 123 while(1){
coconnell7 0:038ed656f129 124 if(Trigger == 0){
coconnell7 0:038ed656f129 125 stdio_mutex.lock(); // Mutex lock for LCD screen
coconnell7 0:038ed656f129 126 uLCD.locate(1,7);
coconnell7 0:038ed656f129 127 uLCD.printf(" ");
coconnell7 0:038ed656f129 128 uLCD.locate(1,8);
coconnell7 0:038ed656f129 129 uLCD.printf(" ");
coconnell7 0:038ed656f129 130 if(num1 == 1 & num2 == 9 & num3 == 4 & num4 == 2){ //Checks to see if the correct key is entered, 1942
coconnell7 0:038ed656f129 131 uLCD.locate(1,1);
coconnell7 0:038ed656f129 132 uLCD.printf("ALARM: DISARMED"); //The disarmed state for the alarm upon entering the correct code
coconnell7 0:038ed656f129 133 uLCD.locate(1,7);
coconnell7 0:038ed656f129 134 uLCD.printf("%d %d %d %d",num1,num2,num3,num4);
coconnell7 0:038ed656f129 135 uLCD.locate(1,8);
coconnell7 0:038ed656f129 136 uLCD.printf("Take your item");
coconnell7 0:038ed656f129 137 Safety = 1;
coconnell7 0:038ed656f129 138 int countDown = 20;
coconnell7 0:038ed656f129 139 while(countDown >=0){ // Countdown to when the alarm will re-arm
coconnell7 0:038ed656f129 140 uLCD.locate(1,9);
coconnell7 0:038ed656f129 141 uLCD.printf("ARMING IN %d ", countDown);
coconnell7 0:038ed656f129 142 countDown--;
coconnell7 0:038ed656f129 143 Thread::wait(1000.0); // Wait 1 second
coconnell7 0:038ed656f129 144 }
coconnell7 0:038ed656f129 145 countDown = 20;
coconnell7 0:038ed656f129 146 uLCD.locate(1,9);
coconnell7 0:038ed656f129 147 uLCD.printf(" "); //clears the lower screen
coconnell7 0:038ed656f129 148 uLCD.locate(1,7);
coconnell7 0:038ed656f129 149 uLCD.printf(" ");
coconnell7 0:038ed656f129 150 uLCD.locate(1,8);
coconnell7 0:038ed656f129 151 uLCD.printf(" ");
coconnell7 0:038ed656f129 152 Safety = 0;
coconnell7 0:038ed656f129 153 }
coconnell7 0:038ed656f129 154 else {
coconnell7 0:038ed656f129 155 uLCD.locate(1,7);
coconnell7 0:038ed656f129 156 uLCD.printf("%d %d %d %d",num1,num2,num3,num4); //Displays the incorrect passcode waits 5 seconds for further entries
coconnell7 0:038ed656f129 157 uLCD.locate(1,8);
coconnell7 0:038ed656f129 158 uLCD.printf("INCORRECT");
coconnell7 0:038ed656f129 159 Thread::wait(5000.0);
coconnell7 0:038ed656f129 160 uLCD.locate(1,7);
coconnell7 0:038ed656f129 161 uLCD.printf(" "); //clears the lower screen
coconnell7 0:038ed656f129 162 uLCD.locate(1,8);
coconnell7 0:038ed656f129 163 uLCD.printf(" ");
coconnell7 0:038ed656f129 164 }
coconnell7 0:038ed656f129 165 stdio_mutex.unlock(); // Unlock mutex
coconnell7 0:038ed656f129 166 Trigger = 1;
coconnell7 0:038ed656f129 167 Thread::wait(1000.0); // 10 second wait
coconnell7 0:038ed656f129 168 }
coconnell7 0:038ed656f129 169 }
coconnell7 0:038ed656f129 170 }
coconnell7 0:038ed656f129 171
coconnell7 0:038ed656f129 172
coconnell7 0:038ed656f129 173 void Alarm(void const *args) //Thread to sound alarm
coconnell7 0:038ed656f129 174 {
coconnell7 0:038ed656f129 175 interrupt.fall(&fallInterrupt);
coconnell7 0:038ed656f129 176 interrupt.mode(PullUp);
coconnell7 0:038ed656f129 177 while(1){
coconnell7 0:038ed656f129 178 if(ArmedOnOff==true & Safety == 0){
coconnell7 0:038ed656f129 179 stdio_mutex.lock(); // Mutex lock for LCD screen
coconnell7 0:038ed656f129 180 uLCD.locate(1,1);
coconnell7 0:038ed656f129 181 uLCD.printf("ALARM: MISSING!"); //Sounds alarm if the correct code isn't entered and the object is missing
coconnell7 0:038ed656f129 182 uLCD.locate(1,5);
coconnell7 0:038ed656f129 183 uLCD.printf("Enter Passcode: ");
coconnell7 0:038ed656f129 184 stdio_mutex.unlock(); // Unlock mutex
coconnell7 0:038ed656f129 185 FILE *wave_file;
coconnell7 0:038ed656f129 186 wave_file=fopen("/sd/myMusic/Alarm.wav","r");
coconnell7 0:038ed656f129 187 waver.play(wave_file);
coconnell7 0:038ed656f129 188 fclose(wave_file);
coconnell7 0:038ed656f129 189
coconnell7 0:038ed656f129 190 //Thread::wait(1000.0); // Wait 1 second
coconnell7 0:038ed656f129 191 }
coconnell7 0:038ed656f129 192 }
coconnell7 0:038ed656f129 193 }
coconnell7 0:038ed656f129 194
coconnell7 0:038ed656f129 195
coconnell7 0:038ed656f129 196 int main(){
coconnell7 0:038ed656f129 197 Thread thread1(Alarm);
coconnell7 0:038ed656f129 198 Thread thread3(Armed);
coconnell7 0:038ed656f129 199 Thread thread4(PassCode);
coconnell7 0:038ed656f129 200 interrupt.mode(PullUp);
coconnell7 0:038ed656f129 201 interrupt.fall(&fallInterrupt);
coconnell7 0:038ed656f129 202
coconnell7 0:038ed656f129 203 while(1){
coconnell7 0:038ed656f129 204
coconnell7 0:038ed656f129 205
coconnell7 0:038ed656f129 206 if (IRout < 0.5f){
coconnell7 0:038ed656f129 207 //led1 = 1;
coconnell7 0:038ed656f129 208 ArmedOnOff = true;
coconnell7 0:038ed656f129 209 }
coconnell7 0:038ed656f129 210 else {
coconnell7 0:038ed656f129 211 ArmedOnOff = false;
coconnell7 0:038ed656f129 212 }
coconnell7 0:038ed656f129 213
coconnell7 0:038ed656f129 214 }
coconnell7 0:038ed656f129 215 }