Ben Kim / Mbed 2 deprecated Skittles Dispenser using Bluetooth or Touchpad

Dependencies:   4DGL-uLCD-SE HC_SR04_Ultrasonic_Library LSM9DS1_Library_cal Servo mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LSM9DS1.h" //Accelerometer
00003 #include "uLCD_4DGL.h" //LCD  
00004 #include "mbed.h"
00005 #include "Servo.h"
00006 
00007 #include <mpr121.h>
00008 #include <string>
00009 #include <list>
00010 
00011 
00012 
00013 Servo myservo(p22); //servo
00014 
00015 uLCD_4DGL uLCD(p13, p14, p29); // serial tx, serial rx, reset pin;
00016 
00017 //Mutex LCDMutex;
00018 
00019 DigitalOut led1(LED1);
00020 DigitalOut led2(LED2);
00021 DigitalOut led3(LED3);
00022 DigitalOut led4(LED4);
00023 int passIndex = 0;
00024 //Change password here. 
00025 int password[4] = {1, 32, 1024, 2048};
00026 int attempt[4];
00027 int correct = 1; //correct is 1 if they havent put in 3 digits. it's 2 if they're correct and it's 3 if they are incorrect.
00028 
00029 // Create the interrupt receiver object on pin 26
00030 InterruptIn interrupt(p21);
00031 
00032 // Setup the i2c bus on pins 28 and 27
00033 I2C i2c(p28, p27);
00034 
00035 // Setup the Mpr121:
00036 // constructor(i2c object, i2c address of the mpr121)
00037 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00038 
00039 Serial pc(USBTX, USBRX);
00040 
00041 AnalogIn in(p20); //Proximity
00042 float voltage;
00043 
00044 Serial bluemod(p9,p10);
00045 
00046 void fallInterrupt()
00047 {
00048     //pc.printf("Start interrupt");
00049     int key_code=0;
00050     int i=0;
00051     int value=mpr121.read(0x00);
00052     value +=mpr121.read(0x01)<<8;
00053     // LED demo mod by J. Hamblen
00054     //pc.printf("MPR value: %x \r\n", value);
00055     i=0;
00056     // puts key number out to LEDs for demo
00057     for (i=0; i<12; i++) {
00058         if (((value>>i)&0x01)==1) key_code=i+1;
00059     }
00060 
00061     if (value !=0) {
00062 
00063         attempt[passIndex] = value;
00064         //uLCD.printf(" %d    %x \n", passIndex, value);
00065         uLCD.printf("   *");
00066         passIndex++;
00067         if (passIndex == 4) {
00068 
00069             if (attempt[0] == password[0] && attempt[1] == password[1] && attempt[2] == password[2] && attempt[3] == password[3]) {
00070                 correct = 2;
00071             } else {
00072                 correct = 3;
00073             }
00074             passIndex = 0;
00075             for (int k = 0; k < 4; k++)
00076                 attempt[k] = NULL;
00077         }
00078 
00079     }
00080 
00081 
00082     //uLCD.printf("number pressed is %d\n", key_code);
00083     led4=key_code & 0x01;
00084     led3=(key_code>>1) & 0x01;
00085     led2=(key_code>>2) & 0x01;
00086     led1=(key_code>>3) & 0x01;
00087 }
00088 
00089 int main()
00090 {
00091 
00092     //Inclue this if you have gyroscope connected. We only had 3 Serial buses so we were unable to use the gyroscope.
00093     /*LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
00094     IMU.begin();
00095     if (!IMU.begin()) {
00096         pc.printf("Failed to communicate with LSM9DS1.\n");
00097     }
00098     IMU.readGyro();
00099     float xval = IMU.calcGyro(IMU.gx);
00100     float yval = IMU.calcGyro(IMU.gy);
00101     float zval = IMU.calcGyro(IMU.gz);
00102     */
00103 
00104     uLCD.baudrate(3000000); //jack up baud rate to max for fast display
00105     interrupt.fall(&fallInterrupt);
00106     interrupt.mode(PullUp);
00107 
00108     uLCD.printf("Enter code for \ncandy\n");
00109 
00110     while(1) {
00111         //Inclue this if you have gyroscope connected. We only had 3 Serial buses so we were unable to use the gyroscope.
00112         /*//Checking if someone is skaing the machine
00113         while(!IMU.gyroAvailable())
00114             IMU.readGyro();
00115         if (abs(xval - IMU.calcGyro(IMU.gx)) > 10 || abs(yval - IMU.calcGyro(IMU.gy)) > 10 || abs(zval - IMU.calcGyro(IMU.gz)) > 10) {
00116             uLCD.cls();
00117             uLCD.printf("Don't shake the machine!\n");
00118             wait(2.0);
00119             for (int i = 3; i > 0; i--) {
00120                 uLCD.cls();
00121                 uLCD.printf("Machine stopped\n for: %d Second(s)", i);
00122                 wait(1.0);
00123             }
00124             uLCD.cls();
00125             uLCD.printf("Enter code for \n candy\n");
00126         }
00127         IMU.readGyro();
00128         xval = IMU.calcGyro(IMU.gx);
00129         yval = IMU.calcGyro(IMU.gy);
00130         zval = IMU.calcGyro(IMU.gz);
00131         */
00132         while(bluemod.readable()) {
00133             if (bluemod.getc() == 'd') {
00134                 correct = 4;
00135                 bluemod.puts("Dispensing Candy!");
00136             }
00137             if (bluemod.getc() == 'c') {
00138                 voltage = in.read();
00139 
00140             }
00141         }
00142 
00143         if(correct == 2) { // They inputted the correct password
00144             uLCD.cls();
00145             uLCD.printf("Correct Password! \n\n\n Dispensing Candy!");
00146             myservo = 1;
00147             wait(1.3333);
00148             myservo = 0.5;
00149             uLCD.cls();
00150             uLCD.printf("Enjoy!");
00151             wait(2.0);
00152             uLCD.cls();
00153             wait(0.2);
00154             correct = 1;
00155             uLCD.printf("Enter code for \ncandy\n");
00156         }
00157         if(correct == 3) { // they inputted the incorrect password
00158             uLCD.cls();
00159             uLCD.printf("Incorrect Password\n\n Try again.");
00160             wait(2.0);
00161             correct = 1;
00162             uLCD.cls();
00163             uLCD.printf("Enter code for \ncandy\n");
00164         }
00165         if(correct == 4) { // They inputted the correct password
00166             uLCD.cls();
00167             uLCD.printf("Dispensing via\nbluetooth!");
00168             myservo = 1;
00169             wait(1.3333);
00170             myservo = 0.5;
00171             uLCD.cls();
00172             uLCD.printf("Enjoy!");
00173             wait(2.0);
00174             uLCD.cls();
00175             wait(0.2);
00176             correct = 1;
00177             uLCD.printf("Enter code for \ncandy\n");
00178         }
00179 
00180 
00181     }
00182 }