Skittles Dispenser by Benjamin Kim and Alex Zdanov

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

Revision:
2:c61e6541a2b4
Parent:
1:1099d4500d8a
Child:
3:2087575bfd0e
--- a/main.cpp	Sun Mar 12 00:39:38 2017 +0000
+++ b/main.cpp	Tue Mar 14 21:30:21 2017 +0000
@@ -1,13 +1,16 @@
 #include "mbed.h"
 #include "LSM9DS1.h" //Accelerometer
 #include "uLCD_4DGL.h" //LCD  
+#include "mbed.h"
+#include "Servo.h"
+
 #include <mpr121.h>
 #include <string>
 #include <list>
 
 
 
-PwmOut motor(p22); //Motor
+Servo myservo(p22); //servo
 
 uLCD_4DGL uLCD(p13, p14, p29); // serial tx, serial rx, reset pin;
 
@@ -20,6 +23,7 @@
 int passIndex = 0;
 int password[4] = {1, 32, 1024, 2048};
 int attempt[4];
+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.
 
 // Create the interrupt receiver object on pin 26
 InterruptIn interrupt(p21);
@@ -36,34 +40,35 @@
 AnalogIn in(p20); //Proximity
 float voltage;
 
+Serial bluemod(p9,p10);
+
 void fallInterrupt()
 {
-    pc.printf("Start interrupt");
+    //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);
+    //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);
+        uLCD.printf("   *");
         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!");
+                correct = 2;
             } else {
-                pc.printf("Incorrect. Try again");
+                correct = 3;
             }
             passIndex = 0;
             for (int k = 0; k < 4; k++)
@@ -71,32 +76,19 @@
         }
 
     }
-    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()
 {
 
-    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");
@@ -105,31 +97,70 @@
     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");
+    uLCD.printf("Enter code for \n candy\n");
+
+    myservo.write(1);
     while(1) {
-        //Checking if someone is skaing the machine
+        /*//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.cls();
             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);
+                uLCD.printf("Machine stopped\n for: %d Second(s)", i);
                 wait(1.0);
             }
             uLCD.cls();
-            uLCD.printf("Enter code for candy\n");
+            uLCD.printf("Enter code for \n candy\n");
         }
         IMU.readGyro();
         xval = IMU.calcGyro(IMU.gx);
         yval = IMU.calcGyro(IMU.gy);
         zval = IMU.calcGyro(IMU.gz);
+        */
+        /*while(bluemod.readable()) {
+            if (bluemod.getc() == 'd') {
+                correct = 2;
+                bluemod.puts("Dispensing Candy!");
+            }
+            if (bluemod.getc() == 'c') {
+                voltage = in.read();
+
+            }
+        }*/
+
+        if(correct == 2) { // They inputted the correct password
+            uLCD.cls();
+            uLCD.printf("Correct Password! \n\n\n Dispensing Candy!");
+            //myservo = 0.5;
+            wait(3);
+            //myservo = 0;
+            uLCD.cls();
+            uLCD.printf("Enjoy!");
+            wait(2.0);
+            uLCD.cls();
+            wait(0.2);
+            correct = 1;
+            uLCD.printf("Enter code for \n candy\n");
+        }
+        if(correct == 3) { // they inputted the incorrect password
+            uLCD.cls();
+            uLCD.printf("Incorrect Password\n\n Try again, bitch.");
+            wait(2.0);
+            correct = 1;
+            uLCD.cls();
+            uLCD.printf("Enter code for \n candy\n");
+        }
+
+
     }
 }