Lab02

Dependencies:   Servo mbed

main.cpp

Committer:
dannellyz
Date:
2014-01-17
Revision:
1:1a7f5ec34d63
Parent:
0:f5f8ea653b56

File content as of revision 1:1a7f5ec34d63:

//SCY202
//Zachary Dannelly 
//Partner: Evan Philips
//Base code provided by CAPT Shorr
//Date Created: 16JAN14
//LAB02
/*************************Includes****************************/
#include "mbed.h"
#include "Servo.h"

/************************Pre-Processes******************************************/
Serial pc(USBTX,USBRX);
Servo myservo(p22);
PwmOut DCmotor(p21);
double turn = 0.0000001;    //Unnoticable close to zero as to not skip the later while loop while not begging the rotation 
double speed = 0.0;
int direction = 0;
double sign = -1.0;
/*******************************************************************************/
int main(){
    pc.printf("%s   %s     \n\r", "Servo","DC Motor");
    while(1){
       myservo.calibrate(.0009,90);      //Code provideed by CAPT Shorr to calibrate Servo
        sign = sign * -1.0;              //Sign tells servo whether to increment or decrement based on which phase of the cycle it is in
        while(turn > 0 && turn < 1){     //While the servo is completing one full rotation continue to turn...
                myservo = turn;
                turn += .005 * sign;     //...based upon wether the servo is in the forward or backwrd phase of its ocilation
                pc.printf("%.2f     %.2f\r", turn, speed);
                wait(.0125);             
                
                }
        if(sign > 0){turn = .9999999;}   //Same concept as on the comment when variable was initialized
        else{turn = .000001;}               
        
        if (speed == 1.0){direction = 1;}   //Once the speed of the DC Motor has reached 100% the direction is set to 1
        if (speed < 0.1){direction = 0;}    //Once the spped of the DC Motor has returned to 0 the direction is reset to 0**
                                            //**Code had to be modifed to <0.1 as when at == 0.0 it would not reset
        
        if(speed < 1 && sign < 0 && direction == 0){            // While the speed of the motor is less than 1 and the servo is back at its starting spot
                speed += .2;                                    // (as indicated by a -1 on the sign) and the motor is accelerating, increse the speed by 20%
                DCmotor = speed;
                }
        else if(speed > 0 && sign < 0 && direction == 1){       // While the speed of the motor is greater than 0 and the servo is back at its starting spot
                speed -= .2;                                    // (as indicated by a -1 on the sign) and the motor is decellerating, decrease the speed by 20%
                DCmotor = speed;
                }
        
        
         //pc.printf("        %.2f      %d \r",speed);
                
             }
}