this is part of the pill dispenser prototype model. the servo motor here controls the gate of the cartridge to drop one pill at a time to the channel.

Dependencies:   mbed

main.cpp

Committer:
khp007
Date:
2018-03-23
Revision:
0:e31980f4132f

File content as of revision 0:e31980f4132f:

// Servo control class, based on a PwmOut
 
 
  // Continuously sweep the servo through it's full range
  #include "mbed.h"
  #include "Servo.h"

  
  
 Serial pc(USBTX,USBRX);     // Create a serial connection to pc through the mbed USB cable
 Servo LRmyservo(PTC5);
 InterruptIn motion(D2);
 DigitalIn sw2(SW2);

 int motion_detected = 0;

 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;
            }
        }
    }
 }

   //Servo UDmyservo(PTC7);
 
    int main() 
    {
        //float range = .0009;
       //LRmyservo.calibrate(range,45);
        //dt.start();  
        
        //Initialize variables
        bool flag = false;
        bool rotate = true;
       // motion.fall(&irq_handler);
        
        //Initilize motor positions

            while(flag == false){
                    //check to make sure the motion sensor was not triggered
               //     if(motion_detected){
                 //       motion_detected = 0;
                   //     pc.printf("I have fallen and I can't get up!\r\n");
                     //   flag = true;
                   // }
                   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);
            }