Test code

Dependencies:   mbed Motor Servo

Committer:
m215910
Date:
Fri Oct 19 14:44:13 2018 +0000
Revision:
4:b4e19be17235
Parent:
3:638dced18d87
both motors and button

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m215910 1:bd813dd636a9 1 //Maria Satre, Riki Gagnon, Theo Boskov, Janelle Stewart
m215910 1:bd813dd636a9 2 //Project 2
m215910 1:bd813dd636a9 3 //Goat Launcher
m215910 1:bd813dd636a9 4
m215910 1:bd813dd636a9 5 //Libraries and mechanical directives
m215910 3:638dced18d87 6
m215910 3:638dced18d87 7 #include "mbed.h"
m215910 1:bd813dd636a9 8 #include "Motor.h" //must change line 31 in Motor.cpp according to Lab
m215910 1:bd813dd636a9 9 #include "Servo.h" //need Servo library
m215910 0:755c12c1a9ad 10
m215910 3:638dced18d87 11 Motor m(p26, p30, p29); //designates the pins used by the motor
m215910 4:b4e19be17235 12 //Servo servo1(p21);
m215910 4:b4e19be17235 13 //Servo servo2(p22);
m215910 2:17956bff3ff2 14 DigitalIn sw1(p16); //switches or buttons
m215910 4:b4e19be17235 15 DigitalIn bu1(p21); //Army man
m215910 4:b4e19be17235 16 DigitalIn bu2(p22); //Army man
m215910 4:b4e19be17235 17 DigitalIn bu3(p23); //Army man
m215910 2:17956bff3ff2 18 //DigitalIn bu4(p); //Motor clockwise
m215910 2:17956bff3ff2 19 //DigitalIn bu5(p); //Motor CC
m215910 4:b4e19be17235 20 DigitalOut leds[3]={p10, p11, p12};
m215910 4:b4e19be17235 21 //float pos = rand()/(float)RAND_MAX; //allows the servo to start at a random position
m215910 4:b4e19be17235 22 //float pos2 = rand()/(float)RAND_MAX;
m215910 1:bd813dd636a9 23 int sw1state, bu1state, bu2state, bu3state, bu4state, bu5state; //the state of the switch
m215910 3:638dced18d87 24 int s=0.6; //speed of motor rotation - fairly slow so as to be exact
m215910 1:bd813dd636a9 25
m215910 0:755c12c1a9ad 26
m215910 0:755c12c1a9ad 27 int main() {
m215910 3:638dced18d87 28 //servo1.calibrate(0.0009, 90.0); //need to calibrate the servos so they don't freak out
m215910 3:638dced18d87 29 //servo2.calibrate(0.0009, 90.0);
m215910 3:638dced18d87 30 //servo1=pos; //used to help calibrate
m215910 3:638dced18d87 31 //servo2=pos2;
m215910 1:bd813dd636a9 32
m215910 1:bd813dd636a9 33 while(1) { //do we need while?
m215910 4:b4e19be17235 34 //sw1state=sw1.read(); //read in switch state
m215910 3:638dced18d87 35 //bu1state=bu1.read();
m215910 2:17956bff3ff2 36 //bu2state=bu2.read();
m215910 2:17956bff3ff2 37 //bu3state=bu3.read();
m215910 2:17956bff3ff2 38 //bu4state=bu4.read();
m215910 2:17956bff3ff2 39 //bu5state=bu5.read();
m215910 3:638dced18d87 40 //pos=0;
m215910 3:638dced18d87 41 //pos2=0;
m215910 2:17956bff3ff2 42
m215910 4:b4e19be17235 43 sw1state=sw1.read(); //read in switch state
m215910 4:b4e19be17235 44
m215910 2:17956bff3ff2 45 if (sw1state == 1) { //if bu4 is on rotate motor fwd
m215910 4:b4e19be17235 46 m.speed(-s);
m215910 3:638dced18d87 47 s=1;
m215910 3:638dced18d87 48 printf("hi");}
m215910 3:638dced18d87 49 else { //if bu5 is on rotate motor rev
m215910 3:638dced18d87 50 m.speed(s);
m215910 2:17956bff3ff2 51 s=0;}
m215910 1:bd813dd636a9 52
m215910 1:bd813dd636a9 53
m215910 4:b4e19be17235 54 bu1state=bu1.read();
m215910 4:b4e19be17235 55 bu2state=bu2.read();
m215910 4:b4e19be17235 56 bu3state=bu3.read();
m215910 4:b4e19be17235 57
m215910 4:b4e19be17235 58
m215910 4:b4e19be17235 59 if (bu1state == 0) { //if ARMY button is off light up LEDS
m215910 4:b4e19be17235 60 leds[0]=1;}
m215910 4:b4e19be17235 61 if (bu1state == 1) {
m215910 4:b4e19be17235 62 leds[0]=0;}
m215910 4:b4e19be17235 63 if (bu2state == 0) { //if ARMY button is off light up LEDS
m215910 4:b4e19be17235 64 leds[1]=1;}
m215910 4:b4e19be17235 65 if (bu2state == 1) {
m215910 4:b4e19be17235 66 leds[1]=0;}
m215910 4:b4e19be17235 67 if (bu3state == 0) { //if ARMY button is off light up LEDS
m215910 4:b4e19be17235 68 leds[2]=1;}
m215910 4:b4e19be17235 69 if (bu3state == 1) {
m215910 4:b4e19be17235 70 leds[2]=0;}
m215910 1:bd813dd636a9 71
m215910 1:bd813dd636a9 72
m215910 2:17956bff3ff2 73 //if((sw1state) == 1) { //if switch is on move servos quickly to other angle
m215910 2:17956bff3ff2 74 // (pos && pos2) = 1;}
m215910 2:17956bff3ff2 75 //if((sw1state) == 1) { //if switch is off move back
m215910 2:17956bff3ff2 76 // (pos && pos2) = 0;}
m215910 1:bd813dd636a9 77
m215910 1:bd813dd636a9 78 } //end while
m215910 1:bd813dd636a9 79 } //end main