![](/media/cache/group/220px-Herndon_Monument_Climb.jpg.50x50_q85.jpg)
Start of project 2
Dependencies: Motor Servo mbed mbed-rtos
main.cpp@7:adcdd7556997, 2018-10-22 (annotated)
- Committer:
- m215676
- Date:
- Mon Oct 22 00:18:49 2018 +0000
- Revision:
- 7:adcdd7556997
- Parent:
- 6:3dc339508843
10/21
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
m215676 | 0:9347308e8e1a | 1 | #include "mbed.h" |
m215676 | 0:9347308e8e1a | 2 | #include "stdio.h" |
m215676 | 0:9347308e8e1a | 3 | #include "Motor.h" |
m215676 | 0:9347308e8e1a | 4 | #include "math.h" |
m215676 | 2:60a8105d0d20 | 5 | #include "Servo.h" |
m215676 | 6:3dc339508843 | 6 | #include "rtos.h" //need to add this library for multiple functions running at once |
m215676 | 6:3dc339508843 | 7 | |
m215676 | 6:3dc339508843 | 8 | |
m215676 | 0:9347308e8e1a | 9 | |
m215676 | 1:7133819e4265 | 10 | int s1, s3, s4, i; |
m215676 | 2:60a8105d0d20 | 11 | float x; |
m215676 | 5:27689dbb0c6e | 12 | float y; |
m215676 | 6:3dc339508843 | 13 | AnalogIn turn(p15); //turn dial to adjust servo for horizontal angle of arm SWITCH 2 |
m215676 | 7:adcdd7556997 | 14 | DigitalIn swtch1(p25); //moves herndon monument on turn table continuously while switch is on |
m215676 | 7:adcdd7556997 | 15 | DigitalIn swtch3(p18); //releases the cover when switch 3 is turned on |
m215676 | 7:adcdd7556997 | 16 | DigitalIn swtch4(p19); //retracts arm back to starting position to try again |
m215676 | 6:3dc339508843 | 17 | Motor m(p26, p30, p29); //launches the cover |
m215676 | 6:3dc339508843 | 18 | //DigitalIn sw1 (p16) ; not sure why this is here - maybe broken pin |
m215676 | 1:7133819e4265 | 19 | |
m215676 | 5:27689dbb0c6e | 20 | Servo back(p21); //servo to move monument on turn table |
m215676 | 5:27689dbb0c6e | 21 | Servo angle(p22); //servo to adjust horizontal angle of arm |
m215676 | 0:9347308e8e1a | 22 | |
m215676 | 0:9347308e8e1a | 23 | int main() { |
m215676 | 7:adcdd7556997 | 24 | /* |
m215676 | 1:7133819e4265 | 25 | s1 = swtch1.read(); //read state of switch 1 |
m215676 | 5:27689dbb0c6e | 26 | angle.calibrate(0.0009, 90.0); |
m215676 | 1:7133819e4265 | 27 | back.calibrate(0.0009, 90.0); //calculate's servos timing by setting pulse width and range |
m215676 | 0:9347308e8e1a | 28 | |
m215676 | 6:3dc339508843 | 29 | while (s1 == 1) //moves herndon monument continuously on turn table |
m215676 | 1:7133819e4265 | 30 | { |
m215676 | 5:27689dbb0c6e | 31 | for (i = 0; i < 18; i++) //moves monument 180 degrees one way |
m215676 | 1:7133819e4265 | 32 | { |
m215676 | 1:7133819e4265 | 33 | back = 0; |
m215676 | 1:7133819e4265 | 34 | back = (0.0 + (i/18.0)); |
m215676 | 1:7133819e4265 | 35 | s1 = swtch1.read(); //read state of switch 1 |
m215676 | 1:7133819e4265 | 36 | }//end of for loop |
m215676 | 1:7133819e4265 | 37 | |
m215676 | 6:3dc339508843 | 38 | i=0; |
m215676 | 6:3dc339508843 | 39 | |
m215676 | 6:3dc339508843 | 40 | for (i = 0; i < 18; i++) //moves monument back and another 180 degress the opposite way |
m215676 | 1:7133819e4265 | 41 | { |
m215676 | 1:7133819e4265 | 42 | back = 1; |
m215676 | 1:7133819e4265 | 43 | back = (1.0 - (i/18.0)); |
m215676 | 1:7133819e4265 | 44 | s1 = swtch1.read(); //read state of switch 1 |
m215676 | 1:7133819e4265 | 45 | }//end of for loop |
m215676 | 1:7133819e4265 | 46 | |
m215676 | 6:3dc339508843 | 47 | i = 0; |
m215676 | 6:3dc339508843 | 48 | |
m215676 | 1:7133819e4265 | 49 | }//end of while loop |
m215676 | 1:7133819e4265 | 50 | |
m215676 | 2:60a8105d0d20 | 51 | |
m215676 | 5:27689dbb0c6e | 52 | |
m215676 | 5:27689dbb0c6e | 53 | //Adjust horiztonal angle with switch 2/dial |
m215676 | 5:27689dbb0c6e | 54 | while(1) { |
m215676 | 5:27689dbb0c6e | 55 | y = turn.read(); //reads the state of the dial the user changes |
m215676 | 5:27689dbb0c6e | 56 | printf("The dial is at %f\n", x); |
m215676 | 5:27689dbb0c6e | 57 | angle.write(y); //makes the servo turn according to the dial |
m215676 | 5:27689dbb0c6e | 58 | printf("The servo is at %f\n", x); |
m215676 | 5:27689dbb0c6e | 59 | |
m215676 | 5:27689dbb0c6e | 60 | } //end of while loop |
m215676 | 5:27689dbb0c6e | 61 | |
m215676 | 7:adcdd7556997 | 62 | */ |
m215676 | 7:adcdd7556997 | 63 | while(1) { |
nhersom | 4:a3e95c9d56d5 | 64 | |
m215676 | 7:adcdd7556997 | 65 | swtch3.read(); //read state of switch 3 (launch the cover) |
m215676 | 7:adcdd7556997 | 66 | if (swtch3==1) { |
nhersom | 3:50cee0fef078 | 67 | while(1) { |
m215676 | 6:3dc339508843 | 68 | printf("motor on"); |
m215676 | 6:3dc339508843 | 69 | m.speed(1.0); |
m215676 | 6:3dc339508843 | 70 | wait (.4); |
m215676 | 6:3dc339508843 | 71 | printf("motor off"); |
m215676 | 6:3dc339508843 | 72 | m.speed(0.0); |
nhersom | 3:50cee0fef078 | 73 | wait (1); |
nhersom | 3:50cee0fef078 | 74 | break; |
m215676 | 5:27689dbb0c6e | 75 | } //end of while |
m215676 | 5:27689dbb0c6e | 76 | } //end of if |
m215676 | 5:27689dbb0c6e | 77 | |
m215676 | 5:27689dbb0c6e | 78 | |
m215676 | 5:27689dbb0c6e | 79 | |
m215676 | 5:27689dbb0c6e | 80 | |
m215676 | 7:adcdd7556997 | 81 | s4 = swtch4.read(); //read state of switch 4 (retract arm to try again) |
m215676 | 5:27689dbb0c6e | 82 | if (s4 == 1){ |
nhersom | 3:50cee0fef078 | 83 | while(1) { |
m215676 | 6:3dc339508843 | 84 | printf("reversed"); |
nhersom | 3:50cee0fef078 | 85 | m.speed(-.25); |
nhersom | 3:50cee0fef078 | 86 | wait(.6); |
m215676 | 6:3dc339508843 | 87 | printf("motor off"); |
nhersom | 3:50cee0fef078 | 88 | m.speed(0); |
nhersom | 3:50cee0fef078 | 89 | wait(1); |
nhersom | 3:50cee0fef078 | 90 | break; |
m215676 | 5:27689dbb0c6e | 91 | } //end of while |
m215676 | 5:27689dbb0c6e | 92 | } //end of if |
m215676 | 7:adcdd7556997 | 93 | }//end of int main |
m215676 | 7:adcdd7556997 | 94 | } |