
Code to fire the ball
main.cpp@0:6ca20e4aa3f0, 2017-11-28 (annotated)
- Committer:
- ihmclachlan
- Date:
- Tue Nov 28 15:54:44 2017 +0000
- Revision:
- 0:6ca20e4aa3f0
- Child:
- 1:3db7a3534296
Code for project "should" work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ihmclachlan | 0:6ca20e4aa3f0 | 1 | /************************************************************** |
ihmclachlan | 0:6ca20e4aa3f0 | 2 | / ME31001 Group: 1-04 |
ihmclachlan | 0:6ca20e4aa3f0 | 3 | / |
ihmclachlan | 0:6ca20e4aa3f0 | 4 | / Final Project |
ihmclachlan | 0:6ca20e4aa3f0 | 5 | / |
ihmclachlan | 0:6ca20e4aa3f0 | 6 | / The program controls the operation of a ball launcher that fires a |
ihmclachlan | 0:6ca20e4aa3f0 | 7 | ball in a random direction and changes distance fired based on how long |
ihmclachlan | 0:6ca20e4aa3f0 | 8 | it took ball to be returned |
ihmclachlan | 0:6ca20e4aa3f0 | 9 | /**************************************************************/ |
ihmclachlan | 0:6ca20e4aa3f0 | 10 | |
ihmclachlan | 0:6ca20e4aa3f0 | 11 | #include "mbed.h" |
ihmclachlan | 0:6ca20e4aa3f0 | 12 | #include "Servo.h" |
ihmclachlan | 0:6ca20e4aa3f0 | 13 | #include "stdlib.h" |
ihmclachlan | 0:6ca20e4aa3f0 | 14 | #include "time.h" |
ihmclachlan | 0:6ca20e4aa3f0 | 15 | |
ihmclachlan | 0:6ca20e4aa3f0 | 16 | |
ihmclachlan | 0:6ca20e4aa3f0 | 17 | PwmOut FMotor(p23); // set the firing motors as an output |
ihmclachlan | 0:6ca20e4aa3f0 | 18 | Servo myservo1 (p22); // servo to rotate the base |
ihmclachlan | 0:6ca20e4aa3f0 | 19 | Servo myservo2 (p21); // servo to release the ball |
ihmclachlan | 0:6ca20e4aa3f0 | 20 | |
ihmclachlan | 0:6ca20e4aa3f0 | 21 | DigitalIn PB(p17); // a push button that when switched turns on the program and when switched again turns it off |
ihmclachlan | 0:6ca20e4aa3f0 | 22 | DigitalIn LDR(p19); // An LDR sensor to check if the ball is in the right position |
ihmclachlan | 0:6ca20e4aa3f0 | 23 | |
ihmclachlan | 0:6ca20e4aa3f0 | 24 | Timer t; // we need a timer to see how long it has been since the ball was in the chute |
ihmclachlan | 0:6ca20e4aa3f0 | 25 | |
ihmclachlan | 0:6ca20e4aa3f0 | 26 | int main() { |
ihmclachlan | 0:6ca20e4aa3f0 | 27 | |
ihmclachlan | 0:6ca20e4aa3f0 | 28 | int x = 0; // a counter to check if the push button switch has been pressed |
ihmclachlan | 0:6ca20e4aa3f0 | 29 | |
ihmclachlan | 0:6ca20e4aa3f0 | 30 | while(1) { |
ihmclachlan | 0:6ca20e4aa3f0 | 31 | |
ihmclachlan | 0:6ca20e4aa3f0 | 32 | if (PB.read() == 1) //if the pushbutton is pressed then we change the variable x |
ihmclachlan | 0:6ca20e4aa3f0 | 33 | { |
ihmclachlan | 0:6ca20e4aa3f0 | 34 | x = !x; |
ihmclachlan | 0:6ca20e4aa3f0 | 35 | wait (0.25); |
ihmclachlan | 0:6ca20e4aa3f0 | 36 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 37 | |
ihmclachlan | 0:6ca20e4aa3f0 | 38 | |
ihmclachlan | 0:6ca20e4aa3f0 | 39 | if (PB.read() == 0 && x == 0) // if the pushbutton is not currently pressed and x is in the 0 state run the rest of the program |
ihmclachlan | 0:6ca20e4aa3f0 | 40 | { |
ihmclachlan | 0:6ca20e4aa3f0 | 41 | if (LDR) { // if the ball is in position |
ihmclachlan | 0:6ca20e4aa3f0 | 42 | FMotor.period(0.010); // set PWM period to 10 ms |
ihmclachlan | 0:6ca20e4aa3f0 | 43 | FMotor = 0.5; // start the motors at 50% duty cycle |
ihmclachlan | 0:6ca20e4aa3f0 | 44 | |
ihmclachlan | 0:6ca20e4aa3f0 | 45 | int r = rand() % 50; // make r be a random number between 1 and 50 |
ihmclachlan | 0:6ca20e4aa3f0 | 46 | |
ihmclachlan | 0:6ca20e4aa3f0 | 47 | |
ihmclachlan | 0:6ca20e4aa3f0 | 48 | for(float p=0; p<1.0 && r != 35; p += 0.05) { // if r is 35 (could be any number) then the servo will stop turning otherwise it sweeps through its full range |
ihmclachlan | 0:6ca20e4aa3f0 | 49 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 50 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 51 | wait(0.2); } |
ihmclachlan | 0:6ca20e4aa3f0 | 52 | |
ihmclachlan | 0:6ca20e4aa3f0 | 53 | for(float p=1; p>0 && r != 35; p -= 0.05) { |
ihmclachlan | 0:6ca20e4aa3f0 | 54 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 55 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 56 | wait(0.2); |
ihmclachlan | 0:6ca20e4aa3f0 | 57 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 58 | |
ihmclachlan | 0:6ca20e4aa3f0 | 59 | myservo2 = 1; // remove the lever holding the ball up |
ihmclachlan | 0:6ca20e4aa3f0 | 60 | wait (1); |
ihmclachlan | 0:6ca20e4aa3f0 | 61 | myservo2 = 0; // put the leaver back into place |
ihmclachlan | 0:6ca20e4aa3f0 | 62 | wait (3); // give time for the ball to fire |
ihmclachlan | 0:6ca20e4aa3f0 | 63 | t.start(); // start the timer |
ihmclachlan | 0:6ca20e4aa3f0 | 64 | FMotor = 0; // turn off motors |
ihmclachlan | 0:6ca20e4aa3f0 | 65 | |
ihmclachlan | 0:6ca20e4aa3f0 | 66 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 67 | do // create a new loop so that the program doesn't keep on repeating the first part |
ihmclachlan | 0:6ca20e4aa3f0 | 68 | { |
ihmclachlan | 0:6ca20e4aa3f0 | 69 | |
ihmclachlan | 0:6ca20e4aa3f0 | 70 | if (LDR) { // see if the ball is back in position in position |
ihmclachlan | 0:6ca20e4aa3f0 | 71 | t.stop(); // stop the timer to see how long it took the ball to get back |
ihmclachlan | 0:6ca20e4aa3f0 | 72 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 73 | |
ihmclachlan | 0:6ca20e4aa3f0 | 74 | if (LDR && t.read() <5) { // if the ball in back in position and it took less than 5 seconds for the ball to get back |
ihmclachlan | 0:6ca20e4aa3f0 | 75 | FMotor.period(0.010); // set PWM period to 10 ms |
ihmclachlan | 0:6ca20e4aa3f0 | 76 | FMotor = 1; // start the motors at 100% duty cycle |
ihmclachlan | 0:6ca20e4aa3f0 | 77 | |
ihmclachlan | 0:6ca20e4aa3f0 | 78 | int r = rand() % 50; // make r be a random number between 1 and 50 |
ihmclachlan | 0:6ca20e4aa3f0 | 79 | |
ihmclachlan | 0:6ca20e4aa3f0 | 80 | |
ihmclachlan | 0:6ca20e4aa3f0 | 81 | for(float p=0; p<1.0 && r != 35; p += 0.05) { // if r is 35 (could be any number) then the servo will stop turning otherwise it sweeps through its full range |
ihmclachlan | 0:6ca20e4aa3f0 | 82 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 83 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 84 | wait(0.2); } |
ihmclachlan | 0:6ca20e4aa3f0 | 85 | |
ihmclachlan | 0:6ca20e4aa3f0 | 86 | for(float p=1; p>0 && r != 35; p -= 0.05) { |
ihmclachlan | 0:6ca20e4aa3f0 | 87 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 88 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 89 | wait(0.2); |
ihmclachlan | 0:6ca20e4aa3f0 | 90 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 91 | |
ihmclachlan | 0:6ca20e4aa3f0 | 92 | myservo2 = 1; // remove the lever holding the ball up |
ihmclachlan | 0:6ca20e4aa3f0 | 93 | wait (1); |
ihmclachlan | 0:6ca20e4aa3f0 | 94 | myservo2 = 0; // put the leaver back into place |
ihmclachlan | 0:6ca20e4aa3f0 | 95 | wait (3); // give time for the ball to fire |
ihmclachlan | 0:6ca20e4aa3f0 | 96 | t.start(); // start the timer |
ihmclachlan | 0:6ca20e4aa3f0 | 97 | FMotor = 0; // turn off motors |
ihmclachlan | 0:6ca20e4aa3f0 | 98 | |
ihmclachlan | 0:6ca20e4aa3f0 | 99 | |
ihmclachlan | 0:6ca20e4aa3f0 | 100 | if (LDR) { // see if the ball is back in position in position |
ihmclachlan | 0:6ca20e4aa3f0 | 101 | t.stop(); // stop the timer to see how long it took the ball to get back |
ihmclachlan | 0:6ca20e4aa3f0 | 102 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 103 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 104 | if (LDR && t.read() >5) { // if the ball in back in position and it took less than 5 seconds for the ball to get back |
ihmclachlan | 0:6ca20e4aa3f0 | 105 | FMotor.period(0.010); // set PWM period to 10 ms |
ihmclachlan | 0:6ca20e4aa3f0 | 106 | FMotor = 0.25; // start the motors at 25% duty cycle |
ihmclachlan | 0:6ca20e4aa3f0 | 107 | |
ihmclachlan | 0:6ca20e4aa3f0 | 108 | int r = rand() % 50; // make r be a random number between 1 and 50 |
ihmclachlan | 0:6ca20e4aa3f0 | 109 | |
ihmclachlan | 0:6ca20e4aa3f0 | 110 | |
ihmclachlan | 0:6ca20e4aa3f0 | 111 | for(float p=0; p<1.0 && r != 35; p += 0.05) { // if r is 35 (could be any number) then the servo will stop turning otherwise it sweeps through its full range |
ihmclachlan | 0:6ca20e4aa3f0 | 112 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 113 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 114 | wait(0.2); } |
ihmclachlan | 0:6ca20e4aa3f0 | 115 | |
ihmclachlan | 0:6ca20e4aa3f0 | 116 | for(float p=1; p>0 && r != 35; p -= 0.05) { |
ihmclachlan | 0:6ca20e4aa3f0 | 117 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 118 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 119 | wait(0.2); |
ihmclachlan | 0:6ca20e4aa3f0 | 120 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 121 | |
ihmclachlan | 0:6ca20e4aa3f0 | 122 | myservo2 = 1; // remove the lever holding the ball up |
ihmclachlan | 0:6ca20e4aa3f0 | 123 | wait (1); |
ihmclachlan | 0:6ca20e4aa3f0 | 124 | myservo2 = 0; // put the leaver back into place |
ihmclachlan | 0:6ca20e4aa3f0 | 125 | wait (3); // give time for the ball to fire |
ihmclachlan | 0:6ca20e4aa3f0 | 126 | t.start(); // start the timer |
ihmclachlan | 0:6ca20e4aa3f0 | 127 | FMotor = 0; // turn off motors |
ihmclachlan | 0:6ca20e4aa3f0 | 128 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 129 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 130 | while ( x == 0); |
ihmclachlan | 0:6ca20e4aa3f0 | 131 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 132 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 133 | |
ihmclachlan | 0:6ca20e4aa3f0 | 134 | } |