Skittles Dispenser by Benjamin Kim and Alex Zdanov
Dependencies: 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library LSM9DS1_Library_cal Servo mbed
main.cpp@5:2532f81cba87, 2017-03-14 (annotated)
- Committer:
- bkim334
- Date:
- Tue Mar 14 23:42:03 2017 +0000
- Revision:
- 5:2532f81cba87
- Parent:
- 4:8952303a18be
Skittles Dispenser by Benjamin Kim and Alex Zdanov
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 | 4:8952303a18be | 24 | //Change password here. |
bkim334 | 1:1099d4500d8a | 25 | int password[4] = {1, 32, 1024, 2048}; |
bkim334 | 1:1099d4500d8a | 26 | int attempt[4]; |
bkim334 | 2:c61e6541a2b4 | 27 | 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 | 28 | |
bkim334 | 1:1099d4500d8a | 29 | // Create the interrupt receiver object on pin 26 |
bkim334 | 1:1099d4500d8a | 30 | InterruptIn interrupt(p21); |
bkim334 | 0:2094aa9eeb53 | 31 | |
bkim334 | 1:1099d4500d8a | 32 | // Setup the i2c bus on pins 28 and 27 |
bkim334 | 1:1099d4500d8a | 33 | I2C i2c(p28, p27); |
bkim334 | 0:2094aa9eeb53 | 34 | |
bkim334 | 1:1099d4500d8a | 35 | // Setup the Mpr121: |
bkim334 | 1:1099d4500d8a | 36 | // constructor(i2c object, i2c address of the mpr121) |
bkim334 | 1:1099d4500d8a | 37 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
bkim334 | 0:2094aa9eeb53 | 38 | |
bkim334 | 0:2094aa9eeb53 | 39 | Serial pc(USBTX, USBRX); |
bkim334 | 0:2094aa9eeb53 | 40 | |
bkim334 | 0:2094aa9eeb53 | 41 | AnalogIn in(p20); //Proximity |
bkim334 | 0:2094aa9eeb53 | 42 | float voltage; |
bkim334 | 0:2094aa9eeb53 | 43 | |
bkim334 | 2:c61e6541a2b4 | 44 | Serial bluemod(p9,p10); |
bkim334 | 2:c61e6541a2b4 | 45 | |
bkim334 | 1:1099d4500d8a | 46 | void fallInterrupt() |
bkim334 | 1:1099d4500d8a | 47 | { |
bkim334 | 2:c61e6541a2b4 | 48 | //pc.printf("Start interrupt"); |
bkim334 | 1:1099d4500d8a | 49 | int key_code=0; |
bkim334 | 1:1099d4500d8a | 50 | int i=0; |
bkim334 | 1:1099d4500d8a | 51 | int value=mpr121.read(0x00); |
bkim334 | 1:1099d4500d8a | 52 | value +=mpr121.read(0x01)<<8; |
bkim334 | 1:1099d4500d8a | 53 | // LED demo mod by J. Hamblen |
bkim334 | 2:c61e6541a2b4 | 54 | //pc.printf("MPR value: %x \r\n", value); |
bkim334 | 1:1099d4500d8a | 55 | i=0; |
bkim334 | 1:1099d4500d8a | 56 | // puts key number out to LEDs for demo |
bkim334 | 1:1099d4500d8a | 57 | for (i=0; i<12; i++) { |
bkim334 | 1:1099d4500d8a | 58 | if (((value>>i)&0x01)==1) key_code=i+1; |
bkim334 | 1:1099d4500d8a | 59 | } |
bkim334 | 1:1099d4500d8a | 60 | |
bkim334 | 1:1099d4500d8a | 61 | if (value !=0) { |
bkim334 | 1:1099d4500d8a | 62 | |
bkim334 | 1:1099d4500d8a | 63 | attempt[passIndex] = value; |
bkim334 | 1:1099d4500d8a | 64 | //uLCD.printf(" %d %x \n", passIndex, value); |
bkim334 | 2:c61e6541a2b4 | 65 | uLCD.printf(" *"); |
bkim334 | 1:1099d4500d8a | 66 | passIndex++; |
bkim334 | 1:1099d4500d8a | 67 | if (passIndex == 4) { |
bkim334 | 0:2094aa9eeb53 | 68 | |
bkim334 | 1:1099d4500d8a | 69 | if (attempt[0] == password[0] && attempt[1] == password[1] && attempt[2] == password[2] && attempt[3] == password[3]) { |
bkim334 | 2:c61e6541a2b4 | 70 | correct = 2; |
bkim334 | 1:1099d4500d8a | 71 | } else { |
bkim334 | 2:c61e6541a2b4 | 72 | correct = 3; |
bkim334 | 1:1099d4500d8a | 73 | } |
bkim334 | 1:1099d4500d8a | 74 | passIndex = 0; |
bkim334 | 1:1099d4500d8a | 75 | for (int k = 0; k < 4; k++) |
bkim334 | 1:1099d4500d8a | 76 | attempt[k] = NULL; |
bkim334 | 1:1099d4500d8a | 77 | } |
bkim334 | 1:1099d4500d8a | 78 | |
bkim334 | 1:1099d4500d8a | 79 | } |
bkim334 | 1:1099d4500d8a | 80 | |
bkim334 | 1:1099d4500d8a | 81 | |
bkim334 | 1:1099d4500d8a | 82 | //uLCD.printf("number pressed is %d\n", key_code); |
bkim334 | 1:1099d4500d8a | 83 | led4=key_code & 0x01; |
bkim334 | 1:1099d4500d8a | 84 | led3=(key_code>>1) & 0x01; |
bkim334 | 1:1099d4500d8a | 85 | led2=(key_code>>2) & 0x01; |
bkim334 | 1:1099d4500d8a | 86 | led1=(key_code>>3) & 0x01; |
bkim334 | 1:1099d4500d8a | 87 | } |
bkim334 | 0:2094aa9eeb53 | 88 | |
bkim334 | 0:2094aa9eeb53 | 89 | int main() |
bkim334 | 0:2094aa9eeb53 | 90 | { |
bkim334 | 0:2094aa9eeb53 | 91 | |
bkim334 | 4:8952303a18be | 92 | //Inclue this if you have gyroscope connected. We only had 3 Serial buses so we were unable to use the gyroscope. |
bkim334 | 2:c61e6541a2b4 | 93 | /*LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
bkim334 | 0:2094aa9eeb53 | 94 | IMU.begin(); |
bkim334 | 0:2094aa9eeb53 | 95 | if (!IMU.begin()) { |
bkim334 | 0:2094aa9eeb53 | 96 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
bkim334 | 1:1099d4500d8a | 97 | } |
bkim334 | 1:1099d4500d8a | 98 | IMU.readGyro(); |
bkim334 | 1:1099d4500d8a | 99 | float xval = IMU.calcGyro(IMU.gx); |
bkim334 | 1:1099d4500d8a | 100 | float yval = IMU.calcGyro(IMU.gy); |
bkim334 | 1:1099d4500d8a | 101 | float zval = IMU.calcGyro(IMU.gz); |
bkim334 | 2:c61e6541a2b4 | 102 | */ |
bkim334 | 1:1099d4500d8a | 103 | |
bkim334 | 1:1099d4500d8a | 104 | uLCD.baudrate(3000000); //jack up baud rate to max for fast display |
bkim334 | 1:1099d4500d8a | 105 | interrupt.fall(&fallInterrupt); |
bkim334 | 1:1099d4500d8a | 106 | interrupt.mode(PullUp); |
bkim334 | 1:1099d4500d8a | 107 | |
bkim334 | 4:8952303a18be | 108 | uLCD.printf("Enter code for \ncandy\n"); |
bkim334 | 2:c61e6541a2b4 | 109 | |
bkim334 | 0:2094aa9eeb53 | 110 | while(1) { |
bkim334 | 4:8952303a18be | 111 | //Inclue this if you have gyroscope connected. We only had 3 Serial buses so we were unable to use the gyroscope. |
bkim334 | 2:c61e6541a2b4 | 112 | /*//Checking if someone is skaing the machine |
bkim334 | 1:1099d4500d8a | 113 | while(!IMU.gyroAvailable()) |
bkim334 | 1:1099d4500d8a | 114 | IMU.readGyro(); |
bkim334 | 1:1099d4500d8a | 115 | 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 | 116 | uLCD.cls(); |
bkim334 | 1:1099d4500d8a | 117 | uLCD.printf("Don't shake the machine!\n"); |
bkim334 | 1:1099d4500d8a | 118 | wait(2.0); |
bkim334 | 1:1099d4500d8a | 119 | for (int i = 3; i > 0; i--) { |
bkim334 | 1:1099d4500d8a | 120 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 121 | uLCD.printf("Machine stopped\n for: %d Second(s)", i); |
bkim334 | 1:1099d4500d8a | 122 | wait(1.0); |
bkim334 | 1:1099d4500d8a | 123 | } |
bkim334 | 1:1099d4500d8a | 124 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 125 | uLCD.printf("Enter code for \n candy\n"); |
bkim334 | 1:1099d4500d8a | 126 | } |
bkim334 | 1:1099d4500d8a | 127 | IMU.readGyro(); |
bkim334 | 1:1099d4500d8a | 128 | xval = IMU.calcGyro(IMU.gx); |
bkim334 | 1:1099d4500d8a | 129 | yval = IMU.calcGyro(IMU.gy); |
bkim334 | 1:1099d4500d8a | 130 | zval = IMU.calcGyro(IMU.gz); |
bkim334 | 2:c61e6541a2b4 | 131 | */ |
bkim334 | 4:8952303a18be | 132 | while(bluemod.readable()) { |
bkim334 | 2:c61e6541a2b4 | 133 | if (bluemod.getc() == 'd') { |
bkim334 | 4:8952303a18be | 134 | correct = 4; |
bkim334 | 2:c61e6541a2b4 | 135 | bluemod.puts("Dispensing Candy!"); |
bkim334 | 2:c61e6541a2b4 | 136 | } |
bkim334 | 2:c61e6541a2b4 | 137 | if (bluemod.getc() == 'c') { |
bkim334 | 2:c61e6541a2b4 | 138 | voltage = in.read(); |
bkim334 | 2:c61e6541a2b4 | 139 | |
bkim334 | 2:c61e6541a2b4 | 140 | } |
bkim334 | 4:8952303a18be | 141 | } |
bkim334 | 2:c61e6541a2b4 | 142 | |
bkim334 | 2:c61e6541a2b4 | 143 | if(correct == 2) { // They inputted the correct password |
bkim334 | 2:c61e6541a2b4 | 144 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 145 | uLCD.printf("Correct Password! \n\n\n Dispensing Candy!"); |
bkim334 | 3:2087575bfd0e | 146 | myservo = 1; |
bkim334 | 3:2087575bfd0e | 147 | wait(1.3333); |
bkim334 | 3:2087575bfd0e | 148 | myservo = 0.5; |
bkim334 | 2:c61e6541a2b4 | 149 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 150 | uLCD.printf("Enjoy!"); |
bkim334 | 2:c61e6541a2b4 | 151 | wait(2.0); |
bkim334 | 2:c61e6541a2b4 | 152 | uLCD.cls(); |
bkim334 | 2:c61e6541a2b4 | 153 | wait(0.2); |
bkim334 | 2:c61e6541a2b4 | 154 | correct = 1; |
bkim334 | 4:8952303a18be | 155 | uLCD.printf("Enter code for \ncandy\n"); |
bkim334 | 2:c61e6541a2b4 | 156 | } |
bkim334 | 2:c61e6541a2b4 | 157 | if(correct == 3) { // they inputted the incorrect password |
bkim334 | 2:c61e6541a2b4 | 158 | uLCD.cls(); |
bkim334 | 4:8952303a18be | 159 | uLCD.printf("Incorrect Password\n\n Try again."); |
bkim334 | 2:c61e6541a2b4 | 160 | wait(2.0); |
bkim334 | 2:c61e6541a2b4 | 161 | correct = 1; |
bkim334 | 2:c61e6541a2b4 | 162 | uLCD.cls(); |
bkim334 | 4:8952303a18be | 163 | uLCD.printf("Enter code for \ncandy\n"); |
bkim334 | 4:8952303a18be | 164 | } |
bkim334 | 4:8952303a18be | 165 | if(correct == 4) { // They inputted the correct password |
bkim334 | 4:8952303a18be | 166 | uLCD.cls(); |
bkim334 | 4:8952303a18be | 167 | uLCD.printf("Dispensing via\nbluetooth!"); |
bkim334 | 4:8952303a18be | 168 | myservo = 1; |
bkim334 | 4:8952303a18be | 169 | wait(1.3333); |
bkim334 | 4:8952303a18be | 170 | myservo = 0.5; |
bkim334 | 4:8952303a18be | 171 | uLCD.cls(); |
bkim334 | 4:8952303a18be | 172 | uLCD.printf("Enjoy!"); |
bkim334 | 4:8952303a18be | 173 | wait(2.0); |
bkim334 | 4:8952303a18be | 174 | uLCD.cls(); |
bkim334 | 4:8952303a18be | 175 | wait(0.2); |
bkim334 | 4:8952303a18be | 176 | correct = 1; |
bkim334 | 4:8952303a18be | 177 | uLCD.printf("Enter code for \ncandy\n"); |
bkim334 | 2:c61e6541a2b4 | 178 | } |
bkim334 | 2:c61e6541a2b4 | 179 | |
bkim334 | 2:c61e6541a2b4 | 180 | |
bkim334 | 0:2094aa9eeb53 | 181 | } |
bkim334 | 0:2094aa9eeb53 | 182 | } |