This the the improved and updated project and program for the earlier automatic pill dispenser project. Includes and rfid, stepper, servo, led-ldr and a load sensor integrate. time stamp is sent to the cloud and saved..
Dependencies: EthernetInterface FXAS21002 FXOS8700 M2XStreamClient MFRC522 Servo jsonlite mbed-rtos mbed
Fork of Accel_Mag_Gyro_SensorStream_K64F_AGM01_M2X by
main.cpp@2:104683639374, 2018-04-26 (annotated)
- Committer:
- khp007
- Date:
- Thu Apr 26 17:52:37 2018 +0000
- Revision:
- 2:104683639374
- Parent:
- 1:5e587e213940
automated pill dispenser project model 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
khp007 | 2:104683639374 | 1 | /////////Automated Pill Dispenser///////////// |
AswinSivakumar | 0:4fd1b0bd1594 | 2 | #include "mbed.h" |
AswinSivakumar | 0:4fd1b0bd1594 | 3 | #include "M2XStreamClient.h" |
AswinSivakumar | 0:4fd1b0bd1594 | 4 | #include "EthernetInterface.h" |
khp007 | 2:104683639374 | 5 | #include "MFRC522.h" |
khp007 | 2:104683639374 | 6 | #include "Servo.h" |
AswinSivakumar | 0:4fd1b0bd1594 | 7 | |
khp007 | 2:104683639374 | 8 | #define SPI_SCLK D13 |
khp007 | 2:104683639374 | 9 | #define SPI_MISO D12 |
khp007 | 2:104683639374 | 10 | #define SPI_MOSI D11 //pints for the RFID reader |
khp007 | 2:104683639374 | 11 | #define SPI_CS D10 |
khp007 | 2:104683639374 | 12 | #define MF_RESET D9 |
khp007 | 2:104683639374 | 13 | |
khp007 | 2:104683639374 | 14 | MFRC522 RfChip(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET); //for the RFID chip |
khp007 | 2:104683639374 | 15 | DigitalOut LedRed (LED_RED); // LED Red |
khp007 | 2:104683639374 | 16 | DigitalOut LedGreen (LED_GREEN); // LED green |
khp007 | 2:104683639374 | 17 | DigitalOut LedBlue (LED_BLUE); // LED blue |
khp007 | 2:104683639374 | 18 | |
khp007 | 2:104683639374 | 19 | DigitalOut pin1(D4); //output pins for the stepper motor |
khp007 | 2:104683639374 | 20 | DigitalOut pin2(D5); |
khp007 | 2:104683639374 | 21 | DigitalOut pin3(D6); |
khp007 | 2:104683639374 | 22 | DigitalOut pin4(D7); |
khp007 | 2:104683639374 | 23 | |
khp007 | 2:104683639374 | 24 | AnalogIn LDRpin(A0); //analog output pin for the LDR |
khp007 | 2:104683639374 | 25 | |
khp007 | 2:104683639374 | 26 | AnalogIn gp1(A2); //Pins for the weight sensor |
khp007 | 2:104683639374 | 27 | AnalogIn gp2(A3); |
khp007 | 2:104683639374 | 28 | |
khp007 | 2:104683639374 | 29 | Serial pc(USBTX, USBRX); |
khp007 | 2:104683639374 | 30 | Servo LRmyservo(PTC5); |
AswinSivakumar | 0:4fd1b0bd1594 | 31 | |
AswinSivakumar | 0:4fd1b0bd1594 | 32 | // Set Sensor Stream details |
khp007 | 2:104683639374 | 33 | char deviceId[] = "8b23ea86abb3aa26711d4d6b92cbfe12"; // Device you want to push to |
khp007 | 2:104683639374 | 34 | char streamLdr[] = "ldr_data"; // Stream you want to push to |
khp007 | 2:104683639374 | 35 | char streamLoad[] = "load_data"; // Stream you want to push to |
khp007 | 2:104683639374 | 36 | char m2xKey[] = "e952c10fdf5d62986f9e229564c92c83"; // Your M2X API Key or Master API Key |
AswinSivakumar | 0:4fd1b0bd1594 | 37 | |
khp007 | 2:104683639374 | 38 | //Sequence for the stepper motor |
khp007 | 2:104683639374 | 39 | int stepSequence[8][4] = |
AswinSivakumar | 0:4fd1b0bd1594 | 40 | { |
khp007 | 2:104683639374 | 41 | {0,0,0,1}, |
khp007 | 2:104683639374 | 42 | {0,0,1,1}, |
khp007 | 2:104683639374 | 43 | {0,0,1,0}, |
khp007 | 2:104683639374 | 44 | {0,1,1,0}, |
khp007 | 2:104683639374 | 45 | {0,1,0,0}, |
khp007 | 2:104683639374 | 46 | {1,1,0,0}, |
khp007 | 2:104683639374 | 47 | {1,0,0,0}, |
khp007 | 2:104683639374 | 48 | {1,0,0,1}, |
khp007 | 2:104683639374 | 49 | }; |
khp007 | 2:104683639374 | 50 | |
khp007 | 2:104683639374 | 51 | int main(){ |
khp007 | 2:104683639374 | 52 | wait_ms(3000); |
khp007 | 2:104683639374 | 53 | //turn all LEDs off |
khp007 | 2:104683639374 | 54 | LedRed = 1; |
khp007 | 2:104683639374 | 55 | LedGreen = 1; |
khp007 | 2:104683639374 | 56 | LedBlue = 1; |
khp007 | 2:104683639374 | 57 | |
AswinSivakumar | 0:4fd1b0bd1594 | 58 | // Intialize Ethernet connection |
khp007 | 2:104683639374 | 59 | EthernetInterface eth; |
khp007 | 2:104683639374 | 60 | eth.init(); |
khp007 | 2:104683639374 | 61 | eth.connect(); |
khp007 | 2:104683639374 | 62 | printf("Success. Connected!. Device IP Address is %s\r\n", eth.getIPAddress()); |
khp007 | 2:104683639374 | 63 | // Initialize the M2X client |
khp007 | 2:104683639374 | 64 | Client client; |
khp007 | 2:104683639374 | 65 | M2XStreamClient m2xClient(&client, m2xKey); |
khp007 | 2:104683639374 | 66 | int ret; |
khp007 | 2:104683639374 | 67 | |
khp007 | 2:104683639374 | 68 | //float ldr_time[3]; |
khp007 | 2:104683639374 | 69 | float ldr_data=0.0; |
khp007 | 2:104683639374 | 70 | //float load_time[3]; |
khp007 | 2:104683639374 | 71 | float load_data=0.0; |
khp007 | 2:104683639374 | 72 | |
khp007 | 2:104683639374 | 73 | RfChip.PCD_Init(); // initialize RDID chip |
khp007 | 2:104683639374 | 74 | |
khp007 | 2:104683639374 | 75 | while (LedRed == 1) { |
khp007 | 2:104683639374 | 76 | //code for stepper motor |
khp007 | 2:104683639374 | 77 | int count_third = 0; |
khp007 | 2:104683639374 | 78 | for(int j = 0; j <= 512; j++){ |
khp007 | 2:104683639374 | 79 | if (count_third%3 == 0){ // rotate a third of a whole revolution |
khp007 | 2:104683639374 | 80 | for (int i = 0; i < 8; i++){ |
khp007 | 2:104683639374 | 81 | pin1 = stepSequence[i][0]; |
khp007 | 2:104683639374 | 82 | pin2 = stepSequence[i][1]; |
khp007 | 2:104683639374 | 83 | pin3 = stepSequence[i][2]; |
khp007 | 2:104683639374 | 84 | pin4 = stepSequence[i][3]; |
khp007 | 2:104683639374 | 85 | wait_ms(2); |
khp007 | 2:104683639374 | 86 | } |
khp007 | 2:104683639374 | 87 | } |
khp007 | 2:104683639374 | 88 | count_third++; |
khp007 | 2:104683639374 | 89 | } |
khp007 | 2:104683639374 | 90 | LedRed = 1; |
khp007 | 2:104683639374 | 91 | LedGreen = 1; |
khp007 | 2:104683639374 | 92 | LedBlue = 1; |
khp007 | 2:104683639374 | 93 | wait_ms(500); |
khp007 | 2:104683639374 | 94 | |
khp007 | 2:104683639374 | 95 | //code for RFID reader |
khp007 | 2:104683639374 | 96 | for ( int k = 0; k < 40; k++){ |
khp007 | 2:104683639374 | 97 | if ( ! RfChip.PICC_IsNewCardPresent()){ |
khp007 | 2:104683639374 | 98 | wait_ms(50); |
khp007 | 2:104683639374 | 99 | continue; |
khp007 | 2:104683639374 | 100 | } |
khp007 | 2:104683639374 | 101 | LedRed = 0; |
AswinSivakumar | 0:4fd1b0bd1594 | 102 | |
khp007 | 2:104683639374 | 103 | // Select one of the cards |
khp007 | 2:104683639374 | 104 | if ( ! RfChip.PICC_ReadCardSerial()){ |
khp007 | 2:104683639374 | 105 | wait_ms(50); |
khp007 | 2:104683639374 | 106 | continue; |
khp007 | 2:104683639374 | 107 | } |
khp007 | 2:104683639374 | 108 | // Print Card type |
khp007 | 2:104683639374 | 109 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
khp007 | 2:104683639374 | 110 | wait_ms(100); |
khp007 | 2:104683639374 | 111 | } |
khp007 | 2:104683639374 | 112 | } |
khp007 | 2:104683639374 | 113 | /////Servo Time!!!!!!///// |
khp007 | 2:104683639374 | 114 | float range = .0009; |
khp007 | 2:104683639374 | 115 | LRmyservo.calibrate(range,45); |
khp007 | 2:104683639374 | 116 | bool stop_Servo = false; |
khp007 | 2:104683639374 | 117 | bool pill_Detected = false; |
khp007 | 2:104683639374 | 118 | while (stop_Servo == false) { |
khp007 | 2:104683639374 | 119 | LRmyservo = 1.45; |
khp007 | 2:104683639374 | 120 | wait_ms(400); |
khp007 | 2:104683639374 | 121 | LRmyservo = 0.45; |
khp007 | 2:104683639374 | 122 | wait_ms(4000); |
khp007 | 2:104683639374 | 123 | |
khp007 | 2:104683639374 | 124 | for (int m = 0; m < 20; m++){ |
khp007 | 2:104683639374 | 125 | float x = LDRpin * 1000; |
khp007 | 2:104683639374 | 126 | if (x < 140){ |
khp007 | 2:104683639374 | 127 | pill_Detected = true; |
khp007 | 2:104683639374 | 128 | } |
khp007 | 2:104683639374 | 129 | } |
khp007 | 2:104683639374 | 130 | float x = LDRpin * 1000; |
khp007 | 2:104683639374 | 131 | if (pill_Detected == true && x > 200){ |
khp007 | 2:104683639374 | 132 | LedBlue = 0; |
khp007 | 2:104683639374 | 133 | wait_ms(500); |
khp007 | 2:104683639374 | 134 | stop_Servo = true; |
khp007 | 2:104683639374 | 135 | ldr_data = 111; |
khp007 | 2:104683639374 | 136 | } |
khp007 | 2:104683639374 | 137 | } |
khp007 | 2:104683639374 | 138 | ret = m2xClient.updateStreamValue(deviceId, streamLdr, ldr_data); |
khp007 | 2:104683639374 | 139 | printf("send() returned %d\r\n", ret); |
khp007 | 2:104683639374 | 140 | ldr_data = 0; |
khp007 | 2:104683639374 | 141 | ret = m2xClient.updateStreamValue(deviceId, streamLdr, ldr_data); |
khp007 | 2:104683639374 | 142 | printf("send() returned %d\r\n", ret); |
khp007 | 2:104683639374 | 143 | |
AswinSivakumar | 0:4fd1b0bd1594 | 144 | |
khp007 | 2:104683639374 | 145 | //Weight Sensor!!!!!!// |
khp007 | 2:104683639374 | 146 | //Calibrate Sensor |
khp007 | 2:104683639374 | 147 | // float sum = 0; |
khp007 | 2:104683639374 | 148 | // for (int i = 0; i< 1000; i ++){ |
khp007 | 2:104683639374 | 149 | // wait_ms(50); |
khp007 | 2:104683639374 | 150 | // sum = (gp2) *10000 + sum; |
khp007 | 2:104683639374 | 151 | // } |
khp007 | 2:104683639374 | 152 | |
khp007 | 2:104683639374 | 153 | float ave = 6877.67; |
khp007 | 2:104683639374 | 154 | // float ave = sum/1000; |
khp007 | 2:104683639374 | 155 | printf("the average is : %0.5f \r\n", ave); |
khp007 | 2:104683639374 | 156 | bool stop_load = false; |
khp007 | 2:104683639374 | 157 | while (stop_load == false) { |
khp007 | 2:104683639374 | 158 | float weightOut1 = gp1*10000; |
khp007 | 2:104683639374 | 159 | float weightOut2 = gp2*10000; |
khp007 | 2:104683639374 | 160 | float difference = weightOut2-ave; |
khp007 | 2:104683639374 | 161 | wait_ms(1000); |
khp007 | 2:104683639374 | 162 | if (difference > 15){ |
khp007 | 2:104683639374 | 163 | printf("I made it here"); |
khp007 | 2:104683639374 | 164 | bool stop_load = true; |
khp007 | 2:104683639374 | 165 | break; |
khp007 | 2:104683639374 | 166 | } |
AswinSivakumar | 0:4fd1b0bd1594 | 167 | } |
khp007 | 2:104683639374 | 168 | LedRed = 1; |
khp007 | 2:104683639374 | 169 | LedGreen = 0; |
khp007 | 2:104683639374 | 170 | LedBlue = 1; |
khp007 | 2:104683639374 | 171 | |
khp007 | 2:104683639374 | 172 | //mag.acquire_mag_data_uT(mag_data); |
khp007 | 2:104683639374 | 173 | load_data = 0; |
khp007 | 2:104683639374 | 174 | ret = m2xClient.updateStreamValue(deviceId, streamLoad, load_data); |
khp007 | 2:104683639374 | 175 | load_data = 222; |
khp007 | 2:104683639374 | 176 | ret = m2xClient.updateStreamValue(deviceId, streamLoad, load_data); |
khp007 | 2:104683639374 | 177 | load_data = 0; |
khp007 | 2:104683639374 | 178 | ret = m2xClient.updateStreamValue(deviceId, streamLoad, load_data); |
khp007 | 2:104683639374 | 179 | } |