Skittles Dispenser by Benjamin Kim and Alex Zdanov
Dependencies: 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library LSM9DS1_Library_cal Servo mbed
main.cpp@3:2087575bfd0e, 2017-03-14 (annotated)
- Committer:
- bkim334
- Date:
- Tue Mar 14 21:54:45 2017 +0000
- Revision:
- 3:2087575bfd0e
- Parent:
- 2:c61e6541a2b4
- Child:
- 4:8952303a18be
before box. everything working. ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bkim334 | 0:2094aa9eeb53 | 1 | #include "mbed.h" |
bkim334 | 0:2094aa9eeb53 | 2 | #include "LSM9DS1.h" //Accelerometer |
bkim334 | 1:1099d4500d8a | 3 | #include "uLCD_4DGL.h" //LCD |
bkim334 | 2:c61e6541a2b4 | 4 | #include "mbed.h" |
bkim334 | 2:c61e6541a2b4 | 5 | #include "Servo.h" |
bkim334 | 2:c61e6541a2b4 | 6 | |
bkim334 | 1:1099d4500d8a | 7 | #include <mpr121.h> |
bkim334 | 1:1099d4500d8a | 8 | #include <string> |
bkim334 | 1:1099d4500d8a | 9 | #include <list> |
bkim334 | 0:2094aa9eeb53 | 10 | |
bkim334 | 0:2094aa9eeb53 | 11 | |
bkim334 | 0:2094aa9eeb53 | 12 | |
bkim334 | 2:c61e6541a2b4 | 13 | Servo myservo(p22); //servo |
bkim334 | 0:2094aa9eeb53 | 14 | |
bkim334 | 1:1099d4500d8a | 15 | uLCD_4DGL uLCD(p13, p14, p29); // serial tx, serial rx, reset pin; |
bkim334 | 0:2094aa9eeb53 | 16 | |
bkim334 | 1:1099d4500d8a | 17 | //Mutex LCDMutex; |
bkim334 | 0:2094aa9eeb53 | 18 | |
bkim334 | 1:1099d4500d8a | 19 | DigitalOut led1(LED1); |
bkim334 | 1:1099d4500d8a | 20 | DigitalOut led2(LED2); |
bkim334 | 1:1099d4500d8a | 21 | DigitalOut led3(LED3); |
bkim334 | 1:1099d4500d8a | 22 | DigitalOut led4(LED4); |
bkim334 | 1:1099d4500d8a | 23 | int passIndex = 0; |
bkim334 | 1:1099d4500d8a | 24 | int password[4] = {1, 32, 1024, 2048}; |
bkim334 | 1:1099d4500d8a | 25 | int attempt[4]; |
bkim334 | 2:c61e6541a2b4 | 26 | 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. |
bkim334 | 0:2094aa9eeb53 | 27 | |
bkim334 | 1:1099d4500d8a | 28 | // Create the interrupt receiver object on pin 26 |
bkim334 | 1:1099d4500d8a | 29 | InterruptIn interrupt(p21); |
bkim334 | 0:2094aa9eeb53 | 30 | |
bkim334 | 1:1099d4500d8a | 31 | // Setup the i2c bus on pins 28 and 27 |
bkim334 | 1:1099d4500d8a | 32 | I2C i2c(p28, p27); |
bkim334 | 0:2094aa9eeb53 | 33 | |
bkim334 | 1:1099d4500d8a | 34 | // Setup the Mpr121: |
bkim334 | 1:1099d4500d8a | 35 | // constructor(i2c object, i2c address of the mpr121) |
bkim334 | 1:1099d4500d8a | 36 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
bkim334 | 0:2094aa9eeb53 | 37 | |
bkim334 | 0:2094aa9eeb53 | 38 | Serial pc(USBTX, USBRX); |
bkim334 | 0:2094aa9eeb53 | 39 | |
bkim334 | 0:2094aa9eeb53 | 40 | AnalogIn in(p20); //Proximity |
bkim334 | 0:2094aa9eeb53 | 41 | float voltage; |
bkim334 | 0:2094aa9eeb53 | 42 | |
bkim334 | 2:c61e6541a2b4 | 43 | Serial bluemod(p9,p10); |
bkim334 | 2:c61e6541a2b4 | 44 | |
bkim334 | 1:1099d4500d8a | 45 | void fallInterrupt() |
bkim334 | 1:1099d4500d8a | 46 | { |
bkim334 | 2:c61e6541a2b4 | 47 | //pc.printf("Start interrupt"); |
bkim334 | 1:1099d4500d8a | 48 | int key_code=0; |
bkim334 | 1:1099d4500d8a | 49 | int i=0; |
bkim334 | 1:1099d4500d8a | 50 | int value=mpr121.read(0x00); |
bkim334 | 1:1099d4500d8a | 51 | value +=mpr121.read(0x01)<<8; |
bkim334 | 1:1099d4500d8a | 52 | // LED demo mod by J. Hamblen |
bkim334 | 2:c61e6541a2b4 | 53 | //pc.printf("MPR value: %x \r\n", value); |
bkim334 | 1:1099d4500d8a | 54 | i=0; |
bkim334 | 1:1099d4500d8a | 55 | // puts key number out to LEDs for demo |
bkim334 | 1:1099d4500d8a | 56 | for (i=0; i<12; i++) { |
bkim334 | 1:1099d4500d8a | 57 | if (((value>>i)&0x01)==1) key_code=i+1; |
bkim334 | 1:1099d4500d8a | 58 | } |
bkim334 | 1:1099d4500d8a | 59 | |
bkim334 | 1:1099d4500d8a | 60 | if (value !=0) { |
bkim334 | 1:1099d4500d8a | 61 | |
bkim334 | 1:1099d4500d8a | 62 | attempt[passIndex] = value; |
bkim334 | 1:1099d4500d8a | 63 | //uLCD.printf(" %d %x \n", passIndex, value); |
bkim334 | 2:c61e6541a2b4 | 64 | uLCD.printf(" *"); |
bkim334 | 1:1099d4500d8a | 65 | passIndex++; |
bkim334 | 1:1099d4500d8a | 66 | if (passIndex == 4) { |
bkim334 | 0:2094aa9eeb53 | 67 | |
bkim334 | 1:1099d4500d8a | 68 | if (attempt[0] == password[0] && attempt[1] == password[1] && attempt[2] == password[2] && attempt[3] == password[3]) { |
bkim334 | 2:c61e6541a2b4 | 69 | correct = 2; |
bkim334 | 1:1099d4500d8a | 70 | } else { |
bkim334 | 2:c61e6541a2b4 | 71 | correct = 3; |
bkim334 | 1:1099d4500d8a | 72 | } |
bkim334 | 1:1099d4500d8a | 73 | passIndex = 0; |
bkim334 | 1:1099d4500d8a | 74 | for (int k = 0; k < 4; k++) |
bkim334 | 1:1099d4500d8a | 75 | attempt[k] = NULL; |
bkim334 | 1:1099d4500d8a | 76 | } |
bkim334 | 1:1099d4500d8a | 77 | |
bkim334 | 1:1099d4500d8a | 78 | } |
bkim334 | 1:1099d4500d8a | 79 | |
bkim334 | 1:1099d4500d8a | 80 | |
bkim334 | 1:1099d4500d8a | 81 | //uLCD.printf("number pressed is %d\n", key_code); |
bkim334 | 1:1099d4500d8a | 82 | led4=key_code & 0x01; |
bkim334 | 1:1099d4500d8a | 83 | led3=(key_code>>1) & 0x01; |
bkim334 | 1:1099d4500d8a | 84 | led2=(key_code>>2) & 0x01; |
bkim334 | 1:1099d4500d8a | 85 | led1=(key_code>>3) & 0x01; |
bkim334 | 1:1099d4500d8a | 86 | } |
bkim334 | 0:2094aa9eeb53 | 87 | |
bkim334 | 0:2094aa9eeb53 | 88 | int main() |
bkim334 | 0:2094aa9eeb53 | 89 | { |
bkim334 | 0:2094aa9eeb53 | 90 | |
bkim334 | 2:c61e6541a2b4 | 91 | /*LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
bkim334 | 0:2094aa9eeb53 | 92 | IMU.begin(); |
bkim334 | 0:2094aa9eeb53 | 93 | if (!IMU.begin()) { |
bkim334 | 0:2094aa9eeb53 | 94 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
bkim334 | 1:1099d4500d8a | 95 | } |
bkim334 | 1:1099d4500d8a | 96 | IMU.readGyro(); |
bkim334 | 1:1099d4500d8a | 97 | float xval = IMU.calcGyro(IMU.gx); |
bkim334 | 1:1099d4500d8a | 98 | float yval = IMU.calcGyro(IMU.gy); |
bkim334 | 1:1099d4500d8a | 99 | float zval = IMU.calcGyro(IMU.gz); |
bkim334 | 2:c61e6541a2b4 | 100 | */ |
bkim334 | 1:1099d4500d8a | 101 | |
bkim334 | 1:1099d4500d8a | 102 | uLCD.baudrate(3000000); //jack up baud rate to max for fast display |
bkim334 | 1:1099d4500d8a | 103 | interrupt.fall(&fallInterrupt); |
bkim334 | 1:1099d4500d8a | 104 | interrupt.mode(PullUp); |
bkim334 | 1:1099d4500d8a | 105 | |
bkim334 | 2:c61e6541a2b4 | 106 | uLCD.printf("Enter code for \n candy\n"); |
bkim334 | 2:c61e6541a2b4 | 107 | |
bkim334 | 0:2094aa9eeb53 | 108 | while(1) { |
bkim334 | 2:c61e6541a2b4 | 109 | /*//Checking if someone is skaing the machine |
bkim334 | 1:1099d4500d8a | 110 | while(!IMU.gyroAvailable()) |
bkim334 | 1:1099d4500d8a | 111 | IMU.readGyro(); |
bkim334 | 1:1099d4500d8a | 112 | if (abs(xval - IMU.calcGyro(IMU.gx)) > 10 || abs(yval - IMU.calcGyro(IMU.gy)) > 10 || abs(zval - IMU.calcGyro(IMU.gz)) > 10) { |
bkim334 | 2:c61e6541a2b4 | 113 | uLCD.cls(); |
bkim334 | 1:1099d4500d8a | 114 | uLCD.printf("Don't shake the machine!\n"); |
bkim334 | 1:1099d4500d8a | 115 | wait(2.0); |
bkim334 | 1:1099d4500d8a | 116 | for (int i = 3; i > 0; i--) { |
bkim334 | 1:1099d4500d8a | 117 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 118 | uLCD.printf("Machine stopped\n for: %d Second(s)", i); |
bkim334 | 1:1099d4500d8a | 119 | wait(1.0); |
bkim334 | 1:1099d4500d8a | 120 | } |
bkim334 | 1:1099d4500d8a | 121 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 122 | uLCD.printf("Enter code for \n candy\n"); |
bkim334 | 1:1099d4500d8a | 123 | } |
bkim334 | 1:1099d4500d8a | 124 | IMU.readGyro(); |
bkim334 | 1:1099d4500d8a | 125 | xval = IMU.calcGyro(IMU.gx); |
bkim334 | 1:1099d4500d8a | 126 | yval = IMU.calcGyro(IMU.gy); |
bkim334 | 1:1099d4500d8a | 127 | zval = IMU.calcGyro(IMU.gz); |
bkim334 | 2:c61e6541a2b4 | 128 | */ |
bkim334 | 2:c61e6541a2b4 | 129 | /*while(bluemod.readable()) { |
bkim334 | 2:c61e6541a2b4 | 130 | if (bluemod.getc() == 'd') { |
bkim334 | 2:c61e6541a2b4 | 131 | correct = 2; |
bkim334 | 2:c61e6541a2b4 | 132 | bluemod.puts("Dispensing Candy!"); |
bkim334 | 2:c61e6541a2b4 | 133 | } |
bkim334 | 2:c61e6541a2b4 | 134 | if (bluemod.getc() == 'c') { |
bkim334 | 2:c61e6541a2b4 | 135 | voltage = in.read(); |
bkim334 | 2:c61e6541a2b4 | 136 | |
bkim334 | 2:c61e6541a2b4 | 137 | } |
bkim334 | 2:c61e6541a2b4 | 138 | }*/ |
bkim334 | 2:c61e6541a2b4 | 139 | |
bkim334 | 2:c61e6541a2b4 | 140 | if(correct == 2) { // They inputted the correct password |
bkim334 | 2:c61e6541a2b4 | 141 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 142 | uLCD.printf("Correct Password! \n\n\n Dispensing Candy!"); |
bkim334 | 3:2087575bfd0e | 143 | myservo = 1; |
bkim334 | 3:2087575bfd0e | 144 | wait(1.3333); |
bkim334 | 3:2087575bfd0e | 145 | myservo = 0.5; |
bkim334 | 2:c61e6541a2b4 | 146 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 147 | uLCD.printf("Enjoy!"); |
bkim334 | 2:c61e6541a2b4 | 148 | wait(2.0); |
bkim334 | 2:c61e6541a2b4 | 149 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 150 | wait(0.2); |
bkim334 | 2:c61e6541a2b4 | 151 | correct = 1; |
bkim334 | 2:c61e6541a2b4 | 152 | uLCD.printf("Enter code for \n candy\n"); |
bkim334 | 2:c61e6541a2b4 | 153 | } |
bkim334 | 2:c61e6541a2b4 | 154 | if(correct == 3) { // they inputted the incorrect password |
bkim334 | 2:c61e6541a2b4 | 155 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 156 | uLCD.printf("Incorrect Password\n\n Try again, bitch."); |
bkim334 | 2:c61e6541a2b4 | 157 | wait(2.0); |
bkim334 | 2:c61e6541a2b4 | 158 | correct = 1; |
bkim334 | 2:c61e6541a2b4 | 159 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 160 | uLCD.printf("Enter code for \n candy\n"); |
bkim334 | 2:c61e6541a2b4 | 161 | } |
bkim334 | 2:c61e6541a2b4 | 162 | |
bkim334 | 2:c61e6541a2b4 | 163 | |
bkim334 | 0:2094aa9eeb53 | 164 | } |
bkim334 | 0:2094aa9eeb53 | 165 | } |