Skittles Dispenser by Benjamin Kim and Alex Zdanov
Dependencies: 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library LSM9DS1_Library_cal Servo mbed
main.cpp@0:2094aa9eeb53, 2017-03-11 (annotated)
- Committer:
- bkim334
- Date:
- Sat Mar 11 21:40:03 2017 +0000
- Revision:
- 0:2094aa9eeb53
- Child:
- 1:1099d4500d8a
After trying the RFID;
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 | 0:2094aa9eeb53 | 3 | #include "uLCD_4DGL.h" //LCD |
bkim334 | 0:2094aa9eeb53 | 4 | #define MAX_BITS 100 // max number of bits |
bkim334 | 0:2094aa9eeb53 | 5 | #define WEIGAND_WAIT_TIME 3000 // time to wait for another weigand pulse. |
bkim334 | 0:2094aa9eeb53 | 6 | |
bkim334 | 0:2094aa9eeb53 | 7 | int databits[MAX_BITS]; // stores all of the data bits |
bkim334 | 0:2094aa9eeb53 | 8 | int bitCount; // number of bits currently captured |
bkim334 | 0:2094aa9eeb53 | 9 | int flagDone; // goes low when data is currently being captured |
bkim334 | 0:2094aa9eeb53 | 10 | int weigand_counter; // countdown until we assume there are no more bits |
bkim334 | 0:2094aa9eeb53 | 11 | |
bkim334 | 0:2094aa9eeb53 | 12 | unsigned long facilityCode=0; // decoded facility code |
bkim334 | 0:2094aa9eeb53 | 13 | unsigned long cardCode=0; // decoded card code |
bkim334 | 0:2094aa9eeb53 | 14 | |
bkim334 | 0:2094aa9eeb53 | 15 | |
bkim334 | 0:2094aa9eeb53 | 16 | DigitalOut LED_GREEN(p15); |
bkim334 | 0:2094aa9eeb53 | 17 | DigitalIn dataIn(p10); |
bkim334 | 0:2094aa9eeb53 | 18 | DigitalIn Clock(p5); |
bkim334 | 0:2094aa9eeb53 | 19 | |
bkim334 | 0:2094aa9eeb53 | 20 | |
bkim334 | 0:2094aa9eeb53 | 21 | InterruptIn int0(p10); |
bkim334 | 0:2094aa9eeb53 | 22 | InterruptIn int1(p5); |
bkim334 | 0:2094aa9eeb53 | 23 | |
bkim334 | 0:2094aa9eeb53 | 24 | uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; |
bkim334 | 0:2094aa9eeb53 | 25 | DigitalOut myled(LED1); |
bkim334 | 0:2094aa9eeb53 | 26 | |
bkim334 | 0:2094aa9eeb53 | 27 | /*int LED_GREEN = 11; |
bkim334 | 0:2094aa9eeb53 | 28 | int LED_RED = 12; |
bkim334 | 0:2094aa9eeb53 | 29 | int BEEP_BEEP = 10;*/ |
bkim334 | 0:2094aa9eeb53 | 30 | |
bkim334 | 0:2094aa9eeb53 | 31 | // interrupt that happens when INTO goes low (0 bit) |
bkim334 | 0:2094aa9eeb53 | 32 | void ISR_INT0() |
bkim334 | 0:2094aa9eeb53 | 33 | { |
bkim334 | 0:2094aa9eeb53 | 34 | //uLCD.print("0"); // uncomment this line to display raw binary |
bkim334 | 0:2094aa9eeb53 | 35 | bitCount++; |
bkim334 | 0:2094aa9eeb53 | 36 | flagDone = 0; |
bkim334 | 0:2094aa9eeb53 | 37 | weigand_counter = WEIGAND_WAIT_TIME; |
bkim334 | 0:2094aa9eeb53 | 38 | |
bkim334 | 0:2094aa9eeb53 | 39 | } |
bkim334 | 0:2094aa9eeb53 | 40 | |
bkim334 | 0:2094aa9eeb53 | 41 | // interrupt that happens when INT1 goes low (1 bit) |
bkim334 | 0:2094aa9eeb53 | 42 | void ISR_INT1() |
bkim334 | 0:2094aa9eeb53 | 43 | { |
bkim334 | 0:2094aa9eeb53 | 44 | //uLCD.print("1"); // uncomment this line to display raw binary |
bkim334 | 0:2094aa9eeb53 | 45 | databits[bitCount] = 1; |
bkim334 | 0:2094aa9eeb53 | 46 | bitCount++; |
bkim334 | 0:2094aa9eeb53 | 47 | flagDone = 0; |
bkim334 | 0:2094aa9eeb53 | 48 | weigand_counter = WEIGAND_WAIT_TIME; |
bkim334 | 0:2094aa9eeb53 | 49 | } |
bkim334 | 0:2094aa9eeb53 | 50 | |
bkim334 | 0:2094aa9eeb53 | 51 | void setup() |
bkim334 | 0:2094aa9eeb53 | 52 | { |
bkim334 | 0:2094aa9eeb53 | 53 | LED_GREEN = 1; |
bkim334 | 0:2094aa9eeb53 | 54 | myled = 1; |
bkim334 | 0:2094aa9eeb53 | 55 | uLCD.printf("RFID Reader Setup"); |
bkim334 | 0:2094aa9eeb53 | 56 | int0.fall(&ISR_INT0); |
bkim334 | 0:2094aa9eeb53 | 57 | int1.fall(&ISR_INT1); |
bkim334 | 0:2094aa9eeb53 | 58 | /*pinMode(LED_RED, OUTPUT); |
bkim334 | 0:2094aa9eeb53 | 59 | pinMode(LED_GREEN, OUTPUT); |
bkim334 | 0:2094aa9eeb53 | 60 | pinMode(BEEP_BEEP, OUTPUT); |
bkim334 | 0:2094aa9eeb53 | 61 | digitalWrite(LED_RED, HIGH); // High = Off |
bkim334 | 0:2094aa9eeb53 | 62 | digitalWrite(BEEP_BEEP, HIGH); // High = off |
bkim334 | 0:2094aa9eeb53 | 63 | digitalWrite(LED_GREEN, LOW); // Low = On |
bkim334 | 0:2094aa9eeb53 | 64 | pinMode(2, INPUT); // DATA0 (INT0) |
bkim334 | 0:2094aa9eeb53 | 65 | pinMode(3, INPUT); // DATA1 (INT1) |
bkim334 | 0:2094aa9eeb53 | 66 | |
bkim334 | 0:2094aa9eeb53 | 67 | uLCD.begin(9600); |
bkim334 | 0:2094aa9eeb53 | 68 | uLCD.println("RFID Readers"); |
bkim334 | 0:2094aa9eeb53 | 69 | |
bkim334 | 0:2094aa9eeb53 | 70 | // binds the ISR functions to the falling edge of INTO and INT1 |
bkim334 | 0:2094aa9eeb53 | 71 | attachInterrupt(0, ISR_INT0, FALLING); |
bkim334 | 0:2094aa9eeb53 | 72 | attachInterrupt(1, ISR_INT1, FALLING); |
bkim334 | 0:2094aa9eeb53 | 73 | */ |
bkim334 | 0:2094aa9eeb53 | 74 | |
bkim334 | 0:2094aa9eeb53 | 75 | weigand_counter = WEIGAND_WAIT_TIME; |
bkim334 | 0:2094aa9eeb53 | 76 | } |
bkim334 | 0:2094aa9eeb53 | 77 | |
bkim334 | 0:2094aa9eeb53 | 78 | void printBits() |
bkim334 | 0:2094aa9eeb53 | 79 | { |
bkim334 | 0:2094aa9eeb53 | 80 | uLCD.printf("FC = "); |
bkim334 | 0:2094aa9eeb53 | 81 | uLCD.printf("%lu", facilityCode); |
bkim334 | 0:2094aa9eeb53 | 82 | uLCD.printf(", CC = "); |
bkim334 | 0:2094aa9eeb53 | 83 | uLCD.printf("%lu", cardCode); |
bkim334 | 0:2094aa9eeb53 | 84 | } |
bkim334 | 0:2094aa9eeb53 | 85 | |
bkim334 | 0:2094aa9eeb53 | 86 | void loop() |
bkim334 | 0:2094aa9eeb53 | 87 | { |
bkim334 | 0:2094aa9eeb53 | 88 | // This waits to make sure that there have been no more data pulses before processing data |
bkim334 | 0:2094aa9eeb53 | 89 | if (!flagDone) { |
bkim334 | 0:2094aa9eeb53 | 90 | if (--weigand_counter == 0) |
bkim334 | 0:2094aa9eeb53 | 91 | flagDone = 1; |
bkim334 | 0:2094aa9eeb53 | 92 | } |
bkim334 | 0:2094aa9eeb53 | 93 | |
bkim334 | 0:2094aa9eeb53 | 94 | // if we have bits and we the weigand counter went out |
bkim334 | 0:2094aa9eeb53 | 95 | if (bitCount > 0 && flagDone) { |
bkim334 | 0:2094aa9eeb53 | 96 | int i; |
bkim334 | 0:2094aa9eeb53 | 97 | |
bkim334 | 0:2094aa9eeb53 | 98 | uLCD.printf("Read "); |
bkim334 | 0:2094aa9eeb53 | 99 | uLCD.printf("%d", bitCount); |
bkim334 | 0:2094aa9eeb53 | 100 | uLCD.printf(" bits. "); |
bkim334 | 0:2094aa9eeb53 | 101 | |
bkim334 | 0:2094aa9eeb53 | 102 | if (bitCount == 35) { |
bkim334 | 0:2094aa9eeb53 | 103 | // 35 bit HID Corporate 1000 format |
bkim334 | 0:2094aa9eeb53 | 104 | // facility code = bits 2 to 14 |
bkim334 | 0:2094aa9eeb53 | 105 | for (i=2; i<14; i++) { |
bkim334 | 0:2094aa9eeb53 | 106 | facilityCode <<=1; |
bkim334 | 0:2094aa9eeb53 | 107 | facilityCode |= databits[i]; |
bkim334 | 0:2094aa9eeb53 | 108 | } |
bkim334 | 0:2094aa9eeb53 | 109 | |
bkim334 | 0:2094aa9eeb53 | 110 | // card code = bits 15 to 34 |
bkim334 | 0:2094aa9eeb53 | 111 | for (i=14; i<34; i++) { |
bkim334 | 0:2094aa9eeb53 | 112 | cardCode <<=1; |
bkim334 | 0:2094aa9eeb53 | 113 | cardCode |= databits[i]; |
bkim334 | 0:2094aa9eeb53 | 114 | } |
bkim334 | 0:2094aa9eeb53 | 115 | |
bkim334 | 0:2094aa9eeb53 | 116 | printBits(); |
bkim334 | 0:2094aa9eeb53 | 117 | } else if (bitCount == 26) { |
bkim334 | 0:2094aa9eeb53 | 118 | // standard 26 bit format |
bkim334 | 0:2094aa9eeb53 | 119 | // facility code = bits 2 to 9 |
bkim334 | 0:2094aa9eeb53 | 120 | for (i=1; i<9; i++) { |
bkim334 | 0:2094aa9eeb53 | 121 | facilityCode <<=1; |
bkim334 | 0:2094aa9eeb53 | 122 | facilityCode |= databits[i]; |
bkim334 | 0:2094aa9eeb53 | 123 | } |
bkim334 | 0:2094aa9eeb53 | 124 | |
bkim334 | 0:2094aa9eeb53 | 125 | // card code = bits 10 to 23 |
bkim334 | 0:2094aa9eeb53 | 126 | for (i=9; i<25; i++) { |
bkim334 | 0:2094aa9eeb53 | 127 | cardCode <<=1; |
bkim334 | 0:2094aa9eeb53 | 128 | cardCode |= databits[i]; |
bkim334 | 0:2094aa9eeb53 | 129 | } |
bkim334 | 0:2094aa9eeb53 | 130 | |
bkim334 | 0:2094aa9eeb53 | 131 | printBits(); |
bkim334 | 0:2094aa9eeb53 | 132 | } else { |
bkim334 | 0:2094aa9eeb53 | 133 | // you can add other formats if you want! |
bkim334 | 0:2094aa9eeb53 | 134 | // uLCD.println("Unable to decode."); |
bkim334 | 0:2094aa9eeb53 | 135 | } |
bkim334 | 0:2094aa9eeb53 | 136 | |
bkim334 | 0:2094aa9eeb53 | 137 | // cleanup and get ready for the next card |
bkim334 | 0:2094aa9eeb53 | 138 | bitCount = 0; |
bkim334 | 0:2094aa9eeb53 | 139 | facilityCode = 0; |
bkim334 | 0:2094aa9eeb53 | 140 | cardCode = 0; |
bkim334 | 0:2094aa9eeb53 | 141 | for (i=0; i<MAX_BITS; i++) { |
bkim334 | 0:2094aa9eeb53 | 142 | databits[i] = 0; |
bkim334 | 0:2094aa9eeb53 | 143 | } |
bkim334 | 0:2094aa9eeb53 | 144 | } |
bkim334 | 0:2094aa9eeb53 | 145 | } |
bkim334 | 0:2094aa9eeb53 | 146 | |
bkim334 | 0:2094aa9eeb53 | 147 | |
bkim334 | 0:2094aa9eeb53 | 148 | |
bkim334 | 0:2094aa9eeb53 | 149 | Serial pc(USBTX, USBRX); |
bkim334 | 0:2094aa9eeb53 | 150 | |
bkim334 | 0:2094aa9eeb53 | 151 | AnalogIn in(p20); //Proximity |
bkim334 | 0:2094aa9eeb53 | 152 | float voltage; |
bkim334 | 0:2094aa9eeb53 | 153 | |
bkim334 | 0:2094aa9eeb53 | 154 | int IDNum; |
bkim334 | 0:2094aa9eeb53 | 155 | |
bkim334 | 0:2094aa9eeb53 | 156 | |
bkim334 | 0:2094aa9eeb53 | 157 | int main() |
bkim334 | 0:2094aa9eeb53 | 158 | { |
bkim334 | 0:2094aa9eeb53 | 159 | uLCD.cls(); |
bkim334 | 0:2094aa9eeb53 | 160 | |
bkim334 | 0:2094aa9eeb53 | 161 | /*LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
bkim334 | 0:2094aa9eeb53 | 162 | IMU.begin(); |
bkim334 | 0:2094aa9eeb53 | 163 | if (!IMU.begin()) { |
bkim334 | 0:2094aa9eeb53 | 164 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
bkim334 | 0:2094aa9eeb53 | 165 | }*/ |
bkim334 | 0:2094aa9eeb53 | 166 | |
bkim334 | 0:2094aa9eeb53 | 167 | setup(); |
bkim334 | 0:2094aa9eeb53 | 168 | uLCD.printf("After Setup \n"); |
bkim334 | 0:2094aa9eeb53 | 169 | |
bkim334 | 0:2094aa9eeb53 | 170 | while(1) { |
bkim334 | 0:2094aa9eeb53 | 171 | |
bkim334 | 0:2094aa9eeb53 | 172 | loop(); |
bkim334 | 0:2094aa9eeb53 | 173 | |
bkim334 | 0:2094aa9eeb53 | 174 | } |
bkim334 | 0:2094aa9eeb53 | 175 | } |