Skittles Dispenser by Benjamin Kim and Alex Zdanov
Dependencies: 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library LSM9DS1_Library_cal Servo mbed
Diff: main.cpp
- Revision:
- 1:1099d4500d8a
- Parent:
- 0:2094aa9eeb53
- Child:
- 2:c61e6541a2b4
diff -r 2094aa9eeb53 -r 1099d4500d8a main.cpp --- a/main.cpp Sat Mar 11 21:40:03 2017 +0000 +++ b/main.cpp Sun Mar 12 00:39:38 2017 +0000 @@ -1,175 +1,135 @@ #include "mbed.h" #include "LSM9DS1.h" //Accelerometer -#include "uLCD_4DGL.h" //LCD -#define MAX_BITS 100 // max number of bits -#define WEIGAND_WAIT_TIME 3000 // time to wait for another weigand pulse. +#include "uLCD_4DGL.h" //LCD +#include <mpr121.h> +#include <string> +#include <list> -int databits[MAX_BITS]; // stores all of the data bits -int bitCount; // number of bits currently captured -int flagDone; // goes low when data is currently being captured -int weigand_counter; // countdown until we assume there are no more bits - -unsigned long facilityCode=0; // decoded facility code -unsigned long cardCode=0; // decoded card code - - -DigitalOut LED_GREEN(p15); -DigitalIn dataIn(p10); -DigitalIn Clock(p5); -InterruptIn int0(p10); -InterruptIn int1(p5); - -uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; -DigitalOut myled(LED1); - -/*int LED_GREEN = 11; -int LED_RED = 12; -int BEEP_BEEP = 10;*/ - -// interrupt that happens when INTO goes low (0 bit) -void ISR_INT0() -{ - //uLCD.print("0"); // uncomment this line to display raw binary - bitCount++; - flagDone = 0; - weigand_counter = WEIGAND_WAIT_TIME; - -} +PwmOut motor(p22); //Motor -// interrupt that happens when INT1 goes low (1 bit) -void ISR_INT1() -{ - //uLCD.print("1"); // uncomment this line to display raw binary - databits[bitCount] = 1; - bitCount++; - flagDone = 0; - weigand_counter = WEIGAND_WAIT_TIME; -} +uLCD_4DGL uLCD(p13, p14, p29); // serial tx, serial rx, reset pin; -void setup() -{ - LED_GREEN = 1; - myled = 1; - uLCD.printf("RFID Reader Setup"); - int0.fall(&ISR_INT0); - int1.fall(&ISR_INT1); - /*pinMode(LED_RED, OUTPUT); - pinMode(LED_GREEN, OUTPUT); - pinMode(BEEP_BEEP, OUTPUT); - digitalWrite(LED_RED, HIGH); // High = Off - digitalWrite(BEEP_BEEP, HIGH); // High = off - digitalWrite(LED_GREEN, LOW); // Low = On - pinMode(2, INPUT); // DATA0 (INT0) - pinMode(3, INPUT); // DATA1 (INT1) - - uLCD.begin(9600); - uLCD.println("RFID Readers"); - - // binds the ISR functions to the falling edge of INTO and INT1 - attachInterrupt(0, ISR_INT0, FALLING); - attachInterrupt(1, ISR_INT1, FALLING); - */ - - weigand_counter = WEIGAND_WAIT_TIME; -} +//Mutex LCDMutex; -void printBits() -{ - uLCD.printf("FC = "); - uLCD.printf("%lu", facilityCode); - uLCD.printf(", CC = "); - uLCD.printf("%lu", cardCode); -} - -void loop() -{ - // This waits to make sure that there have been no more data pulses before processing data - if (!flagDone) { - if (--weigand_counter == 0) - flagDone = 1; - } - - // if we have bits and we the weigand counter went out - if (bitCount > 0 && flagDone) { - int i; - - uLCD.printf("Read "); - uLCD.printf("%d", bitCount); - uLCD.printf(" bits. "); - - if (bitCount == 35) { - // 35 bit HID Corporate 1000 format - // facility code = bits 2 to 14 - for (i=2; i<14; i++) { - facilityCode <<=1; - facilityCode |= databits[i]; - } +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); +int passIndex = 0; +int password[4] = {1, 32, 1024, 2048}; +int attempt[4]; - // card code = bits 15 to 34 - for (i=14; i<34; i++) { - cardCode <<=1; - cardCode |= databits[i]; - } - - printBits(); - } else if (bitCount == 26) { - // standard 26 bit format - // facility code = bits 2 to 9 - for (i=1; i<9; i++) { - facilityCode <<=1; - facilityCode |= databits[i]; - } +// Create the interrupt receiver object on pin 26 +InterruptIn interrupt(p21); - // card code = bits 10 to 23 - for (i=9; i<25; i++) { - cardCode <<=1; - cardCode |= databits[i]; - } - - printBits(); - } else { - // you can add other formats if you want! - // uLCD.println("Unable to decode."); - } +// Setup the i2c bus on pins 28 and 27 +I2C i2c(p28, p27); - // cleanup and get ready for the next card - bitCount = 0; - facilityCode = 0; - cardCode = 0; - for (i=0; i<MAX_BITS; i++) { - databits[i] = 0; - } - } -} - - +// Setup the Mpr121: +// constructor(i2c object, i2c address of the mpr121) +Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); Serial pc(USBTX, USBRX); AnalogIn in(p20); //Proximity float voltage; -int IDNum; +void fallInterrupt() +{ + pc.printf("Start interrupt"); + 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; + } + + bool correct = false; + if (value !=0) { + + attempt[passIndex] = value; + //uLCD.printf(" %d %x \n", passIndex, value); + passIndex++; + if (passIndex == 4) { + if (attempt[0] == password[0] && attempt[1] == password[1] && attempt[2] == password[2] && attempt[3] == password[3]) { + correct = true; + pc.printf("Correct!"); + } else { + pc.printf("Incorrect. Try again"); + } + passIndex = 0; + for (int k = 0; k < 4; k++) + attempt[k] = NULL; + } + + } + if(correct) { + motor.write(0.5); + wait(1); + motor.write(0); + /*uLCD.cls(); + uLCD.printf("Enjoy!"); + wait(2.0); + uLCD.cls(); + wait(0.2); + uLCD.printf("Enter code for candy\n");*/ + + } + + //uLCD.printf("number pressed is %d\n", key_code); + led4=key_code & 0x01; + led3=(key_code>>1) & 0x01; + led2=(key_code>>2) & 0x01; + led1=(key_code>>3) & 0x01; + + pc.printf("test4\n"); +} int main() { - uLCD.cls(); - /*LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); + LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); IMU.begin(); if (!IMU.begin()) { pc.printf("Failed to communicate with LSM9DS1.\n"); - }*/ - - setup(); - uLCD.printf("After Setup \n"); - + } + IMU.readGyro(); + float xval = IMU.calcGyro(IMU.gx); + float yval = IMU.calcGyro(IMU.gy); + float zval = IMU.calcGyro(IMU.gz); + + + uLCD.baudrate(3000000); //jack up baud rate to max for fast display + interrupt.fall(&fallInterrupt); + interrupt.mode(PullUp); + + uLCD.printf("Enter code for candy\n"); while(1) { - - loop(); - + //Checking if someone is skaing the machine + while(!IMU.gyroAvailable()) + IMU.readGyro(); + if (abs(xval - IMU.calcGyro(IMU.gx)) > 10 || abs(yval - IMU.calcGyro(IMU.gy)) > 10 || abs(zval - IMU.calcGyro(IMU.gz)) > 10) { + uLCD.printf("Don't shake the machine!\n"); + wait(2.0); + for (int i = 3; i > 0; i--) { + uLCD.cls(); + uLCD.printf("Machine stopped for: %d Second(s)", i); + wait(1.0); + } + uLCD.cls(); + uLCD.printf("Enter code for candy\n"); + } + IMU.readGyro(); + xval = IMU.calcGyro(IMU.gx); + yval = IMU.calcGyro(IMU.gy); + zval = IMU.calcGyro(IMU.gz); } }