Start of project 2

Dependencies:   Motor Servo mbed mbed-rtos

Committer:
m215676
Date:
Tue Oct 16 13:16:20 2018 +0000
Revision:
5:27689dbb0c6e
Parent:
4:a3e95c9d56d5
Child:
6:3dc339508843
Added working user input through the dial for switch 2 to move arm horizontally

Who changed what in which revision?

UserRevisionLine numberNew 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 0:9347308e8e1a 6
m215676 1:7133819e4265 7 int s1, s3, s4, i;
m215676 2:60a8105d0d20 8 float x;
m215676 5:27689dbb0c6e 9 float y;
m215676 5:27689dbb0c6e 10 AnalogIn turn(p15); //turn dial to adjust servo for horizontal angle of arm
m215676 1:7133819e4265 11 DigitalIn swtch1(p19);
m215676 1:7133819e4265 12 DigitalIn swtch3(p20);
m215676 1:7133819e4265 13 DigitalIn swtch4(p21);
m215676 0:9347308e8e1a 14 Motor m(p26, p30, p29);
nhersom 4:a3e95c9d56d5 15 DigitalIn sw1 (p16) ;
m215676 1:7133819e4265 16
m215676 5:27689dbb0c6e 17 Servo back(p21); //servo to move monument on turn table
m215676 5:27689dbb0c6e 18 Servo angle(p22); //servo to adjust horizontal angle of arm
m215676 0:9347308e8e1a 19
m215676 0:9347308e8e1a 20 int main() {
m215676 1:7133819e4265 21 s1 = swtch1.read(); //read state of switch 1
m215676 5:27689dbb0c6e 22 angle.calibrate(0.0009, 90.0);
m215676 1:7133819e4265 23 back.calibrate(0.0009, 90.0); //calculate's servos timing by setting pulse width and range
m215676 0:9347308e8e1a 24
m215676 1:7133819e4265 25 while (s1 == 1)
m215676 1:7133819e4265 26 {
m215676 5:27689dbb0c6e 27 for (i = 0; i < 18; i++) //moves monument 180 degrees one way
m215676 1:7133819e4265 28 {
m215676 1:7133819e4265 29 back = 0;
m215676 1:7133819e4265 30 back = (0.0 + (i/18.0));
m215676 1:7133819e4265 31 s1 = swtch1.read(); //read state of switch 1
m215676 1:7133819e4265 32 }//end of for loop
m215676 1:7133819e4265 33
m215676 5:27689dbb0c6e 34 for (i = 0; i < 35; i++) //moves monument back and another 180 degress the opposite way
m215676 1:7133819e4265 35 {
m215676 1:7133819e4265 36 back = 1;
m215676 1:7133819e4265 37 back = (1.0 - (i/18.0));
m215676 1:7133819e4265 38 s1 = swtch1.read(); //read state of switch 1
m215676 1:7133819e4265 39 }//end of for loop
m215676 1:7133819e4265 40
m215676 1:7133819e4265 41 }//end of while loop
m215676 1:7133819e4265 42
m215676 2:60a8105d0d20 43
m215676 5:27689dbb0c6e 44
m215676 5:27689dbb0c6e 45 //Adjust horiztonal angle with switch 2/dial
m215676 5:27689dbb0c6e 46 while(1) {
m215676 5:27689dbb0c6e 47 y = turn.read(); //reads the state of the dial the user changes
m215676 5:27689dbb0c6e 48 printf("The dial is at %f\n", x);
m215676 5:27689dbb0c6e 49 angle.write(y); //makes the servo turn according to the dial
m215676 5:27689dbb0c6e 50 printf("The servo is at %f\n", x);
m215676 5:27689dbb0c6e 51
m215676 5:27689dbb0c6e 52 } //end of while loop
m215676 5:27689dbb0c6e 53
m215676 5:27689dbb0c6e 54
nhersom 4:a3e95c9d56d5 55
m215676 5:27689dbb0c6e 56 sw3.read(); //read state of switch 3 (launch the cover)
m215676 5:27689dbb0c6e 57 if (sw3==1) {
nhersom 3:50cee0fef078 58 while(1) {
nhersom 3:50cee0fef078 59 m.speed(1);
nhersom 3:50cee0fef078 60 wait (.15);
nhersom 3:50cee0fef078 61 m.speed(0);
nhersom 3:50cee0fef078 62 wait (1);
nhersom 3:50cee0fef078 63 break;
m215676 5:27689dbb0c6e 64 } //end of while
m215676 5:27689dbb0c6e 65 } //end of if
m215676 5:27689dbb0c6e 66
m215676 5:27689dbb0c6e 67
m215676 5:27689dbb0c6e 68
m215676 5:27689dbb0c6e 69
m215676 5:27689dbb0c6e 70 sw4.read(); //read state of switch 4 (retract arm to try again)
m215676 5:27689dbb0c6e 71 if (s4 == 1){
nhersom 3:50cee0fef078 72 while(1) {
nhersom 3:50cee0fef078 73 m.speed(-.25);
nhersom 3:50cee0fef078 74 wait(.6);
nhersom 3:50cee0fef078 75 m.speed(0);
nhersom 3:50cee0fef078 76 wait(1);
nhersom 3:50cee0fef078 77 break;
m215676 5:27689dbb0c6e 78 } //end of while
m215676 5:27689dbb0c6e 79 } //end of if
m215676 0:9347308e8e1a 80 } //end of int main