DC motor and Servo
Dependencies: DC_Motor Servo mbed
Fork of Lab2_41 by
Diff: main.cpp
- Revision:
- 2:ecf535666eb3
- Parent:
- 1:88e499bfdcc7
--- a/main.cpp Wed Oct 03 18:31:18 2018 +0000 +++ b/main.cpp Thu Oct 11 17:44:55 2018 +0000 @@ -1,29 +1,42 @@ - +//Lab2_4 +//Code demonstrates how to use outputs and inputs within a loop +//Written by: MIDN 3/C Caicedo and MIDN 3/C Eckert +//Modified: +// 9/27/18 Eckert and Caicedo. Program created +// 10/1/18 Caicedo. Modified servo portion of code +// 10/3/18 Eckert. Added DC motor into servo motor coding #include "mbed.h" #include "Servo.h" #include "Motor.h" -Servo myservo(p21); -Servo myservo2(p22); -Motor(PinName pwm, PinName fwd, PinName rev): +Servo myservo(p21); //pin associated with first servo motor +Servo myservo2(p22); // pin associated with second servo motor +Motor m(p26,p29,p30); //pins associated with DC motor - int i; +float i; //variable for servo motor control +float f; //variable for DC motor control int main() { - myservo.calibrate(0.0009,90); + myservo.calibrate(0.0009,90); //servo calibration myservo2.calibrate(0.0009, 90); while(1) { - for( i=0; i<180; i++) { - myservo = i/180.0; - myservo2=(180.0-i)/180.0; - wait(0.025); + for(f=.2; f<=1; f+=.1) { //for loop controlling DC motor + m.speed(f); //speed of DC motor + printf("speed is %f\n\r", f); //print statement to ensure speed of motor is correct + for( i=0; i<180; i++) { //for loop controlling first part of servo motor motion + myservo = i/180.0; //first servo moves forward + myservo2=(180.0-i)/180.0; //second servo moves opposite + wait(.025); + } + m.speed(-f); //direction of DC motor changes + printf("speed is - %f\n\r", f); + for( i=180; i>0; i--) {//loop controlling return of servo motors + myservo = i/180.0; + myservo2=(180.0-i)/180.0; + wait(.025); + } } - for( i=180; i>0; i--) { - myservo = i/180.0; - myservo2=(180.0-i)/180.0; - wait(0.025); + } } -} -}