ECE4018 Final Project - Cookie Jar

Dependencies:   FSRCookieJar mbed

Fork of MPR121_Demo by jim hamblen

Files at this revision

API Documentation at this revision

Comitter:
lalexander37
Date:
Tue May 01 16:00:00 2018 +0000
Parent:
0:e09703934ff4
Commit message:
CookieJarDemo

Changed in this revision

FSR.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FSR.lib	Tue May 01 16:00:00 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/lalexander37/code/FSRCookieJar/#78279e66b1a0
--- a/main.cpp	Wed Mar 16 01:49:13 2011 +0000
+++ b/main.cpp	Tue May 01 16:00:00 2018 +0000
@@ -20,16 +20,23 @@
 THE SOFTWARE.
 */
 
+
 #include <mbed.h>
 #include <string>
 #include <list>
+#include <sstream>
+#include <math.h> 
+#include <mpr121.h>
+#include "FSR.h"
 
-#include <mpr121.h>
+
+using namespace std;
 
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
+Serial dev(p28,p27);
 
 // Create the interrupt receiver object on pin 26
 InterruptIn interrupt(p26);
@@ -44,82 +51,172 @@
 // constructor(i2c object, i2c address of the mpr121)
 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
 
-void fallInterrupt() {
-    int key_code=0;
-    int i=0;
-    int value=mpr121.read(0x00);
-    value +=mpr121.read(0x01)<<8;
-    // LED demo mod by J. Hamblen
-    //pc.printf("MPR value: %x \r\n", value);
-    i=0;
-    // puts key number out to LEDs for demo
-    for (i=0; i<12; i++) {
-        if (((value>>i)&0x01)==1) key_code=i+1;
-        }
-    led4=key_code & 0x01;
-    led3=(key_code>>1) & 0x01;
-    led2=(key_code>>2) & 0x01;
-    led1=(key_code>>3) & 0x01;
+//FSR handles the weight sensor
+FSR fsr(p20, 10);
+int j = 0;
+float weight_array[20];
+float average = 0.0;
+//float startWeight = 0.0;
+
+DigitalOut ctrl(p8);
+void lid(int);
+int count = 0;
+
+int testKey [4] = {};
+char testPassword [9] = {};
+
+// -------- decalre correct passwords ----------//
+int realKey[4] = {1,2,2,4};
+char realPassword [9]= {'G', 'e', 'o', 'r', 'g', 'e', 'P', '.', '\0'};
+ 
+int countToo = 0;
+
+void dev_recv()
+{
+    int index = 0;
+    while(dev.readable()) {
+        testPassword[index]=dev.getc();
+        pc.putc(testPassword[index]);
+        index++;
+        wait(0.05);
+    }
+    pc.printf("\n\r");
 }
-int main() {
-
-    pc.printf("\nHello from the mbed & mpr121\n\r");
-
-    unsigned char dataArray[2];
-    int key;
-    int count = 0;
-
-    pc.printf("Test 1: read a value: \r\n");
-    dataArray[0] = mpr121.read(AFE_CFG);
-    pc.printf("Read value=%x\r\n\n",dataArray[0]);
-
-    pc.printf("Test 2: read a value: \r\n");
-    dataArray[0] = mpr121.read(0x5d);
-    pc.printf("Read value=%x\r\n\n",dataArray[0]);
+ 
+void pc_recv()
+{
+    led4 = !led4;
+    while(pc.readable()) {
+        dev.putc(pc.getc());
+    }
+}
 
-    pc.printf("Test 3: write & read a value: \r\n");
-    mpr121.read(ELE0_T);
-    mpr121.write(ELE0_T,0x22);
-    dataArray[0] = mpr121.read(ELE0_T);
-    pc.printf("Read value=%x\r\n\n",dataArray[0]);
+void lid(int time) {
+    ctrl = 1;
+    wait(time);
+    ctrl = 0;
+}
+
+void open() {
+    ctrl = 1;
+}
 
-    pc.printf("Test 4: Write many values: \r\n");
-    unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41};
-    mpr121.writeMany(0x42,data,7);
-
-    // Now read them back ..
-    key = 0x42;
-    count = 0;
-    while (count < 7) {
-        char result = mpr121.read(key);
-        key++;
-        count++;
-        pc.printf("Read value: '%x'=%x\n\r",key,result);
+void close() {
+    ctrl = 0;
+}
+float weight_sensor() {
+    wait(1);
+    float h = fsr.readFSRResistance();
+    float slope = (4 - 2) / (log10(6.2) - log10(0.25));
+    float pepsiSux = log10(h);
+    float cocaCola = pow(10, ((log10(6.2) - pepsiSux) * slope + 2));
+    cocaCola = 453*cocaCola;
+    wait(0.5);
+    
+    if (cocaCola-30 <= 0) {
+        return 0;
+    } else {
+        return cocaCola-30;
     }
+}
+ 
+int main() {
+    pc.baud(9600);
+    dev.baud(9600);
+ 
+    pc.attach(&pc_recv, Serial::RxIrq);
+    dev.attach(&dev_recv, Serial::RxIrq);
+    
+    int counter = 0;
 
-    pc.printf("Test 5: Read Electrodes:\r\n");
-    key = ELE0_T;
-    count = 0;
-    while (count < 24) {
-        char result = mpr121.read(key);
-        pc.printf("Read key:%x value:%x\n\r",key,result);
-        key++;
-        count++;
-    }
-    pc.printf("--------- \r\n\n");
-
-    // mpr121.setProximityMode(true);
-
-    pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG));
-
-    interrupt.fall(&fallInterrupt);
-    interrupt.mode(PullUp);
+    //pc.printf("\nHello from the mbed & mpr121\n\r");
+    
+    
+    bool keyFlag = 0;
+    bool passFlag = 0;
+    
+    //startWeight = weight_sensor();
 
     while (1) {
-        wait(5);
-        pc.printf(".");
+        wait(1);
+        while (keyFlag == 0 & passFlag == 0){
+            //while(mpr121.read(0x000)==NULL){
+            //    dev.printf("Current cookies: %d grams\n\r", (int)weight_sensor());
+            //    dev.printf("Enter the key code into the keypad... \n\r ");
+            //}
+            //pc.printf("got here!");
+            //wait(1);
+            while (counter<4) {
+                //printf("read: %d \n\r", counter);
+                while(mpr121.read(0x000)==NULL){}
+                int value=mpr121.read(0x000);
+                double lauren = log((double)value);
+                lauren = lauren/log(2.0);
+                testKey[counter] = (int)lauren;
+                dev.printf("You entered: %d\n\r", testKey[counter]);
+                while(mpr121.read(0x000)!=NULL){}
+                counter++;
+            }
+            if(testKey[0] == realKey[0] 
+            & testKey[1] == realKey[1]
+            & testKey[2] == realKey[2]
+            & testKey[3] == realKey[3]) {
+                keyFlag = 1;
+                dev.printf("lauren: waiting for 2 factor authentication\n\r");
+            } else {
+                counter = 0;
+                dev.printf("Please try again\n\r");
+            }
+        }
+        while (keyFlag == 1 & passFlag == 0) {
+            if(
+            testPassword[0] == realPassword[0]
+            & testPassword[1] == realPassword[1]
+            & testPassword[2] == realPassword[2]
+            & testPassword[3] == realPassword[3]
+            & testPassword[4] == realPassword[4]
+            & testPassword[5] == realPassword[5]
+            & testPassword[6] == realPassword[6]
+            & testPassword[7] == realPassword[7]){
+            passFlag = 1;
+            dev.printf("Cookie jar is open, press any key to close\n\r");
+            }
+            else
+            {
+                //pc.printf("\n\rPlease enter a correct password");
+                dev.printf("Please enter a correct password \n\r");
+                wait(1.5);
+            }
+        }
+        
+        while (keyFlag == 1 & passFlag == 1) {
+            pc.printf("\n\rUnlocked \n\rPress any button to lock");
+            //print weight sensor to UART
+            //open the servos
+            open();
+            while(mpr121.read(0x000)==NULL){
+                dev.printf("current weight is about -- %d  -- grams\n\r", (int)weight_sensor());
+                wait(0.5);
+            }
+            close();
+            
+            keyFlag = 0;
+            passFlag = 0;
+            testKey[0] = 0; 
+            testKey[1] = 0;
+            testKey[2] = 0;
+            testKey[3] = 0;
+            testPassword[0] = 0;
+            testPassword[1] = 0;
+            testPassword[2] = 0;
+            testPassword[3] = 0;
+            testPassword[4] = 0;
+            testPassword[5] = 0;
+            testPassword[6] = 0;
+            testPassword[7] = 0;
+        }
+        wait(1);
     }
 }
 
 
-