ECE4018 Final Project - Cookie Jar
Dependencies: FSRCookieJar mbed
Fork of MPR121_Demo by
main.cpp@1:25bc0044cb07, 2018-05-01 (annotated)
- Committer:
- lalexander37
- Date:
- Tue May 01 16:00:00 2018 +0000
- Revision:
- 1:25bc0044cb07
- Parent:
- 0:e09703934ff4
CookieJarDemo
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 4180_1 | 0:e09703934ff4 | 1 | /* |
| 4180_1 | 0:e09703934ff4 | 2 | Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) |
| 4180_1 | 0:e09703934ff4 | 3 | |
| 4180_1 | 0:e09703934ff4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4180_1 | 0:e09703934ff4 | 5 | of this software and associated documentation files (the "Software"), to deal |
| 4180_1 | 0:e09703934ff4 | 6 | in the Software without restriction, including without limitation the rights |
| 4180_1 | 0:e09703934ff4 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 4180_1 | 0:e09703934ff4 | 8 | copies of the Software, and to permit persons to whom the Software is |
| 4180_1 | 0:e09703934ff4 | 9 | furnished to do so, subject to the following conditions: |
| 4180_1 | 0:e09703934ff4 | 10 | |
| 4180_1 | 0:e09703934ff4 | 11 | The above copyright notice and this permission notice shall be included in |
| 4180_1 | 0:e09703934ff4 | 12 | all copies or substantial portions of the Software. |
| 4180_1 | 0:e09703934ff4 | 13 | |
| 4180_1 | 0:e09703934ff4 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 4180_1 | 0:e09703934ff4 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 4180_1 | 0:e09703934ff4 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 4180_1 | 0:e09703934ff4 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 4180_1 | 0:e09703934ff4 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 4180_1 | 0:e09703934ff4 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 4180_1 | 0:e09703934ff4 | 20 | THE SOFTWARE. |
| 4180_1 | 0:e09703934ff4 | 21 | */ |
| 4180_1 | 0:e09703934ff4 | 22 | |
| lalexander37 | 1:25bc0044cb07 | 23 | |
| 4180_1 | 0:e09703934ff4 | 24 | #include <mbed.h> |
| 4180_1 | 0:e09703934ff4 | 25 | #include <string> |
| 4180_1 | 0:e09703934ff4 | 26 | #include <list> |
| lalexander37 | 1:25bc0044cb07 | 27 | #include <sstream> |
| lalexander37 | 1:25bc0044cb07 | 28 | #include <math.h> |
| lalexander37 | 1:25bc0044cb07 | 29 | #include <mpr121.h> |
| lalexander37 | 1:25bc0044cb07 | 30 | #include "FSR.h" |
| 4180_1 | 0:e09703934ff4 | 31 | |
| lalexander37 | 1:25bc0044cb07 | 32 | |
| lalexander37 | 1:25bc0044cb07 | 33 | using namespace std; |
| 4180_1 | 0:e09703934ff4 | 34 | |
| 4180_1 | 0:e09703934ff4 | 35 | DigitalOut led1(LED1); |
| 4180_1 | 0:e09703934ff4 | 36 | DigitalOut led2(LED2); |
| 4180_1 | 0:e09703934ff4 | 37 | DigitalOut led3(LED3); |
| 4180_1 | 0:e09703934ff4 | 38 | DigitalOut led4(LED4); |
| lalexander37 | 1:25bc0044cb07 | 39 | Serial dev(p28,p27); |
| 4180_1 | 0:e09703934ff4 | 40 | |
| 4180_1 | 0:e09703934ff4 | 41 | // Create the interrupt receiver object on pin 26 |
| 4180_1 | 0:e09703934ff4 | 42 | InterruptIn interrupt(p26); |
| 4180_1 | 0:e09703934ff4 | 43 | |
| 4180_1 | 0:e09703934ff4 | 44 | // Setup the Serial to the PC for debugging |
| 4180_1 | 0:e09703934ff4 | 45 | Serial pc(USBTX, USBRX); |
| 4180_1 | 0:e09703934ff4 | 46 | |
| 4180_1 | 0:e09703934ff4 | 47 | // Setup the i2c bus on pins 28 and 27 |
| 4180_1 | 0:e09703934ff4 | 48 | I2C i2c(p9, p10); |
| 4180_1 | 0:e09703934ff4 | 49 | |
| 4180_1 | 0:e09703934ff4 | 50 | // Setup the Mpr121: |
| 4180_1 | 0:e09703934ff4 | 51 | // constructor(i2c object, i2c address of the mpr121) |
| 4180_1 | 0:e09703934ff4 | 52 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
| 4180_1 | 0:e09703934ff4 | 53 | |
| lalexander37 | 1:25bc0044cb07 | 54 | //FSR handles the weight sensor |
| lalexander37 | 1:25bc0044cb07 | 55 | FSR fsr(p20, 10); |
| lalexander37 | 1:25bc0044cb07 | 56 | int j = 0; |
| lalexander37 | 1:25bc0044cb07 | 57 | float weight_array[20]; |
| lalexander37 | 1:25bc0044cb07 | 58 | float average = 0.0; |
| lalexander37 | 1:25bc0044cb07 | 59 | //float startWeight = 0.0; |
| lalexander37 | 1:25bc0044cb07 | 60 | |
| lalexander37 | 1:25bc0044cb07 | 61 | DigitalOut ctrl(p8); |
| lalexander37 | 1:25bc0044cb07 | 62 | void lid(int); |
| lalexander37 | 1:25bc0044cb07 | 63 | int count = 0; |
| lalexander37 | 1:25bc0044cb07 | 64 | |
| lalexander37 | 1:25bc0044cb07 | 65 | int testKey [4] = {}; |
| lalexander37 | 1:25bc0044cb07 | 66 | char testPassword [9] = {}; |
| lalexander37 | 1:25bc0044cb07 | 67 | |
| lalexander37 | 1:25bc0044cb07 | 68 | // -------- decalre correct passwords ----------// |
| lalexander37 | 1:25bc0044cb07 | 69 | int realKey[4] = {1,2,2,4}; |
| lalexander37 | 1:25bc0044cb07 | 70 | char realPassword [9]= {'G', 'e', 'o', 'r', 'g', 'e', 'P', '.', '\0'}; |
| lalexander37 | 1:25bc0044cb07 | 71 | |
| lalexander37 | 1:25bc0044cb07 | 72 | int countToo = 0; |
| lalexander37 | 1:25bc0044cb07 | 73 | |
| lalexander37 | 1:25bc0044cb07 | 74 | void dev_recv() |
| lalexander37 | 1:25bc0044cb07 | 75 | { |
| lalexander37 | 1:25bc0044cb07 | 76 | int index = 0; |
| lalexander37 | 1:25bc0044cb07 | 77 | while(dev.readable()) { |
| lalexander37 | 1:25bc0044cb07 | 78 | testPassword[index]=dev.getc(); |
| lalexander37 | 1:25bc0044cb07 | 79 | pc.putc(testPassword[index]); |
| lalexander37 | 1:25bc0044cb07 | 80 | index++; |
| lalexander37 | 1:25bc0044cb07 | 81 | wait(0.05); |
| lalexander37 | 1:25bc0044cb07 | 82 | } |
| lalexander37 | 1:25bc0044cb07 | 83 | pc.printf("\n\r"); |
| 4180_1 | 0:e09703934ff4 | 84 | } |
| lalexander37 | 1:25bc0044cb07 | 85 | |
| lalexander37 | 1:25bc0044cb07 | 86 | void pc_recv() |
| lalexander37 | 1:25bc0044cb07 | 87 | { |
| lalexander37 | 1:25bc0044cb07 | 88 | led4 = !led4; |
| lalexander37 | 1:25bc0044cb07 | 89 | while(pc.readable()) { |
| lalexander37 | 1:25bc0044cb07 | 90 | dev.putc(pc.getc()); |
| lalexander37 | 1:25bc0044cb07 | 91 | } |
| lalexander37 | 1:25bc0044cb07 | 92 | } |
| 4180_1 | 0:e09703934ff4 | 93 | |
| lalexander37 | 1:25bc0044cb07 | 94 | void lid(int time) { |
| lalexander37 | 1:25bc0044cb07 | 95 | ctrl = 1; |
| lalexander37 | 1:25bc0044cb07 | 96 | wait(time); |
| lalexander37 | 1:25bc0044cb07 | 97 | ctrl = 0; |
| lalexander37 | 1:25bc0044cb07 | 98 | } |
| lalexander37 | 1:25bc0044cb07 | 99 | |
| lalexander37 | 1:25bc0044cb07 | 100 | void open() { |
| lalexander37 | 1:25bc0044cb07 | 101 | ctrl = 1; |
| lalexander37 | 1:25bc0044cb07 | 102 | } |
| 4180_1 | 0:e09703934ff4 | 103 | |
| lalexander37 | 1:25bc0044cb07 | 104 | void close() { |
| lalexander37 | 1:25bc0044cb07 | 105 | ctrl = 0; |
| lalexander37 | 1:25bc0044cb07 | 106 | } |
| lalexander37 | 1:25bc0044cb07 | 107 | float weight_sensor() { |
| lalexander37 | 1:25bc0044cb07 | 108 | wait(1); |
| lalexander37 | 1:25bc0044cb07 | 109 | float h = fsr.readFSRResistance(); |
| lalexander37 | 1:25bc0044cb07 | 110 | float slope = (4 - 2) / (log10(6.2) - log10(0.25)); |
| lalexander37 | 1:25bc0044cb07 | 111 | float pepsiSux = log10(h); |
| lalexander37 | 1:25bc0044cb07 | 112 | float cocaCola = pow(10, ((log10(6.2) - pepsiSux) * slope + 2)); |
| lalexander37 | 1:25bc0044cb07 | 113 | cocaCola = 453*cocaCola; |
| lalexander37 | 1:25bc0044cb07 | 114 | wait(0.5); |
| lalexander37 | 1:25bc0044cb07 | 115 | |
| lalexander37 | 1:25bc0044cb07 | 116 | if (cocaCola-30 <= 0) { |
| lalexander37 | 1:25bc0044cb07 | 117 | return 0; |
| lalexander37 | 1:25bc0044cb07 | 118 | } else { |
| lalexander37 | 1:25bc0044cb07 | 119 | return cocaCola-30; |
| 4180_1 | 0:e09703934ff4 | 120 | } |
| lalexander37 | 1:25bc0044cb07 | 121 | } |
| lalexander37 | 1:25bc0044cb07 | 122 | |
| lalexander37 | 1:25bc0044cb07 | 123 | int main() { |
| lalexander37 | 1:25bc0044cb07 | 124 | pc.baud(9600); |
| lalexander37 | 1:25bc0044cb07 | 125 | dev.baud(9600); |
| lalexander37 | 1:25bc0044cb07 | 126 | |
| lalexander37 | 1:25bc0044cb07 | 127 | pc.attach(&pc_recv, Serial::RxIrq); |
| lalexander37 | 1:25bc0044cb07 | 128 | dev.attach(&dev_recv, Serial::RxIrq); |
| lalexander37 | 1:25bc0044cb07 | 129 | |
| lalexander37 | 1:25bc0044cb07 | 130 | int counter = 0; |
| 4180_1 | 0:e09703934ff4 | 131 | |
| lalexander37 | 1:25bc0044cb07 | 132 | //pc.printf("\nHello from the mbed & mpr121\n\r"); |
| lalexander37 | 1:25bc0044cb07 | 133 | |
| lalexander37 | 1:25bc0044cb07 | 134 | |
| lalexander37 | 1:25bc0044cb07 | 135 | bool keyFlag = 0; |
| lalexander37 | 1:25bc0044cb07 | 136 | bool passFlag = 0; |
| lalexander37 | 1:25bc0044cb07 | 137 | |
| lalexander37 | 1:25bc0044cb07 | 138 | //startWeight = weight_sensor(); |
| 4180_1 | 0:e09703934ff4 | 139 | |
| 4180_1 | 0:e09703934ff4 | 140 | while (1) { |
| lalexander37 | 1:25bc0044cb07 | 141 | wait(1); |
| lalexander37 | 1:25bc0044cb07 | 142 | while (keyFlag == 0 & passFlag == 0){ |
| lalexander37 | 1:25bc0044cb07 | 143 | //while(mpr121.read(0x000)==NULL){ |
| lalexander37 | 1:25bc0044cb07 | 144 | // dev.printf("Current cookies: %d grams\n\r", (int)weight_sensor()); |
| lalexander37 | 1:25bc0044cb07 | 145 | // dev.printf("Enter the key code into the keypad... \n\r "); |
| lalexander37 | 1:25bc0044cb07 | 146 | //} |
| lalexander37 | 1:25bc0044cb07 | 147 | //pc.printf("got here!"); |
| lalexander37 | 1:25bc0044cb07 | 148 | //wait(1); |
| lalexander37 | 1:25bc0044cb07 | 149 | while (counter<4) { |
| lalexander37 | 1:25bc0044cb07 | 150 | //printf("read: %d \n\r", counter); |
| lalexander37 | 1:25bc0044cb07 | 151 | while(mpr121.read(0x000)==NULL){} |
| lalexander37 | 1:25bc0044cb07 | 152 | int value=mpr121.read(0x000); |
| lalexander37 | 1:25bc0044cb07 | 153 | double lauren = log((double)value); |
| lalexander37 | 1:25bc0044cb07 | 154 | lauren = lauren/log(2.0); |
| lalexander37 | 1:25bc0044cb07 | 155 | testKey[counter] = (int)lauren; |
| lalexander37 | 1:25bc0044cb07 | 156 | dev.printf("You entered: %d\n\r", testKey[counter]); |
| lalexander37 | 1:25bc0044cb07 | 157 | while(mpr121.read(0x000)!=NULL){} |
| lalexander37 | 1:25bc0044cb07 | 158 | counter++; |
| lalexander37 | 1:25bc0044cb07 | 159 | } |
| lalexander37 | 1:25bc0044cb07 | 160 | if(testKey[0] == realKey[0] |
| lalexander37 | 1:25bc0044cb07 | 161 | & testKey[1] == realKey[1] |
| lalexander37 | 1:25bc0044cb07 | 162 | & testKey[2] == realKey[2] |
| lalexander37 | 1:25bc0044cb07 | 163 | & testKey[3] == realKey[3]) { |
| lalexander37 | 1:25bc0044cb07 | 164 | keyFlag = 1; |
| lalexander37 | 1:25bc0044cb07 | 165 | dev.printf("lauren: waiting for 2 factor authentication\n\r"); |
| lalexander37 | 1:25bc0044cb07 | 166 | } else { |
| lalexander37 | 1:25bc0044cb07 | 167 | counter = 0; |
| lalexander37 | 1:25bc0044cb07 | 168 | dev.printf("Please try again\n\r"); |
| lalexander37 | 1:25bc0044cb07 | 169 | } |
| lalexander37 | 1:25bc0044cb07 | 170 | } |
| lalexander37 | 1:25bc0044cb07 | 171 | while (keyFlag == 1 & passFlag == 0) { |
| lalexander37 | 1:25bc0044cb07 | 172 | if( |
| lalexander37 | 1:25bc0044cb07 | 173 | testPassword[0] == realPassword[0] |
| lalexander37 | 1:25bc0044cb07 | 174 | & testPassword[1] == realPassword[1] |
| lalexander37 | 1:25bc0044cb07 | 175 | & testPassword[2] == realPassword[2] |
| lalexander37 | 1:25bc0044cb07 | 176 | & testPassword[3] == realPassword[3] |
| lalexander37 | 1:25bc0044cb07 | 177 | & testPassword[4] == realPassword[4] |
| lalexander37 | 1:25bc0044cb07 | 178 | & testPassword[5] == realPassword[5] |
| lalexander37 | 1:25bc0044cb07 | 179 | & testPassword[6] == realPassword[6] |
| lalexander37 | 1:25bc0044cb07 | 180 | & testPassword[7] == realPassword[7]){ |
| lalexander37 | 1:25bc0044cb07 | 181 | passFlag = 1; |
| lalexander37 | 1:25bc0044cb07 | 182 | dev.printf("Cookie jar is open, press any key to close\n\r"); |
| lalexander37 | 1:25bc0044cb07 | 183 | } |
| lalexander37 | 1:25bc0044cb07 | 184 | else |
| lalexander37 | 1:25bc0044cb07 | 185 | { |
| lalexander37 | 1:25bc0044cb07 | 186 | //pc.printf("\n\rPlease enter a correct password"); |
| lalexander37 | 1:25bc0044cb07 | 187 | dev.printf("Please enter a correct password \n\r"); |
| lalexander37 | 1:25bc0044cb07 | 188 | wait(1.5); |
| lalexander37 | 1:25bc0044cb07 | 189 | } |
| lalexander37 | 1:25bc0044cb07 | 190 | } |
| lalexander37 | 1:25bc0044cb07 | 191 | |
| lalexander37 | 1:25bc0044cb07 | 192 | while (keyFlag == 1 & passFlag == 1) { |
| lalexander37 | 1:25bc0044cb07 | 193 | pc.printf("\n\rUnlocked \n\rPress any button to lock"); |
| lalexander37 | 1:25bc0044cb07 | 194 | //print weight sensor to UART |
| lalexander37 | 1:25bc0044cb07 | 195 | //open the servos |
| lalexander37 | 1:25bc0044cb07 | 196 | open(); |
| lalexander37 | 1:25bc0044cb07 | 197 | while(mpr121.read(0x000)==NULL){ |
| lalexander37 | 1:25bc0044cb07 | 198 | dev.printf("current weight is about -- %d -- grams\n\r", (int)weight_sensor()); |
| lalexander37 | 1:25bc0044cb07 | 199 | wait(0.5); |
| lalexander37 | 1:25bc0044cb07 | 200 | } |
| lalexander37 | 1:25bc0044cb07 | 201 | close(); |
| lalexander37 | 1:25bc0044cb07 | 202 | |
| lalexander37 | 1:25bc0044cb07 | 203 | keyFlag = 0; |
| lalexander37 | 1:25bc0044cb07 | 204 | passFlag = 0; |
| lalexander37 | 1:25bc0044cb07 | 205 | testKey[0] = 0; |
| lalexander37 | 1:25bc0044cb07 | 206 | testKey[1] = 0; |
| lalexander37 | 1:25bc0044cb07 | 207 | testKey[2] = 0; |
| lalexander37 | 1:25bc0044cb07 | 208 | testKey[3] = 0; |
| lalexander37 | 1:25bc0044cb07 | 209 | testPassword[0] = 0; |
| lalexander37 | 1:25bc0044cb07 | 210 | testPassword[1] = 0; |
| lalexander37 | 1:25bc0044cb07 | 211 | testPassword[2] = 0; |
| lalexander37 | 1:25bc0044cb07 | 212 | testPassword[3] = 0; |
| lalexander37 | 1:25bc0044cb07 | 213 | testPassword[4] = 0; |
| lalexander37 | 1:25bc0044cb07 | 214 | testPassword[5] = 0; |
| lalexander37 | 1:25bc0044cb07 | 215 | testPassword[6] = 0; |
| lalexander37 | 1:25bc0044cb07 | 216 | testPassword[7] = 0; |
| lalexander37 | 1:25bc0044cb07 | 217 | } |
| lalexander37 | 1:25bc0044cb07 | 218 | wait(1); |
| 4180_1 | 0:e09703934ff4 | 219 | } |
| 4180_1 | 0:e09703934ff4 | 220 | } |
| 4180_1 | 0:e09703934ff4 | 221 | |
| 4180_1 | 0:e09703934ff4 | 222 |
