Test code
Dependencies: mbed Motor Servo
main.cpp@1:bd813dd636a9, 2018-10-09 (annotated)
- Committer:
- m215910
- Date:
- Tue Oct 09 15:05:03 2018 +0000
- Revision:
- 1:bd813dd636a9
- Parent:
- 0:755c12c1a9ad
- Child:
- 2:17956bff3ff2
First version of pseudo code. The pins are not entered. All if statements
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 | 1:bd813dd636a9 | 6 | #include "Motor.h" //must change line 31 in Motor.cpp according to Lab |
m215910 | 0:755c12c1a9ad | 7 | #include "mbed.h" |
m215910 | 1:bd813dd636a9 | 8 | #include "Servo.h" //need Servo library |
m215910 | 0:755c12c1a9ad | 9 | |
m215910 | 1:bd813dd636a9 | 10 | Motor m(p, p, p); //designates the pins used by the motor |
m215910 | 1:bd813dd636a9 | 11 | Servo servo1(p); |
m215910 | 1:bd813dd636a9 | 12 | Servo servo2(p); |
m215910 | 1:bd813dd636a9 | 13 | DigitalIn sw1(p); //switches or buttons |
m215910 | 1:bd813dd636a9 | 14 | DigitalIn bu1(p); //Army man |
m215910 | 1:bd813dd636a9 | 15 | DigitalIn bu2(p); //Army man |
m215910 | 1:bd813dd636a9 | 16 | DigitalIn bu3(p); //Army man |
m215910 | 1:bd813dd636a9 | 17 | DigitalIn bu4(p); //Motor clockwise |
m215910 | 1:bd813dd636a9 | 18 | DigitalIn bu5(p); //Motor CC |
m215910 | 1:bd813dd636a9 | 19 | DigitalOut leds[5]={p,p,p,p,p}; |
m215910 | 1:bd813dd636a9 | 20 | float pos = rand()/(float)RAND_MAX; //allows the servo to start at a random position |
m215910 | 1:bd813dd636a9 | 21 | float pos2 = rand()/(float)RAND_MAX; |
m215910 | 1:bd813dd636a9 | 22 | int sw1state, bu1state, bu2state, bu3state, bu4state, bu5state; //the state of the switch |
m215910 | 1:bd813dd636a9 | 23 | int s=0.2; //speed of motor rotation - fairly slow so as to be exact |
m215910 | 1:bd813dd636a9 | 24 | |
m215910 | 0:755c12c1a9ad | 25 | |
m215910 | 0:755c12c1a9ad | 26 | int main() { |
m215910 | 1:bd813dd636a9 | 27 | servo1.calibrate(0.0009, 90.0); //need to calibrate the servos so they don't freak out |
m215910 | 1:bd813dd636a9 | 28 | servo2.calibrate(0.0009, 90.0); |
m215910 | 1:bd813dd636a9 | 29 | servo1=pos; //used to help calibrate |
m215910 | 1:bd813dd636a9 | 30 | servo2=pos2; |
m215910 | 1:bd813dd636a9 | 31 | |
m215910 | 1:bd813dd636a9 | 32 | while(1) { //do we need while? |
m215910 | 1:bd813dd636a9 | 33 | sw1state=sw1.read(); //read in switch state |
m215910 | 1:bd813dd636a9 | 34 | bu1state=bu1.read(); |
m215910 | 1:bd813dd636a9 | 35 | bu2state=bu2.read(); |
m215910 | 1:bd813dd636a9 | 36 | bu3state=bu3.read(); |
m215910 | 1:bd813dd636a9 | 37 | bu4state=bu4.read(); |
m215910 | 1:bd813dd636a9 | 38 | bu5state=bu5.read(); |
m215910 | 1:bd813dd636a9 | 39 | pos=0 |
m215910 | 1:bd813dd636a9 | 40 | pos2=0 |
m215910 | 1:bd813dd636a9 | 41 | |
m215910 | 1:bd813dd636a9 | 42 | if((sw1state) == 1) { //if switch is on move servos quickly to other angle |
m215910 | 1:bd813dd636a9 | 43 | (pos && pos2) = 1;} |
m215910 | 1:bd813dd636a9 | 44 | if((sw1state) == 1) { //if switch is off move back |
m215910 | 1:bd813dd636a9 | 45 | (pos && pos2) = 0;} |
m215910 | 1:bd813dd636a9 | 46 | |
m215910 | 1:bd813dd636a9 | 47 | |
m215910 | 1:bd813dd636a9 | 48 | if (bu1state == 0) { //if ARMY button is off light up LEDS |
m215910 | 1:bd813dd636a9 | 49 | leds[0]=1;} |
m215910 | 1:bd813dd636a9 | 50 | if (bu2state == 0) { |
m215910 | 1:bd813dd636a9 | 51 | leds[1]=1;} |
m215910 | 1:bd813dd636a9 | 52 | if (bu3state == 0) { |
m215910 | 1:bd813dd636a9 | 53 | leds[2]=1;} |
m215910 | 1:bd813dd636a9 | 54 | if (bu1state == 1) { //if ARMY button is on nothing happens - ARMY guys with weight on LEDS |
m215910 | 1:bd813dd636a9 | 55 | leds[0]=0;} |
m215910 | 1:bd813dd636a9 | 56 | if (bu2state == 1) { |
m215910 | 1:bd813dd636a9 | 57 | leds[1]=0;} |
m215910 | 1:bd813dd636a9 | 58 | if (bu3state == 1) { |
m215910 | 1:bd813dd636a9 | 59 | leds[2]=0;} |
m215910 | 1:bd813dd636a9 | 60 | |
m215910 | 1:bd813dd636a9 | 61 | |
m215910 | 1:bd813dd636a9 | 62 | if (bu4state == 1) { //if bu4 is on rotate motor fwd |
m215910 | 1:bd813dd636a9 | 63 | m.speed(s);} |
m215910 | 1:bd813dd636a9 | 64 | if (bu5state == 1) { //if bu5 is on rotate motor rev |
m215910 | 1:bd813dd636a9 | 65 | m.speed(-s);} |
m215910 | 1:bd813dd636a9 | 66 | |
m215910 | 1:bd813dd636a9 | 67 | } //end while |
m215910 | 1:bd813dd636a9 | 68 | } //end main |