Lab 2 DC

Dependencies:   Motor mbed

Committer:
mattsims12
Date:
Tue Oct 13 02:55:17 2015 +0000
Revision:
0:ed96989f8478
Lab2 DC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mattsims12 0:ed96989f8478 1 /*Matthew Sims
mattsims12 0:ed96989f8478 2 DC Control
mattsims12 0:ed96989f8478 3 Moves Speeds a dc motor and changes its direction every time it speeds up. At full speed, it slows down.
mattsims12 0:ed96989f8478 4 10/13/15
mattsims12 0:ed96989f8478 5 */
mattsims12 0:ed96989f8478 6
mattsims12 0:ed96989f8478 7 #include "mbed.h" //include libraries
mattsims12 0:ed96989f8478 8 #include "Motor.h"
mattsims12 0:ed96989f8478 9
mattsims12 0:ed96989f8478 10 Motor m(p25,p27,p28); //Set up motor
mattsims12 0:ed96989f8478 11
mattsims12 0:ed96989f8478 12 int main()
mattsims12 0:ed96989f8478 13 {
mattsims12 0:ed96989f8478 14 int direction=1; //tracks which way motor is turning
mattsims12 0:ed96989f8478 15 int tracker=1; //Tracks if speed is increasing or decreasing
mattsims12 0:ed96989f8478 16 float speed=.2; //speed of motor
mattsims12 0:ed96989f8478 17
mattsims12 0:ed96989f8478 18 while(1) {
mattsims12 0:ed96989f8478 19 getchar(); //Wait for button press
mattsims12 0:ed96989f8478 20
mattsims12 0:ed96989f8478 21 if(tracker==1) //Add .2 to absolute value of speeed
mattsims12 0:ed96989f8478 22 speed+=.2;
mattsims12 0:ed96989f8478 23 else
mattsims12 0:ed96989f8478 24 speed-=.2;
mattsims12 0:ed96989f8478 25 direction*=-1; //Direction changes after every speed change
mattsims12 0:ed96989f8478 26 if (direction==1)
mattsims12 0:ed96989f8478 27 m.speed(speed);
mattsims12 0:ed96989f8478 28 else
mattsims12 0:ed96989f8478 29 m.speed(-1*speed);
mattsims12 0:ed96989f8478 30 if(speed==1.0 || speed<0.1) //If it reaches maximum speed, It will start decresing, if minimum, it will incrcease.
mattsims12 0:ed96989f8478 31 tracker*=-1;
mattsims12 0:ed96989f8478 32 }
mattsims12 0:ed96989f8478 33 }