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
Revision 1:d3628d93e4d4, committed 2018-03-21
- Comitter:
- khp007
- Date:
- Wed Mar 21 18:06:06 2018 +0000
- Parent:
- 0:8cc71e8eb869
- Commit message:
- updated with option to run either of the sensors by commenting the other 2 functions
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 8cc71e8eb869 -r d3628d93e4d4 main.cpp --- a/main.cpp Tue Mar 20 03:57:32 2018 +0000 +++ b/main.cpp Wed Mar 21 18:06:06 2018 +0000 @@ -1,130 +1,154 @@ -// Servo control class, based on a PwmOut - - - // Continuously sweep the servo through it's full range - #include "mbed.h" - #include "Servo.h" - #include "HCSR04.h" +// THis program contains 3 different parts, to control a servo motor, interface a PIR motion sensor and an Ultrasonic sensor +// with the NXP FRDM-k64f board. +// The program uses 3 libraries, one for the mbed functions, one for the Servo motor and one for the ultrasonic sensor. +// The motion sensor sends out digital signals which are used as inputs to trigger interrupts. + + +// Including libraries +#include "mbed.h" +#include "Servo.h" +#include "HCSR04.h" + - Serial pc(USBTX,USBRX); // Create a serial connection to pc through the mbed USB cable +// Create a serial connection to pc through the mbed USB cable +Serial pc(USBTX,USBRX); + - -HCSR04 sensor(D8,D9); //hcsr04 pin numbers -Servo LRmyservo(PTC5); //servo pwm pin -InterruptIn motion(D2); //pir motion sensor interrupt pin +// Initializing instances of HCSR04, servo and interrupt pin +HCSR04 sensor(D8,D9); //D8 is PTC12 and D9 is PTC4 +Servo LRmyservo(PTC5); +InterruptIn motion(D2); //D2 is PTB9 -//Input at switch SW2 and output at led2 + + +// Initializing input at switch 2 and output at led 2 DigitalIn sw2(SW2); DigitalOut Green(LED2); +// Initializing global variables int motion_detected = 0; -//interrupt and other functions - void irq_handler(void) - { - motion_detected = 1; - } - - void rotateServo(bool rotate) - { - int i = 0; - if (rotate == true){ //rotate right - for(i=10; i<111; i++){ - LRmyservo = i/100.00; - //pc.printf("read = %f\r\n", LRmyservo); - wait_ms(4); - if(motion_detected){ - break; + +// Defining IRQ routine +void irq_handler(void) +{ + motion_detected = 1; //raise flag if motion is detected +} + + +// Function to rotate the servo, swaping direction of rotation based on position +void rotateServo(bool rotate) +{ + int i = 0; + if (rotate == true) //rotate right + { + for(i=10; i<111; i++) + { + LRmyservo = i/100.00; + //pc.printf("read = %f\r\n", LRmyservo); + wait_ms(4); + if(motion_detected) + { + break; //Makes sure the servo doesnt drop another pill if one is already dropped + } + } + } else //rotate left + { + for(i=110; i>=10; i--) + { + LRmyservo = i/100.00; + //pc.printf("read = %d, signal = %d\r\n", LRmyservo.read(),something); + wait_ms(4); + if(motion_detected) + { + break; //Makes sure the servo doesnt drop another pill if one is already dropped + } + } + } +} + + +// Function to rotate the servo when switch is pressed +void servo_fnc() +{ + bool flag = false; + bool rotate = true; + + while(flag == false) + { + if (sw2 == 0) + { + rotateServo(rotate); + if (rotate == true) //adjust rotation direction for next iteration + { + rotate = false; + } else + { + rotate = true; + } } + } +} + + +// Function to detect motion on the tray, if pills are dispensed +void ultrasonic_fn() +{ + wait_ms(4000); + int echoVal = sensor.echo_duration(); + Green = 1; + + while(1) + { + int a = sensor.echo_duration(); + if (a - echoVal > 75 || echoVal - a > 75) //blinks green led as long as the hand is detected over the tray + { + Green=0; + wait(.2); + Green=1; + wait(.2); } - } else{ - for(i=110; i>=10; i--){ //rotate left - LRmyservo = i/100.00; - //pc.printf("read = %d, signal = %d\r\n", LRmyservo.read(),something); - wait_ms(4); - if(motion_detected){ - break; - } + pc.printf("%d \n\r",a); + wait_ms(50); + } +} + + +// Function to detect motion of pill passing through the channel +void motion_sensor_fn() +{ + int cnt = 0; + motion.fall(&irq_handler); + Green=1; + + while(1) + { + if(motion_detected) + { + cnt++; + motion_detected = 0; + pc.printf("Hello! I've detected %d times since reset\r\n", cnt); + Green=0; //blinks led 2 green once when pill is detected + wait(.2); + Green=1; + wait(1); + } else + { + pc.printf("did not detect\r\n"); } } - } - - void dist(int distance) -{ - //put code here to execute when the distance has changed - printf("Distance %d mm\r\n", distance); } -//main program, runs one peripheral at a time, unable to interface simutanously - int main() - { - - //Initialize variables - bool flag = false; - bool rotate = true; - - //Initilize motor positions - while(flag == false) - { - //check to make sure the motion sensor was not triggered - if (sw2 == 0) - { - rotateServo(rotate); - if (rotate == true) - { //adjust rotation direction for next iteration - rotate = false; - } else{ - rotate = true; - } - } - } - pc.printf("I am finished with the program"); - //wait_ms(5); - -///////for ditecting motion at the dispenser tray using hcsr04 - /* - wait_ms(4000); - int echoVal = sensor.echo_duration(); - Green = 1; - - while(1){ - int a = sensor.echo_duration(); - if (a - echoVal > 75 || echoVal - a > 75){ - Green=0; - wait(.2); - Green=1; - wait(.2); - } - pc.printf("%d \n\r",a); - wait_ms(50); - } - */ - -///////for ditecting motion motion of the pill passing through the channel using PIR motion sensor - /* - int cnt = 0; - motion.fall(&irq_handler); - Green=1; - - while(1) - { - if(motion_detected) - { - cnt++; - motion_detected = 0; - pc.printf("Hello! I've detected %d times since reset\r\n", cnt); - Green=0; - wait(.2); - Green=1; - wait(1); - } else{ - pc.printf("did not detect\r\n"); - } - } - */ - } \ No newline at end of file +// Main program, runs one peripheral at a time since we were unable to run them concurrently +// 3 function calls define the respective routines, 2 are commented at a time to test the one +int main() +{ + servo_fnc(); + //ultrasonic_fn() + //motion_sensor_fn() +} \ No newline at end of file