ECE4018 Final Project - Cookie Jar

Dependencies:   FSRCookieJar mbed

Fork of MPR121_Demo by jim hamblen

main.cpp

Committer:
lalexander37
Date:
2018-05-01
Revision:
1:25bc0044cb07
Parent:
0:e09703934ff4

File content as of revision 1:25bc0044cb07:

/*
Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


#include <mbed.h>
#include <string>
#include <list>
#include <sstream>
#include <math.h> 
#include <mpr121.h>
#include "FSR.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);

// Setup the Serial to the PC for debugging
Serial pc(USBTX, USBRX);

// Setup the i2c bus on pins 28 and 27
I2C i2c(p9, p10);

// Setup the Mpr121:
// constructor(i2c object, i2c address of the mpr121)
Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);

//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");
}
 
void pc_recv()
{
    led4 = !led4;
    while(pc.readable()) {
        dev.putc(pc.getc());
    }
}

void lid(int time) {
    ctrl = 1;
    wait(time);
    ctrl = 0;
}

void open() {
    ctrl = 1;
}

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("\nHello from the mbed & mpr121\n\r");
    
    
    bool keyFlag = 0;
    bool passFlag = 0;
    
    //startWeight = weight_sensor();

    while (1) {
        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);
    }
}