Lab02

Dependencies:   Servo mbed

Revision:
0:f5f8ea653b56
Child:
1:1a7f5ec34d63
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jan 17 02:22:02 2014 +0000
@@ -0,0 +1,49 @@
+//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(){
+    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
+                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("%f\n",speed);
+        
+         pc.printf("%.2f   %.2f   %d   %d \r",turn,speed,direction,sign);
+                
+             }
+}
\ No newline at end of file