DC motor and Servo

Dependencies:   DC_Motor Servo mbed

Fork of Lab2_41 by Lab #01

Committer:
m211656
Date:
Thu Oct 11 17:44:55 2018 +0000
Revision:
2:ecf535666eb3
Parent:
1:88e499bfdcc7
Servo and DC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m211656 2:ecf535666eb3 1 //Lab2_4
m211656 2:ecf535666eb3 2 //Code demonstrates how to use outputs and inputs within a loop
m211656 2:ecf535666eb3 3 //Written by: MIDN 3/C Caicedo and MIDN 3/C Eckert
m211656 2:ecf535666eb3 4 //Modified:
m211656 2:ecf535666eb3 5 // 9/27/18 Eckert and Caicedo. Program created
m211656 2:ecf535666eb3 6 // 10/1/18 Caicedo. Modified servo portion of code
m211656 2:ecf535666eb3 7 // 10/3/18 Eckert. Added DC motor into servo motor coding
Red_Nova_Six 0:5ea7adc7e9cf 8 #include "mbed.h"
Red_Nova_Six 0:5ea7adc7e9cf 9 #include "Servo.h"
Red_Nova_Six 1:88e499bfdcc7 10 #include "Motor.h"
Red_Nova_Six 0:5ea7adc7e9cf 11
m211656 2:ecf535666eb3 12 Servo myservo(p21); //pin associated with first servo motor
m211656 2:ecf535666eb3 13 Servo myservo2(p22); // pin associated with second servo motor
m211656 2:ecf535666eb3 14 Motor m(p26,p29,p30); //pins associated with DC motor
Red_Nova_Six 0:5ea7adc7e9cf 15
m211656 2:ecf535666eb3 16 float i; //variable for servo motor control
m211656 2:ecf535666eb3 17 float f; //variable for DC motor control
Red_Nova_Six 1:88e499bfdcc7 18
Red_Nova_Six 1:88e499bfdcc7 19 int main()
Red_Nova_Six 0:5ea7adc7e9cf 20 {
m211656 2:ecf535666eb3 21 myservo.calibrate(0.0009,90); //servo calibration
Red_Nova_Six 1:88e499bfdcc7 22 myservo2.calibrate(0.0009, 90);
Red_Nova_Six 0:5ea7adc7e9cf 23
Red_Nova_Six 1:88e499bfdcc7 24 while(1) {
m211656 2:ecf535666eb3 25 for(f=.2; f<=1; f+=.1) { //for loop controlling DC motor
m211656 2:ecf535666eb3 26 m.speed(f); //speed of DC motor
m211656 2:ecf535666eb3 27 printf("speed is %f\n\r", f); //print statement to ensure speed of motor is correct
m211656 2:ecf535666eb3 28 for( i=0; i<180; i++) { //for loop controlling first part of servo motor motion
m211656 2:ecf535666eb3 29 myservo = i/180.0; //first servo moves forward
m211656 2:ecf535666eb3 30 myservo2=(180.0-i)/180.0; //second servo moves opposite
m211656 2:ecf535666eb3 31 wait(.025);
m211656 2:ecf535666eb3 32 }
m211656 2:ecf535666eb3 33 m.speed(-f); //direction of DC motor changes
m211656 2:ecf535666eb3 34 printf("speed is - %f\n\r", f);
m211656 2:ecf535666eb3 35 for( i=180; i>0; i--) {//loop controlling return of servo motors
m211656 2:ecf535666eb3 36 myservo = i/180.0;
m211656 2:ecf535666eb3 37 myservo2=(180.0-i)/180.0;
m211656 2:ecf535666eb3 38 wait(.025);
m211656 2:ecf535666eb3 39 }
Red_Nova_Six 1:88e499bfdcc7 40 }
m211656 2:ecf535666eb3 41 }
Red_Nova_Six 0:5ea7adc7e9cf 42 }