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:
Tue Mar 20 03:57:32 2018 +0000
Revision:
0:8cc71e8eb869
Child:
1:d3628d93e4d4
pill dispenser prototype testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khp007 0:8cc71e8eb869 1 // Servo control class, based on a PwmOut
khp007 0:8cc71e8eb869 2
khp007 0:8cc71e8eb869 3
khp007 0:8cc71e8eb869 4 // Continuously sweep the servo through it's full range
khp007 0:8cc71e8eb869 5 #include "mbed.h"
khp007 0:8cc71e8eb869 6 #include "Servo.h"
khp007 0:8cc71e8eb869 7 #include "HCSR04.h"
khp007 0:8cc71e8eb869 8
khp007 0:8cc71e8eb869 9 Serial pc(USBTX,USBRX); // Create a serial connection to pc through the mbed USB cable
khp007 0:8cc71e8eb869 10
khp007 0:8cc71e8eb869 11
khp007 0:8cc71e8eb869 12 HCSR04 sensor(D8,D9); //hcsr04 pin numbers
khp007 0:8cc71e8eb869 13 Servo LRmyservo(PTC5); //servo pwm pin
khp007 0:8cc71e8eb869 14 InterruptIn motion(D2); //pir motion sensor interrupt pin
khp007 0:8cc71e8eb869 15
khp007 0:8cc71e8eb869 16 //Input at switch SW2 and output at led2
khp007 0:8cc71e8eb869 17 DigitalIn sw2(SW2);
khp007 0:8cc71e8eb869 18 DigitalOut Green(LED2);
khp007 0:8cc71e8eb869 19
khp007 0:8cc71e8eb869 20
khp007 0:8cc71e8eb869 21 int motion_detected = 0;
khp007 0:8cc71e8eb869 22
khp007 0:8cc71e8eb869 23
khp007 0:8cc71e8eb869 24 //interrupt and other functions
khp007 0:8cc71e8eb869 25 void irq_handler(void)
khp007 0:8cc71e8eb869 26 {
khp007 0:8cc71e8eb869 27 motion_detected = 1;
khp007 0:8cc71e8eb869 28 }
khp007 0:8cc71e8eb869 29
khp007 0:8cc71e8eb869 30 void rotateServo(bool rotate)
khp007 0:8cc71e8eb869 31 {
khp007 0:8cc71e8eb869 32 int i = 0;
khp007 0:8cc71e8eb869 33 if (rotate == true){ //rotate right
khp007 0:8cc71e8eb869 34 for(i=10; i<111; i++){
khp007 0:8cc71e8eb869 35 LRmyservo = i/100.00;
khp007 0:8cc71e8eb869 36 //pc.printf("read = %f\r\n", LRmyservo);
khp007 0:8cc71e8eb869 37 wait_ms(4);
khp007 0:8cc71e8eb869 38 if(motion_detected){
khp007 0:8cc71e8eb869 39 break;
khp007 0:8cc71e8eb869 40 }
khp007 0:8cc71e8eb869 41 }
khp007 0:8cc71e8eb869 42 } else{
khp007 0:8cc71e8eb869 43 for(i=110; i>=10; i--){ //rotate left
khp007 0:8cc71e8eb869 44 LRmyservo = i/100.00;
khp007 0:8cc71e8eb869 45 //pc.printf("read = %d, signal = %d\r\n", LRmyservo.read(),something);
khp007 0:8cc71e8eb869 46 wait_ms(4);
khp007 0:8cc71e8eb869 47 if(motion_detected){
khp007 0:8cc71e8eb869 48 break;
khp007 0:8cc71e8eb869 49 }
khp007 0:8cc71e8eb869 50 }
khp007 0:8cc71e8eb869 51 }
khp007 0:8cc71e8eb869 52 }
khp007 0:8cc71e8eb869 53
khp007 0:8cc71e8eb869 54 void dist(int distance)
khp007 0:8cc71e8eb869 55 {
khp007 0:8cc71e8eb869 56 //put code here to execute when the distance has changed
khp007 0:8cc71e8eb869 57 printf("Distance %d mm\r\n", distance);
khp007 0:8cc71e8eb869 58 }
khp007 0:8cc71e8eb869 59
khp007 0:8cc71e8eb869 60
khp007 0:8cc71e8eb869 61
khp007 0:8cc71e8eb869 62 //main program, runs one peripheral at a time, unable to interface simutanously
khp007 0:8cc71e8eb869 63 int main()
khp007 0:8cc71e8eb869 64 {
khp007 0:8cc71e8eb869 65
khp007 0:8cc71e8eb869 66 //Initialize variables
khp007 0:8cc71e8eb869 67 bool flag = false;
khp007 0:8cc71e8eb869 68 bool rotate = true;
khp007 0:8cc71e8eb869 69
khp007 0:8cc71e8eb869 70 //Initilize motor positions
khp007 0:8cc71e8eb869 71
khp007 0:8cc71e8eb869 72 while(flag == false)
khp007 0:8cc71e8eb869 73 {
khp007 0:8cc71e8eb869 74 //check to make sure the motion sensor was not triggered
khp007 0:8cc71e8eb869 75 if (sw2 == 0)
khp007 0:8cc71e8eb869 76 {
khp007 0:8cc71e8eb869 77 rotateServo(rotate);
khp007 0:8cc71e8eb869 78 if (rotate == true)
khp007 0:8cc71e8eb869 79 { //adjust rotation direction for next iteration
khp007 0:8cc71e8eb869 80 rotate = false;
khp007 0:8cc71e8eb869 81 } else{
khp007 0:8cc71e8eb869 82 rotate = true;
khp007 0:8cc71e8eb869 83 }
khp007 0:8cc71e8eb869 84 }
khp007 0:8cc71e8eb869 85 }
khp007 0:8cc71e8eb869 86 pc.printf("I am finished with the program");
khp007 0:8cc71e8eb869 87 //wait_ms(5);
khp007 0:8cc71e8eb869 88
khp007 0:8cc71e8eb869 89 ///////for ditecting motion at the dispenser tray using hcsr04
khp007 0:8cc71e8eb869 90 /*
khp007 0:8cc71e8eb869 91 wait_ms(4000);
khp007 0:8cc71e8eb869 92 int echoVal = sensor.echo_duration();
khp007 0:8cc71e8eb869 93 Green = 1;
khp007 0:8cc71e8eb869 94
khp007 0:8cc71e8eb869 95 while(1){
khp007 0:8cc71e8eb869 96 int a = sensor.echo_duration();
khp007 0:8cc71e8eb869 97 if (a - echoVal > 75 || echoVal - a > 75){
khp007 0:8cc71e8eb869 98 Green=0;
khp007 0:8cc71e8eb869 99 wait(.2);
khp007 0:8cc71e8eb869 100 Green=1;
khp007 0:8cc71e8eb869 101 wait(.2);
khp007 0:8cc71e8eb869 102 }
khp007 0:8cc71e8eb869 103 pc.printf("%d \n\r",a);
khp007 0:8cc71e8eb869 104 wait_ms(50);
khp007 0:8cc71e8eb869 105 }
khp007 0:8cc71e8eb869 106 */
khp007 0:8cc71e8eb869 107
khp007 0:8cc71e8eb869 108 ///////for ditecting motion motion of the pill passing through the channel using PIR motion sensor
khp007 0:8cc71e8eb869 109 /*
khp007 0:8cc71e8eb869 110 int cnt = 0;
khp007 0:8cc71e8eb869 111 motion.fall(&irq_handler);
khp007 0:8cc71e8eb869 112 Green=1;
khp007 0:8cc71e8eb869 113
khp007 0:8cc71e8eb869 114 while(1)
khp007 0:8cc71e8eb869 115 {
khp007 0:8cc71e8eb869 116 if(motion_detected)
khp007 0:8cc71e8eb869 117 {
khp007 0:8cc71e8eb869 118 cnt++;
khp007 0:8cc71e8eb869 119 motion_detected = 0;
khp007 0:8cc71e8eb869 120 pc.printf("Hello! I've detected %d times since reset\r\n", cnt);
khp007 0:8cc71e8eb869 121 Green=0;
khp007 0:8cc71e8eb869 122 wait(.2);
khp007 0:8cc71e8eb869 123 Green=1;
khp007 0:8cc71e8eb869 124 wait(1);
khp007 0:8cc71e8eb869 125 } else{
khp007 0:8cc71e8eb869 126 pc.printf("did not detect\r\n");
khp007 0:8cc71e8eb869 127 }
khp007 0:8cc71e8eb869 128 }
khp007 0:8cc71e8eb869 129 */
khp007 0:8cc71e8eb869 130 }