Adam Lawrence / Mbed 2 deprecated smartSafe

Dependencies:   4DGL-uLCD-SE DebounceIn Motordriver PinDetect SDFileSystem mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <cmath>
00003 #include "solenoid.h"
00004 #include "rtos.h"
00005 #include "uLCD_4DGL.h"
00006 #include "TMP36.h"
00007 #include "motordriver.h"
00008 #include "PinDetect.h"
00009 #include "mpr121.h"      //touchpad
00010 #include "DebounceIn.h"
00011 #include  "Speaker.h"    //used for playing tones
00012 Speaker mySpeaker(p18); 
00013 Serial blue(p13,p14); //blue tooth device
00014 TMP36 myTMP36(p15);  //Analog in
00015 Mutex mut; //standard io mutex lock
00016 DigitalOut led1(LED1);
00017 DigitalOut led2(LED2);
00018 DigitalOut led3(LED3);
00019 DigitalOut led4(LED4);
00020 AnalogIn ir(p19); //Sharp IR sensor 
00021 uLCD_4DGL uLCD(p28,p27,p30); //create a global lcd object
00022 I2C i2c(p9, p10);
00023 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00024 InterruptIn interrupt(p25);
00025 PinDetect pb1(p21);//push button 1
00026 PinDetect pb2(p22);//push button 2
00027 PinDetect pb3(p23);//push button 3
00028 PinDetect pb4(p24);//push button 4
00029 Motor  left(p26, p16, p17, 1); // pwm, fwd, rev, has brake feature
00030 Solenoid mySolenoid(p8); 
00031   char bnum=0;
00032     char bhit=0;
00033 float tempC = 0.0;
00034 bool shadeDown = true; //true shade is down, false shade is up
00035 bool tempMode = false; //false is celcius, true is fahrenheit
00036 int combo[5];
00037 bool setUp = false; //boolean for initial setup of safe (will be set to true after passcode is entered)
00038 bool locked = true; //boolean if safe is locked or not.
00039 int passCode[5];
00040 int j = 0;
00041 int passVal = 0; //keeps track of correct input values
00042 bool passed = true; //this boolean will be used to unlock the safe
00043 bool pb1push = false; //pushbutton 1 was pushed boolean
00044 bool pb2push = false;
00045 bool pb3push = false;
00046 bool pb4push = false;
00047 int failVal = 0; //counts failed attempts
00048 int key_code = -17; //set this to a negative number that won't ever be in Simon's array and also isn't a possible value for drawing
00049 void fallInterrupt() {
00050   int i=0;
00051   int value=mpr121.read(0x00);
00052   value +=mpr121.read(0x01)<<8;
00053   // LED demo mod
00054   i=0;
00055   // puts key number out to LEDs for demo
00056   for (i=0; i<12; i++) {
00057   if (((value>>i)&0x01)==1) key_code=i;
00058   }
00059 
00060 }
00061 void unlocker() {
00062      mySolenoid =1;
00063     
00064       wait(2);
00065       mySolenoid = 0;
00066 }
00067 void lockInterrupt() {
00068     //button 1
00069    pb1push = true;
00070         led1 = 1;
00071         wait(0.5);
00072         led1 = 0;
00073     mySpeaker.PlayNote(400, 0.5, 1.0);
00074 }
00075 void shadeInterrupt() {
00076    //button 2 
00077     pb2push = true;
00078     led2 = 1;
00079     wait(0.5);
00080     led2 = 0;
00081     mySpeaker.PlayNote(800, 0.5, 1.0);
00082 }
00083 void settingsInterrupt() {
00084     //button 3
00085     //will be able to change passcode.
00086     //change temp display
00087     //change display shade.
00088     pb3push = true;
00089     led3 = 1;
00090         wait(0.5);
00091         led3 = 0;
00092     mySpeaker.PlayNote(1200, 0.5, 1.0);
00093 }    
00094 
00095 void exitInterrupt() {
00096     //button 4
00097     pb4push = true;
00098     led4 = 1;
00099         wait(0.5);
00100         led4 = 0;
00101     mySpeaker.PlayNote(1600, 0.5, 1.0);
00102 }
00103 void setPassword() {
00104     uLCD.cls();
00105     uLCD.color(WHITE);
00106     uLCD.printf("Please put in your \n 5 digit passcode now \n \n \n \n");
00107     wait(0.5);
00108     int i = 0;
00109     while(i < 5 && !pb4push) {
00110         if (key_code >= 0) {
00111             combo[i] = key_code;
00112             uLCD.text_height(4);
00113             uLCD.text_width(2);
00114             uLCD.printf("%d ",combo[i]);
00115             i++;
00116             key_code = -4;
00117         }
00118     }
00119     wait(0.5);
00120     uLCD.cls();
00121     uLCD.printf("Passcode saved. \nLock engaged. \n");
00122     wait(1);
00123     uLCD.cls(); 
00124 }
00125 void shadeMove() {
00126     uLCD.cls();
00127     if(shadeDown) {
00128     
00129     shadeDown = false;
00130     //turn motor backward.
00131     for (int i = 0; i < 6 ; i ++) {
00132         led1=!led1;
00133         left.speed(.5);
00134         wait(0.2);
00135     }
00136     left.speed(0);
00137     uLCD.printf("Removing shade.");    
00138     wait(2); //might need to be longer dependent on actual mplementation speed
00139     uLCD.cls();
00140     } else {
00141     shadeDown = true;
00142     for (int i = 0; i < 6 ; i ++) {
00143         led1=!led1;
00144         left.speed(-.5);
00145         wait(0.2);
00146     }
00147     left.speed(0);
00148     //turn motor forward    
00149     uLCD.printf("Placing shade.");
00150     wait(2); //might need to be longer dependent on actual mplementation speed
00151     uLCD.cls();
00152     }    
00153 }
00154 void bluetoothOn() {
00155  while (!pb4push) {
00156     if (blue.getc()=='!') {
00157             uLCD.locate(0,0);
00158             uLCD.printf("In Bluetooth Mode.");
00159             if (blue.getc()=='B') { //button data packet
00160                 bnum = blue.getc(); //button number
00161                 bhit = blue.getc(); //1=hit, 0=release
00162                 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
00163                    // myled = bnum - '0'; //current button number will appear on LEDs
00164                     switch (bnum) {
00165                         case '1': //number button 1
00166                            if (bhit=='1') {
00167                                 unlocker();
00168                               //add hit code here
00169                                 //open safe (or close safe) dependent on state
00170                             } else {
00171                                 //add release code here
00172                             }
00173                             break;
00174                         case '2': //number button 2
00175                             if (bhit=='1') {
00176                                 //add hit code here
00177                                 //move shade
00178                                  shadeMove();
00179                             } else {
00180                                
00181                                 //add release code here
00182                             }
00183                             break;
00184                         case '3': //number button 3
00185                             if (bhit=='1') {
00186                                 tempMode = !tempMode;
00187                             } else {
00188                                 //add release code here
00189                             }
00190                             break;
00191                         case '4': //number button 4
00192                             if (bhit=='1') {
00193                                 //add hit code here
00194                                 pb4push = true;
00195                             } else {
00196                                 //add release code here
00197                             }
00198                             break;
00199                       
00200                     }
00201                 }
00202             }    
00203     }
00204     }
00205 }
00206 int main() {
00207     set_time(46800); // Set RTC time to Wed, 28 Oct 2009 11:35:37 let's change this to wahtever the presentation day is.
00208 //clock stuff goes here
00209 
00210 pb1.mode(PullUp); //the safe unlock/lock method initiator
00211 pb2.mode(PullUp); //the shade button
00212 pb3.mode(PullUp); //temperature button
00213 pb4.mode(PullUp); //exit routine button
00214 wait(.01);
00215 // Setup Interrupt callback functions for a pb hit
00216 pb1.attach_deasserted(&lockInterrupt);
00217 pb2.attach_deasserted(&shadeInterrupt);
00218 pb3.attach_deasserted(&settingsInterrupt);
00219 pb4.attach_deasserted(&exitInterrupt);
00220 pb1.setSampleFrequency();
00221 pb2.setSampleFrequency();
00222 pb3.setSampleFrequency();
00223 pb4.setSampleFrequency();
00224 interrupt.fall(&fallInterrupt); //setting up the touch pad for use by assigning a method for the falling side of the touchpad interrupt
00225 interrupt.mode(PullUp);         //put the interrupt mode to pull up, so that interrupts will trigger when the touchpad is hit
00226     while(1) {
00227         time_t seconds = time(NULL);
00228         //if ir.read is greater than .5 beep(?)
00229         tempC = myTMP36.read();
00230         if (!setUp) {
00231             mut.lock();
00232             uLCD.color(WHITE);
00233             uLCD.printf("Welcome to \nSMART Safe.");
00234             
00235             wait(1.0);
00236             uLCD.cls();
00237             setPassword();
00238             mut.unlock();
00239             setUp = true;
00240             //put in clock
00241             //put in temp
00242         } else if (pb1push) {
00243                  pb1push = false;
00244                  pb4push = false;
00245                  uLCD.cls();
00246                  uLCD.printf("Enter your pass code now.\n");
00247                  while (passVal < 5 && failVal < 5 && !pb4push) {
00248                     if(key_code >= 0) {
00249                         uLCD.text_height(4);
00250                         uLCD.printf("%d ",key_code);
00251                         if (key_code == combo[j]) {
00252                             passVal++;
00253                             passed = true;
00254                         } else {
00255                             passed = false;
00256                             j = 0;
00257                             failVal++;
00258                         } 
00259                         key_code = -1;
00260                     }   
00261                     if (passVal == 5 && passed && failVal < 5) {
00262                         passed = true;
00263                         pb1push = false;
00264                         j = 0;
00265                         failVal = 0;   
00266                         uLCD.cls();
00267                         uLCD.printf("Safe unlocked.");
00268                         locked = false;
00269                         unlocker();
00270                         locked = true;
00271                         break;
00272                     }
00273                     if (failVal == 5) {
00274                         uLCD.printf("You have put in \n the wrong password \n five times. \n You are locked out.");    
00275                     }  
00276                 }    
00277                 if(pb4push){
00278                     pb4push = false;
00279                     uLCD.printf("Unlocking \nsequence \naborted.");  
00280                 }  
00281                 passVal = 0;
00282                 failVal = 0;
00283                 wait(1);
00284                 uLCD.cls();
00285             } else if (pb2push) { 
00286             pb2push = false;
00287 
00288                 shadeMove();
00289             
00290         } else if(pb3push){
00291             pb3push = false;
00292             uLCD.cls();
00293             while (!pb4push) {
00294                 uLCD.locate(0,0);
00295                 uLCD.printf("Change Temperature Format (1)\n");
00296                 uLCD.printf("Change Passcode (2) \n");
00297                 uLCD.printf("Enter BLUETOOTH \n mode (3)\n");
00298                 uLCD.printf("Exit menu (4)");
00299                 wait(1);
00300                 if (pb1push) {
00301                     pb1push = false;
00302                     uLCD.cls();
00303                     uLCD.printf("Fahrenheit (1)\n");
00304                     uLCD.printf("Celcius (2)");
00305                     wait(1); 
00306                     bool action = false;
00307                     while (!pb4push && !action) {  
00308                         if (pb1push) {
00309                             action = true;
00310                             pb1push = false;
00311                             tempMode = true;//thermometer change to F   
00312                             break; 
00313                         } else if (pb2push) {
00314                             action = true;
00315                             pb2push = false;
00316                             tempMode = false;//themometer change to C    
00317                             break;
00318                         }
00319                         led1 = !led1;
00320                         wait(.5);
00321                     }
00322                     uLCD.cls();
00323                 } else if (pb2push) { //this is going to change the time of day.
00324                     pb2push = false;
00325                     uLCD.cls();
00326                     setPassword();
00327                 } else if (pb3push) {
00328                     pb3push = false;
00329                     bluetoothOn();
00330                     
00331                 }
00332             }
00333                 uLCD.cls();
00334                 led1 =1;
00335                 led2 =1;
00336                 pb4push = false;
00337         }   else {
00338            float temp = myTMP36.read();
00339            if (tempMode) {
00340                 temp = (temp * 1.8) + 32; 
00341             }
00342             if (locked) {
00343                 uLCD.text_height(4);
00344                 uLCD.color(RED);
00345                 uLCD.locate(4,0);
00346                 uLCD.printf("Locked");
00347                 uLCD.color(WHITE);
00348                 uLCD.text_height(3);
00349                 uLCD.locate(1,2);
00350                 char buffer[32];
00351                 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
00352                 uLCD.printf("Time %s",buffer);
00353                 uLCD.locate(1,4);
00354                 if (tempMode) {
00355                     uLCD.printf("Temp %3.2f F",temp);   
00356                 } else {
00357                     uLCD.printf("Temp %3.2f C",temp);
00358                 }
00359                 wait(2);
00360                 //uLCD.cls();    
00361             } else {
00362                 uLCD.text_height(4);
00363                 uLCD.color(GREEN);
00364                 uLCD.locate(4,0);
00365                 uLCD.printf("Unlocked");
00366                 uLCD.color(WHITE);
00367                 uLCD.text_height(3);
00368                 uLCD.locate(1,2);
00369                 char buffer[32];
00370                 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
00371                 uLCD.printf("Time %s",buffer);
00372                 uLCD.locate(1,4);
00373                 if (tempMode) {
00374                     uLCD.printf("Temp %3.2f F",temp);   
00375                 } else {
00376                     uLCD.printf("Temp %3.2f C",temp);
00377                 }
00378                 wait(2);
00379                 //uLCD.cls();    
00380                     
00381             }
00382             
00383         }  
00384      
00385     }
00386 }