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

main.cpp

Committer:
khp007
Date:
2018-03-20
Revision:
0:8cc71e8eb869
Child:
1:d3628d93e4d4

File content as of revision 0:8cc71e8eb869:

// 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"

 Serial pc(USBTX,USBRX);     // Create a serial connection to pc through the mbed USB cable

 
HCSR04 sensor(D8,D9);        //hcsr04 pin numbers
Servo LRmyservo(PTC5);       //servo pwm pin
InterruptIn motion(D2);      //pir motion sensor interrupt pin

//Input at switch SW2 and output at led2
DigitalIn sw2(SW2);
DigitalOut Green(LED2);
 

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;
            }
        }
    } 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;
            }
        }
    }
 }
 
 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");
                }
        }
    */
    }