Test code
Dependencies: mbed Motor Servo
main.cpp@3:638dced18d87, 2018-10-16 (annotated)
- Committer:
- m215910
- Date:
- Tue Oct 16 15:43:29 2018 +0000
- Revision:
- 3:638dced18d87
- Parent:
- 2:17956bff3ff2
- Child:
- 4:b4e19be17235
buttons and motors should work
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:17956bff3ff2 | 12 | Servo servo1(p21); |
m215910 | 2:17956bff3ff2 | 13 | Servo servo2(p22); |
m215910 | 2:17956bff3ff2 | 14 | DigitalIn sw1(p16); //switches or buttons |
m215910 | 3:638dced18d87 | 15 | DigitalIn bu1(p15); //Army man |
m215910 | 3:638dced18d87 | 16 | DigitalIn bu2(p14); //Army man |
m215910 | 2:17956bff3ff2 | 17 | //DigitalIn bu3(p); //Army man |
m215910 | 2:17956bff3ff2 | 18 | //DigitalIn bu4(p); //Motor clockwise |
m215910 | 2:17956bff3ff2 | 19 | //DigitalIn bu5(p); //Motor CC |
m215910 | 2:17956bff3ff2 | 20 | DigitalOut leds[3]={p25, p26, p27}; |
m215910 | 1:bd813dd636a9 | 21 | float pos = rand()/(float)RAND_MAX; //allows the servo to start at a random position |
m215910 | 1:bd813dd636a9 | 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 | 1:bd813dd636a9 | 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 | 2:17956bff3ff2 | 43 | if (sw1state == 1) { //if bu4 is on rotate motor fwd |
m215910 | 2:17956bff3ff2 | 44 | m.speed(s); |
m215910 | 3:638dced18d87 | 45 | s=1; |
m215910 | 3:638dced18d87 | 46 | printf("hi");} |
m215910 | 3:638dced18d87 | 47 | else { //if bu5 is on rotate motor rev |
m215910 | 3:638dced18d87 | 48 | m.speed(s); |
m215910 | 2:17956bff3ff2 | 49 | s=0;} |
m215910 | 1:bd813dd636a9 | 50 | |
m215910 | 1:bd813dd636a9 | 51 | |
m215910 | 3:638dced18d87 | 52 | //if (bu1state == 0) { //if ARMY button is off light up LEDS |
m215910 | 3:638dced18d87 | 53 | // leds[0]=1;} |
m215910 | 3:638dced18d87 | 54 | // else { |
m215910 | 3:638dced18d87 | 55 | // leds[0]=0;} |
m215910 | 3:638dced18d87 | 56 | //if (bu2state == 0) { |
m215910 | 3:638dced18d87 | 57 | // leds[1]=1;} |
m215910 | 3:638dced18d87 | 58 | // else { |
m215910 | 3:638dced18d87 | 59 | // leds[1]=0;} |
m215910 | 3:638dced18d87 | 60 | //if (bu3state == 0) { |
m215910 | 3:638dced18d87 | 61 | // leds[2]=1;} |
m215910 | 3:638dced18d87 | 62 | // else { |
m215910 | 3:638dced18d87 | 63 | // leds[2]=0;} |
m215910 | 1:bd813dd636a9 | 64 | |
m215910 | 1:bd813dd636a9 | 65 | |
m215910 | 2:17956bff3ff2 | 66 | //if((sw1state) == 1) { //if switch is on move servos quickly to other angle |
m215910 | 2:17956bff3ff2 | 67 | // (pos && pos2) = 1;} |
m215910 | 2:17956bff3ff2 | 68 | //if((sw1state) == 1) { //if switch is off move back |
m215910 | 2:17956bff3ff2 | 69 | // (pos && pos2) = 0;} |
m215910 | 1:bd813dd636a9 | 70 | |
m215910 | 1:bd813dd636a9 | 71 | } //end while |
m215910 | 1:bd813dd636a9 | 72 | } //end main |