Design and testing of prototype for pill dispenser made using frdm-k64f, and interfaced with servo as gate for pill cartilage, PIR motion sensor to detect pill dropping into dispenser and HCSR04 ultrasonic sensor to detect pills being taken off the dispenser.

Dependencies:   HCSR04 Servo mbed

Committer:
khp007
Date:
Wed Mar 21 18:06:06 2018 +0000
Revision:
1:d3628d93e4d4
Parent:
0:8cc71e8eb869
updated with option to run either of the sensors by commenting the other 2 functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khp007 1:d3628d93e4d4 1 // THis program contains 3 different parts, to control a servo motor, interface a PIR motion sensor and an Ultrasonic sensor
khp007 1:d3628d93e4d4 2 // with the NXP FRDM-k64f board.
khp007 1:d3628d93e4d4 3 // The program uses 3 libraries, one for the mbed functions, one for the Servo motor and one for the ultrasonic sensor.
khp007 1:d3628d93e4d4 4 // The motion sensor sends out digital signals which are used as inputs to trigger interrupts.
khp007 1:d3628d93e4d4 5
khp007 1:d3628d93e4d4 6
khp007 1:d3628d93e4d4 7 // Including libraries
khp007 1:d3628d93e4d4 8 #include "mbed.h"
khp007 1:d3628d93e4d4 9 #include "Servo.h"
khp007 1:d3628d93e4d4 10 #include "HCSR04.h"
khp007 1:d3628d93e4d4 11
khp007 0:8cc71e8eb869 12
khp007 1:d3628d93e4d4 13 // Create a serial connection to pc through the mbed USB cable
khp007 1:d3628d93e4d4 14 Serial pc(USBTX,USBRX);
khp007 1:d3628d93e4d4 15
khp007 0:8cc71e8eb869 16
khp007 1:d3628d93e4d4 17 // Initializing instances of HCSR04, servo and interrupt pin
khp007 1:d3628d93e4d4 18 HCSR04 sensor(D8,D9); //D8 is PTC12 and D9 is PTC4
khp007 1:d3628d93e4d4 19 Servo LRmyservo(PTC5);
khp007 1:d3628d93e4d4 20 InterruptIn motion(D2); //D2 is PTB9
khp007 0:8cc71e8eb869 21
khp007 1:d3628d93e4d4 22
khp007 1:d3628d93e4d4 23
khp007 1:d3628d93e4d4 24 // Initializing input at switch 2 and output at led 2
khp007 0:8cc71e8eb869 25 DigitalIn sw2(SW2);
khp007 0:8cc71e8eb869 26 DigitalOut Green(LED2);
khp007 0:8cc71e8eb869 27
khp007 0:8cc71e8eb869 28
khp007 1:d3628d93e4d4 29 // Initializing global variables
khp007 0:8cc71e8eb869 30 int motion_detected = 0;
khp007 0:8cc71e8eb869 31
khp007 0:8cc71e8eb869 32
khp007 1:d3628d93e4d4 33
khp007 1:d3628d93e4d4 34 // Defining IRQ routine
khp007 1:d3628d93e4d4 35 void irq_handler(void)
khp007 1:d3628d93e4d4 36 {
khp007 1:d3628d93e4d4 37 motion_detected = 1; //raise flag if motion is detected
khp007 1:d3628d93e4d4 38 }
khp007 1:d3628d93e4d4 39
khp007 1:d3628d93e4d4 40
khp007 1:d3628d93e4d4 41 // Function to rotate the servo, swaping direction of rotation based on position
khp007 1:d3628d93e4d4 42 void rotateServo(bool rotate)
khp007 1:d3628d93e4d4 43 {
khp007 1:d3628d93e4d4 44 int i = 0;
khp007 1:d3628d93e4d4 45 if (rotate == true) //rotate right
khp007 1:d3628d93e4d4 46 {
khp007 1:d3628d93e4d4 47 for(i=10; i<111; i++)
khp007 1:d3628d93e4d4 48 {
khp007 1:d3628d93e4d4 49 LRmyservo = i/100.00;
khp007 1:d3628d93e4d4 50 //pc.printf("read = %f\r\n", LRmyservo);
khp007 1:d3628d93e4d4 51 wait_ms(4);
khp007 1:d3628d93e4d4 52 if(motion_detected)
khp007 1:d3628d93e4d4 53 {
khp007 1:d3628d93e4d4 54 break; //Makes sure the servo doesnt drop another pill if one is already dropped
khp007 1:d3628d93e4d4 55 }
khp007 1:d3628d93e4d4 56 }
khp007 1:d3628d93e4d4 57 } else //rotate left
khp007 1:d3628d93e4d4 58 {
khp007 1:d3628d93e4d4 59 for(i=110; i>=10; i--)
khp007 1:d3628d93e4d4 60 {
khp007 1:d3628d93e4d4 61 LRmyservo = i/100.00;
khp007 1:d3628d93e4d4 62 //pc.printf("read = %d, signal = %d\r\n", LRmyservo.read(),something);
khp007 1:d3628d93e4d4 63 wait_ms(4);
khp007 1:d3628d93e4d4 64 if(motion_detected)
khp007 1:d3628d93e4d4 65 {
khp007 1:d3628d93e4d4 66 break; //Makes sure the servo doesnt drop another pill if one is already dropped
khp007 1:d3628d93e4d4 67 }
khp007 1:d3628d93e4d4 68 }
khp007 1:d3628d93e4d4 69 }
khp007 1:d3628d93e4d4 70 }
khp007 1:d3628d93e4d4 71
khp007 1:d3628d93e4d4 72
khp007 1:d3628d93e4d4 73 // Function to rotate the servo when switch is pressed
khp007 1:d3628d93e4d4 74 void servo_fnc()
khp007 1:d3628d93e4d4 75 {
khp007 1:d3628d93e4d4 76 bool flag = false;
khp007 1:d3628d93e4d4 77 bool rotate = true;
khp007 1:d3628d93e4d4 78
khp007 1:d3628d93e4d4 79 while(flag == false)
khp007 1:d3628d93e4d4 80 {
khp007 1:d3628d93e4d4 81 if (sw2 == 0)
khp007 1:d3628d93e4d4 82 {
khp007 1:d3628d93e4d4 83 rotateServo(rotate);
khp007 1:d3628d93e4d4 84 if (rotate == true) //adjust rotation direction for next iteration
khp007 1:d3628d93e4d4 85 {
khp007 1:d3628d93e4d4 86 rotate = false;
khp007 1:d3628d93e4d4 87 } else
khp007 1:d3628d93e4d4 88 {
khp007 1:d3628d93e4d4 89 rotate = true;
khp007 1:d3628d93e4d4 90 }
khp007 0:8cc71e8eb869 91 }
khp007 1:d3628d93e4d4 92 }
khp007 1:d3628d93e4d4 93 }
khp007 1:d3628d93e4d4 94
khp007 1:d3628d93e4d4 95
khp007 1:d3628d93e4d4 96 // Function to detect motion on the tray, if pills are dispensed
khp007 1:d3628d93e4d4 97 void ultrasonic_fn()
khp007 1:d3628d93e4d4 98 {
khp007 1:d3628d93e4d4 99 wait_ms(4000);
khp007 1:d3628d93e4d4 100 int echoVal = sensor.echo_duration();
khp007 1:d3628d93e4d4 101 Green = 1;
khp007 1:d3628d93e4d4 102
khp007 1:d3628d93e4d4 103 while(1)
khp007 1:d3628d93e4d4 104 {
khp007 1:d3628d93e4d4 105 int a = sensor.echo_duration();
khp007 1:d3628d93e4d4 106 if (a - echoVal > 75 || echoVal - a > 75) //blinks green led as long as the hand is detected over the tray
khp007 1:d3628d93e4d4 107 {
khp007 1:d3628d93e4d4 108 Green=0;
khp007 1:d3628d93e4d4 109 wait(.2);
khp007 1:d3628d93e4d4 110 Green=1;
khp007 1:d3628d93e4d4 111 wait(.2);
khp007 0:8cc71e8eb869 112 }
khp007 1:d3628d93e4d4 113 pc.printf("%d \n\r",a);
khp007 1:d3628d93e4d4 114 wait_ms(50);
khp007 1:d3628d93e4d4 115 }
khp007 1:d3628d93e4d4 116 }
khp007 1:d3628d93e4d4 117
khp007 1:d3628d93e4d4 118
khp007 1:d3628d93e4d4 119 // Function to detect motion of pill passing through the channel
khp007 1:d3628d93e4d4 120 void motion_sensor_fn()
khp007 1:d3628d93e4d4 121 {
khp007 1:d3628d93e4d4 122 int cnt = 0;
khp007 1:d3628d93e4d4 123 motion.fall(&irq_handler);
khp007 1:d3628d93e4d4 124 Green=1;
khp007 1:d3628d93e4d4 125
khp007 1:d3628d93e4d4 126 while(1)
khp007 1:d3628d93e4d4 127 {
khp007 1:d3628d93e4d4 128 if(motion_detected)
khp007 1:d3628d93e4d4 129 {
khp007 1:d3628d93e4d4 130 cnt++;
khp007 1:d3628d93e4d4 131 motion_detected = 0;
khp007 1:d3628d93e4d4 132 pc.printf("Hello! I've detected %d times since reset\r\n", cnt);
khp007 1:d3628d93e4d4 133 Green=0; //blinks led 2 green once when pill is detected
khp007 1:d3628d93e4d4 134 wait(.2);
khp007 1:d3628d93e4d4 135 Green=1;
khp007 1:d3628d93e4d4 136 wait(1);
khp007 1:d3628d93e4d4 137 } else
khp007 1:d3628d93e4d4 138 {
khp007 1:d3628d93e4d4 139 pc.printf("did not detect\r\n");
khp007 0:8cc71e8eb869 140 }
khp007 0:8cc71e8eb869 141 }
khp007 0:8cc71e8eb869 142 }
khp007 0:8cc71e8eb869 143
khp007 0:8cc71e8eb869 144
khp007 0:8cc71e8eb869 145
khp007 0:8cc71e8eb869 146
khp007 1:d3628d93e4d4 147 // Main program, runs one peripheral at a time since we were unable to run them concurrently
khp007 1:d3628d93e4d4 148 // 3 function calls define the respective routines, 2 are commented at a time to test the one
khp007 1:d3628d93e4d4 149 int main()
khp007 1:d3628d93e4d4 150 {
khp007 1:d3628d93e4d4 151 servo_fnc();
khp007 1:d3628d93e4d4 152 //ultrasonic_fn()
khp007 1:d3628d93e4d4 153 //motion_sensor_fn()
khp007 1:d3628d93e4d4 154 }