Lauren Alexander / Mbed 2 deprecated WhoStoleTheCookieFinal

Dependencies:   FSRCookieJar mbed

Fork of MPR121_Demo by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 
00024 #include <mbed.h>
00025 #include <string>
00026 #include <list>
00027 #include <sstream>
00028 #include <math.h> 
00029 #include <mpr121.h>
00030 #include "FSR.h"
00031 
00032 
00033 using namespace std;
00034 
00035 DigitalOut led1(LED1);
00036 DigitalOut led2(LED2);
00037 DigitalOut led3(LED3);
00038 DigitalOut led4(LED4);
00039 Serial dev(p28,p27);
00040 
00041 // Create the interrupt receiver object on pin 26
00042 InterruptIn interrupt(p26);
00043 
00044 // Setup the Serial to the PC for debugging
00045 Serial pc(USBTX, USBRX);
00046 
00047 // Setup the i2c bus on pins 28 and 27
00048 I2C i2c(p9, p10);
00049 
00050 // Setup the Mpr121:
00051 // constructor(i2c object, i2c address of the mpr121)
00052 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00053 
00054 //FSR handles the weight sensor
00055 FSR fsr(p20, 10);
00056 int j = 0;
00057 float weight_array[20];
00058 float average = 0.0;
00059 //float startWeight = 0.0;
00060 
00061 DigitalOut ctrl(p8);
00062 void lid(int);
00063 int count = 0;
00064 
00065 int testKey [4] = {};
00066 char testPassword [9] = {};
00067 
00068 // -------- decalre correct passwords ----------//
00069 int realKey[4] = {1,2,2,4};
00070 char realPassword [9]= {'G', 'e', 'o', 'r', 'g', 'e', 'P', '.', '\0'};
00071  
00072 int countToo = 0;
00073 
00074 void dev_recv()
00075 {
00076     int index = 0;
00077     while(dev.readable()) {
00078         testPassword[index]=dev.getc();
00079         pc.putc(testPassword[index]);
00080         index++;
00081         wait(0.05);
00082     }
00083     pc.printf("\n\r");
00084 }
00085  
00086 void pc_recv()
00087 {
00088     led4 = !led4;
00089     while(pc.readable()) {
00090         dev.putc(pc.getc());
00091     }
00092 }
00093 
00094 void lid(int time) {
00095     ctrl = 1;
00096     wait(time);
00097     ctrl = 0;
00098 }
00099 
00100 void open() {
00101     ctrl = 1;
00102 }
00103 
00104 void close() {
00105     ctrl = 0;
00106 }
00107 float weight_sensor() {
00108     wait(1);
00109     float h = fsr.readFSRResistance();
00110     float slope = (4 - 2) / (log10(6.2) - log10(0.25));
00111     float pepsiSux = log10(h);
00112     float cocaCola = pow(10, ((log10(6.2) - pepsiSux) * slope + 2));
00113     cocaCola = 453*cocaCola;
00114     wait(0.5);
00115     
00116     if (cocaCola-30 <= 0) {
00117         return 0;
00118     } else {
00119         return cocaCola-30;
00120     }
00121 }
00122  
00123 int main() {
00124     pc.baud(9600);
00125     dev.baud(9600);
00126  
00127     pc.attach(&pc_recv, Serial::RxIrq);
00128     dev.attach(&dev_recv, Serial::RxIrq);
00129     
00130     int counter = 0;
00131 
00132     //pc.printf("\nHello from the mbed & mpr121\n\r");
00133     
00134     
00135     bool keyFlag = 0;
00136     bool passFlag = 0;
00137     
00138     //startWeight = weight_sensor();
00139 
00140     while (1) {
00141         wait(1);
00142         while (keyFlag == 0 & passFlag == 0){
00143             //while(mpr121.read(0x000)==NULL){
00144             //    dev.printf("Current cookies: %d grams\n\r", (int)weight_sensor());
00145             //    dev.printf("Enter the key code into the keypad... \n\r ");
00146             //}
00147             //pc.printf("got here!");
00148             //wait(1);
00149             while (counter<4) {
00150                 //printf("read: %d \n\r", counter);
00151                 while(mpr121.read(0x000)==NULL){}
00152                 int value=mpr121.read(0x000);
00153                 double lauren = log((double)value);
00154                 lauren = lauren/log(2.0);
00155                 testKey[counter] = (int)lauren;
00156                 dev.printf("You entered: %d\n\r", testKey[counter]);
00157                 while(mpr121.read(0x000)!=NULL){}
00158                 counter++;
00159             }
00160             if(testKey[0] == realKey[0] 
00161             & testKey[1] == realKey[1]
00162             & testKey[2] == realKey[2]
00163             & testKey[3] == realKey[3]) {
00164                 keyFlag = 1;
00165                 dev.printf("lauren: waiting for 2 factor authentication\n\r");
00166             } else {
00167                 counter = 0;
00168                 dev.printf("Please try again\n\r");
00169             }
00170         }
00171         while (keyFlag == 1 & passFlag == 0) {
00172             if(
00173             testPassword[0] == realPassword[0]
00174             & testPassword[1] == realPassword[1]
00175             & testPassword[2] == realPassword[2]
00176             & testPassword[3] == realPassword[3]
00177             & testPassword[4] == realPassword[4]
00178             & testPassword[5] == realPassword[5]
00179             & testPassword[6] == realPassword[6]
00180             & testPassword[7] == realPassword[7]){
00181             passFlag = 1;
00182             dev.printf("Cookie jar is open, press any key to close\n\r");
00183             }
00184             else
00185             {
00186                 //pc.printf("\n\rPlease enter a correct password");
00187                 dev.printf("Please enter a correct password \n\r");
00188                 wait(1.5);
00189             }
00190         }
00191         
00192         while (keyFlag == 1 & passFlag == 1) {
00193             pc.printf("\n\rUnlocked \n\rPress any button to lock");
00194             //print weight sensor to UART
00195             //open the servos
00196             open();
00197             while(mpr121.read(0x000)==NULL){
00198                 dev.printf("current weight is about -- %d  -- grams\n\r", (int)weight_sensor());
00199                 wait(0.5);
00200             }
00201             close();
00202             
00203             keyFlag = 0;
00204             passFlag = 0;
00205             testKey[0] = 0; 
00206             testKey[1] = 0;
00207             testKey[2] = 0;
00208             testKey[3] = 0;
00209             testPassword[0] = 0;
00210             testPassword[1] = 0;
00211             testPassword[2] = 0;
00212             testPassword[3] = 0;
00213             testPassword[4] = 0;
00214             testPassword[5] = 0;
00215             testPassword[6] = 0;
00216             testPassword[7] = 0;
00217         }
00218         wait(1);
00219     }
00220 }
00221 
00222