karan patel / Mbed 2 deprecated Motor

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Servo control class, based on a PwmOut
00002  
00003  
00004   // Continuously sweep the servo through it's full range
00005   #include "mbed.h"
00006   #include "Servo.h"
00007 
00008   
00009   
00010  Serial pc(USBTX,USBRX);     // Create a serial connection to pc through the mbed USB cable
00011  Servo LRmyservo(PTC5);
00012  InterruptIn motion(D2);
00013  DigitalIn sw2(SW2);
00014 
00015  int motion_detected = 0;
00016 
00017  void irq_handler(void)
00018  {
00019      //motion_detected = 1;
00020  }
00021  
00022  void rotateServo(bool rotate){
00023     int i = 0;
00024     if (rotate == true){ //rotate right
00025         for(i=10; i<111; i++){
00026             LRmyservo = i/100.00;
00027             //pc.printf("read = %f\r\n", LRmyservo);
00028             wait_ms(4);
00029             if(motion_detected){
00030                 break;
00031             }
00032         }
00033     } else{ 
00034         for(i=110; i>=10; i--){ //rotate left
00035             LRmyservo = i/100.00;
00036             //pc.printf("read = %d, signal = %d\r\n", LRmyservo.read(),something);
00037             wait_ms(4);
00038             if(motion_detected){
00039                 break;
00040             }
00041         }
00042     }
00043  }
00044 
00045    //Servo UDmyservo(PTC7);
00046  
00047     int main() 
00048     {
00049         //float range = .0009;
00050        //LRmyservo.calibrate(range,45);
00051         //dt.start();  
00052         
00053         //Initialize variables
00054         bool flag = false;
00055         bool rotate = true;
00056        // motion.fall(&irq_handler);
00057         
00058         //Initilize motor positions
00059 
00060             while(flag == false){
00061                     //check to make sure the motion sensor was not triggered
00062                //     if(motion_detected){
00063                  //       motion_detected = 0;
00064                    //     pc.printf("I have fallen and I can't get up!\r\n");
00065                      //   flag = true;
00066                    // }
00067                    if (sw2 == 0){
00068                        rotateServo(rotate);
00069                     if (rotate == true){ //adjust rotation direction for next iteration
00070                         rotate = false;
00071                     } else{
00072                         rotate = true;
00073                     }
00074                    }
00075                 }
00076                 pc.printf("I am finished with the program");
00077              //wait_ms(5);
00078             }
00079      
00080