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

Committer:
khp007
Date:
Fri Mar 23 22:25:11 2018 +0000
Revision:
0:e31980f4132f
to run the servo motor to move the gate of the dispenser cartridge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khp007 0:e31980f4132f 1 // Servo control class, based on a PwmOut
khp007 0:e31980f4132f 2
khp007 0:e31980f4132f 3
khp007 0:e31980f4132f 4 // Continuously sweep the servo through it's full range
khp007 0:e31980f4132f 5 #include "mbed.h"
khp007 0:e31980f4132f 6 #include "Servo.h"
khp007 0:e31980f4132f 7
khp007 0:e31980f4132f 8
khp007 0:e31980f4132f 9
khp007 0:e31980f4132f 10 Serial pc(USBTX,USBRX); // Create a serial connection to pc through the mbed USB cable
khp007 0:e31980f4132f 11 Servo LRmyservo(PTC5);
khp007 0:e31980f4132f 12 InterruptIn motion(D2);
khp007 0:e31980f4132f 13 DigitalIn sw2(SW2);
khp007 0:e31980f4132f 14
khp007 0:e31980f4132f 15 int motion_detected = 0;
khp007 0:e31980f4132f 16
khp007 0:e31980f4132f 17 void irq_handler(void)
khp007 0:e31980f4132f 18 {
khp007 0:e31980f4132f 19 //motion_detected = 1;
khp007 0:e31980f4132f 20 }
khp007 0:e31980f4132f 21
khp007 0:e31980f4132f 22 void rotateServo(bool rotate){
khp007 0:e31980f4132f 23 int i = 0;
khp007 0:e31980f4132f 24 if (rotate == true){ //rotate right
khp007 0:e31980f4132f 25 for(i=10; i<111; i++){
khp007 0:e31980f4132f 26 LRmyservo = i/100.00;
khp007 0:e31980f4132f 27 //pc.printf("read = %f\r\n", LRmyservo);
khp007 0:e31980f4132f 28 wait_ms(4);
khp007 0:e31980f4132f 29 if(motion_detected){
khp007 0:e31980f4132f 30 break;
khp007 0:e31980f4132f 31 }
khp007 0:e31980f4132f 32 }
khp007 0:e31980f4132f 33 } else{
khp007 0:e31980f4132f 34 for(i=110; i>=10; i--){ //rotate left
khp007 0:e31980f4132f 35 LRmyservo = i/100.00;
khp007 0:e31980f4132f 36 //pc.printf("read = %d, signal = %d\r\n", LRmyservo.read(),something);
khp007 0:e31980f4132f 37 wait_ms(4);
khp007 0:e31980f4132f 38 if(motion_detected){
khp007 0:e31980f4132f 39 break;
khp007 0:e31980f4132f 40 }
khp007 0:e31980f4132f 41 }
khp007 0:e31980f4132f 42 }
khp007 0:e31980f4132f 43 }
khp007 0:e31980f4132f 44
khp007 0:e31980f4132f 45 //Servo UDmyservo(PTC7);
khp007 0:e31980f4132f 46
khp007 0:e31980f4132f 47 int main()
khp007 0:e31980f4132f 48 {
khp007 0:e31980f4132f 49 //float range = .0009;
khp007 0:e31980f4132f 50 //LRmyservo.calibrate(range,45);
khp007 0:e31980f4132f 51 //dt.start();
khp007 0:e31980f4132f 52
khp007 0:e31980f4132f 53 //Initialize variables
khp007 0:e31980f4132f 54 bool flag = false;
khp007 0:e31980f4132f 55 bool rotate = true;
khp007 0:e31980f4132f 56 // motion.fall(&irq_handler);
khp007 0:e31980f4132f 57
khp007 0:e31980f4132f 58 //Initilize motor positions
khp007 0:e31980f4132f 59
khp007 0:e31980f4132f 60 while(flag == false){
khp007 0:e31980f4132f 61 //check to make sure the motion sensor was not triggered
khp007 0:e31980f4132f 62 // if(motion_detected){
khp007 0:e31980f4132f 63 // motion_detected = 0;
khp007 0:e31980f4132f 64 // pc.printf("I have fallen and I can't get up!\r\n");
khp007 0:e31980f4132f 65 // flag = true;
khp007 0:e31980f4132f 66 // }
khp007 0:e31980f4132f 67 if (sw2 == 0){
khp007 0:e31980f4132f 68 rotateServo(rotate);
khp007 0:e31980f4132f 69 if (rotate == true){ //adjust rotation direction for next iteration
khp007 0:e31980f4132f 70 rotate = false;
khp007 0:e31980f4132f 71 } else{
khp007 0:e31980f4132f 72 rotate = true;
khp007 0:e31980f4132f 73 }
khp007 0:e31980f4132f 74 }
khp007 0:e31980f4132f 75 }
khp007 0:e31980f4132f 76 pc.printf("I am finished with the program");
khp007 0:e31980f4132f 77 //wait_ms(5);
khp007 0:e31980f4132f 78 }
khp007 0:e31980f4132f 79
khp007 0:e31980f4132f 80