this is
Dependencies: mbed Servo Motor
main.cpp@8:67b2471a7346, 2018-10-23 (annotated)
- Committer:
- m210690
- Date:
- Tue Oct 23 01:48:04 2018 +0000
- Revision:
- 8:67b2471a7346
- Parent:
- 6:648b5e2ede73
FINALVERSION
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
m210690 | 8:67b2471a7346 | 1 | /* WHACK-A-MOLE |
m210690 | 8:67b2471a7346 | 2 | This code executes a whack-a-mole style game using LEDs and buttons and |
m210690 | 8:67b2471a7346 | 3 | depending on the performance of the user, will either give the user a high five |
m210690 | 8:67b2471a7346 | 4 | or open up a rotating door to shoot the user with a nerf bullet. |
m210690 | 8:67b2471a7346 | 5 | Cadet Seth Shields |
m210690 | 8:67b2471a7346 | 6 | MIDN Joseph Brock |
m210690 | 8:67b2471a7346 | 7 | MIDN Marco McGavick |
m210690 | 8:67b2471a7346 | 8 | MIDN Vincent Potente |
m210690 | 8:67b2471a7346 | 9 | */ |
m210690 | 8:67b2471a7346 | 10 | |
m210690 | 0:df0146f8b257 | 11 | #include "mbed.h" |
m210690 | 1:cfefb1f7549e | 12 | #include "stdlib.h" |
m210690 | 1:cfefb1f7549e | 13 | #include "stdio.h" |
McGavick | 2:260539a94ad7 | 14 | #include "Servo.h" |
m210690 | 8:67b2471a7346 | 15 | #include "Motor.h"//include all necessary libraries for the code |
m210690 | 6:648b5e2ede73 | 16 | |
m210690 | 8:67b2471a7346 | 17 | DigitalOut leds[4] = {p25, p24, p23, p28};//the "moles", LEDs |
m210690 | 8:67b2471a7346 | 18 | DigitalOut ledz[2] = {p27,p12};//show failure or success on each press |
m210690 | 8:67b2471a7346 | 19 | DigitalIn button[4] = {p17, p18, p19, p20};//buttons to whack the mole |
m210690 | 8:67b2471a7346 | 20 | Motor z(p26,p30,p29);//motor to open rotating door |
m210690 | 8:67b2471a7346 | 21 | Servo myservo(p21);//guntrigger |
m210690 | 8:67b2471a7346 | 22 | Servo hmyservo(p22);//hand high five |
m210690 | 6:648b5e2ede73 | 23 | |
m210690 | 8:67b2471a7346 | 24 | float hpos;//position of hand |
m210690 | 8:67b2471a7346 | 25 | float pos;//position of bar which pulls trigger |
m210690 | 8:67b2471a7346 | 26 | float f; //Motor speed |
m210690 | 1:cfefb1f7549e | 27 | int r; //used for randomly lighting LEDs |
m210690 | 1:cfefb1f7549e | 28 | int i; //counter |
McGavick | 2:260539a94ad7 | 29 | int s; // seed |
McGavick | 2:260539a94ad7 | 30 | int k; // preventative variable |
McGavick | 2:260539a94ad7 | 31 | int l; // preventative variable |
McGavick | 2:260539a94ad7 | 32 | int m; // preventative variable |
m210690 | 8:67b2471a7346 | 33 | int n; // preventative variable |
McGavick | 2:260539a94ad7 | 34 | int o; // preventative variable |
McGavick | 2:260539a94ad7 | 35 | int w = 0; //counter for successful attempts |
m209230 | 5:c2d2c1804d2e | 36 | int x; //count the number of times the motor runs |
m210690 | 8:67b2471a7346 | 37 | float timer;//times the motor |
m210690 | 0:df0146f8b257 | 38 | |
m210690 | 0:df0146f8b257 | 39 | int main() { |
McGavick | 2:260539a94ad7 | 40 | |
McGavick | 2:260539a94ad7 | 41 | myservo.calibrate(0.0009,90.0); |
m210690 | 8:67b2471a7346 | 42 | myservo=0.0;//calibrates guntrigger |
McGavick | 2:260539a94ad7 | 43 | hmyservo.calibrate(0.0009,90.0); |
m210690 | 8:67b2471a7346 | 44 | hmyservo=0.0;//calibrates hand |
m210690 | 8:67b2471a7346 | 45 | printf("how hard(0.05 is difficult, 0.2 is easier)\n\r");//change difficulty of game |
m210690 | 8:67b2471a7346 | 46 | scanf("%f", &timer); //input for difficulty |
m210690 | 8:67b2471a7346 | 47 | printf("Enter ten digit number\n\r");//seed for random generated numbers to light LEDs |
m210690 | 8:67b2471a7346 | 48 | scanf("%10d\n\r",&s);//input seed |
McGavick | 2:260539a94ad7 | 49 | srand(s); |
m210690 | 8:67b2471a7346 | 50 | k=0;//initialize k to 0 |
m210690 | 8:67b2471a7346 | 51 | wait(3);//begin game |
McGavick | 2:260539a94ad7 | 52 | |
m210690 | 8:67b2471a7346 | 53 | for (i=0; i<10; i++){ //for loop 10 times |
m210690 | 1:cfefb1f7549e | 54 | r = rand()%4;//generate random number 0 to 3 |
m210690 | 1:cfefb1f7549e | 55 | leds[r] = 1;//light up one of the leds |
m210690 | 8:67b2471a7346 | 56 | wait(0.3);//have 0.3 seconds to respond |
McGavick | 2:260539a94ad7 | 57 | |
McGavick | 2:260539a94ad7 | 58 | if (button[r] == 1) {//if good response |
McGavick | 2:260539a94ad7 | 59 | printf("noice\n\r");//good |
m210690 | 8:67b2471a7346 | 60 | ledz[0]=1;//flash good LED |
McGavick | 2:260539a94ad7 | 61 | wait(.2); |
m210690 | 8:67b2471a7346 | 62 | ledz[0]=0;//turn off good LED |
m210690 | 8:67b2471a7346 | 63 | leds[r] = 0;//turn off mole LED |
McGavick | 2:260539a94ad7 | 64 | k=1; // ensure fail code isn't activated |
McGavick | 2:260539a94ad7 | 65 | }//turn off again and loop |
m210690 | 8:67b2471a7346 | 66 | wait(timer);//have determined difficulty time to respond |
McGavick | 2:260539a94ad7 | 67 | |
McGavick | 2:260539a94ad7 | 68 | if (button[r] == 1) {//if good response |
McGavick | 2:260539a94ad7 | 69 | printf("noice\n\r");//good |
McGavick | 2:260539a94ad7 | 70 | ledz[0]=1; |
McGavick | 2:260539a94ad7 | 71 | wait(.2); |
McGavick | 2:260539a94ad7 | 72 | ledz[0]=0; |
m210690 | 8:67b2471a7346 | 73 | leds[r] = 0;//same as above |
m210690 | 8:67b2471a7346 | 74 | l=1;//next counter variable |
McGavick | 2:260539a94ad7 | 75 | }//turn off again and loop |
m210690 | 8:67b2471a7346 | 76 | wait(timer);//have determined difficulty seconds to respond |
McGavick | 2:260539a94ad7 | 77 | |
McGavick | 2:260539a94ad7 | 78 | if (button[r] == 1) {//if good response |
McGavick | 2:260539a94ad7 | 79 | printf("noice\n\r");//good |
McGavick | 2:260539a94ad7 | 80 | ledz[0]=1; |
m210690 | 6:648b5e2ede73 | 81 | wait(0.2); |
McGavick | 2:260539a94ad7 | 82 | ledz[0]=0; |
m210690 | 8:67b2471a7346 | 83 | leds[r] = 0;//same as above |
m210690 | 8:67b2471a7346 | 84 | m=1;//next counter variable |
McGavick | 2:260539a94ad7 | 85 | }//turn off again and loop |
m210690 | 8:67b2471a7346 | 86 | wait(timer);//have determined difficulty seconds to respond |
McGavick | 2:260539a94ad7 | 87 | |
m210690 | 1:cfefb1f7549e | 88 | if (button[r] == 1) {//if good response |
m210690 | 1:cfefb1f7549e | 89 | printf("noice\n\r");//good |
McGavick | 2:260539a94ad7 | 90 | ledz[0]=1; |
McGavick | 2:260539a94ad7 | 91 | wait(.2); |
McGavick | 2:260539a94ad7 | 92 | ledz[0]=0; |
m210690 | 8:67b2471a7346 | 93 | leds[r] = 0;//same as above |
m210690 | 8:67b2471a7346 | 94 | n=1;//next counter variable |
McGavick | 2:260539a94ad7 | 95 | }//turn off again and loop |
m210690 | 8:67b2471a7346 | 96 | wait(timer);//have determined difficulty seconds to respond |
McGavick | 2:260539a94ad7 | 97 | |
McGavick | 2:260539a94ad7 | 98 | if (button[r] == 1) {//if good response |
McGavick | 2:260539a94ad7 | 99 | printf("noice\n\r");//good |
McGavick | 2:260539a94ad7 | 100 | ledz[0]=1; |
McGavick | 2:260539a94ad7 | 101 | wait(.2); |
McGavick | 2:260539a94ad7 | 102 | ledz[0]=0; |
m210690 | 8:67b2471a7346 | 103 | leds[r] = 0;//same as above |
m210690 | 8:67b2471a7346 | 104 | o=1;//next counter variable |
McGavick | 2:260539a94ad7 | 105 | }//turn off again and loop |
McGavick | 2:260539a94ad7 | 106 | |
McGavick | 2:260539a94ad7 | 107 | |
McGavick | 2:260539a94ad7 | 108 | |
m210690 | 8:67b2471a7346 | 109 | if (k==0 && l==0 && m==0 && n==0 && o==0) {printf("fail\n\r");//if you did not hit the button on time |
m210690 | 8:67b2471a7346 | 110 | leds[r]=0;//turn off LED |
m210690 | 8:67b2471a7346 | 111 | ledz[1]=1;//turn on fail LED |
McGavick | 2:260539a94ad7 | 112 | wait(.25); |
m210690 | 8:67b2471a7346 | 113 | ledz[1]=0;//turn off fail LED |
McGavick | 2:260539a94ad7 | 114 | }//if |
McGavick | 2:260539a94ad7 | 115 | |
m210690 | 8:67b2471a7346 | 116 | if (k==1 || l==1 || m==1 || n==1 ||o==1) {w = w + 1;//if you hit the button in the alotted time |
m210690 | 8:67b2471a7346 | 117 | }//end if, w variable counts successes, adds one to w |
McGavick | 2:260539a94ad7 | 118 | leds[r]=0; |
McGavick | 2:260539a94ad7 | 119 | k=0; |
McGavick | 2:260539a94ad7 | 120 | l=0; |
McGavick | 2:260539a94ad7 | 121 | m=0; |
McGavick | 2:260539a94ad7 | 122 | n=0; |
m210690 | 8:67b2471a7346 | 123 | o=0;//reset all variables for next light up |
McGavick | 2:260539a94ad7 | 124 | printf("%d\n\r", r); |
McGavick | 2:260539a94ad7 | 125 | |
McGavick | 2:260539a94ad7 | 126 | |
McGavick | 2:260539a94ad7 | 127 | |
McGavick | 2:260539a94ad7 | 128 | |
McGavick | 2:260539a94ad7 | 129 | }//for |
McGavick | 2:260539a94ad7 | 130 | |
m210690 | 8:67b2471a7346 | 131 | printf("you got %d out of 10 right!\n\r", w);//display how many successful pushes |
McGavick | 2:260539a94ad7 | 132 | |
m210690 | 8:67b2471a7346 | 133 | if (w < 9) {//if 9 or less out of 10, gun trigger |
m210690 | 6:648b5e2ede73 | 134 | |
m210690 | 8:67b2471a7346 | 135 | f = -1.0;//initialize f |
m210690 | 8:67b2471a7346 | 136 | z.speed(f);//activate motor |
m210690 | 8:67b2471a7346 | 137 | wait(.1); |
m210690 | 8:67b2471a7346 | 138 | z.speed(0);//stop motor |
m210690 | 6:648b5e2ede73 | 139 | |
m210690 | 6:648b5e2ede73 | 140 | |
McGavick | 2:260539a94ad7 | 141 | for (pos =0.0; pos<=10.0; pos=pos+10.0) { |
McGavick | 2:260539a94ad7 | 142 | myservo=(pos/10.0); |
McGavick | 2:260539a94ad7 | 143 | wait (0.07); |
McGavick | 2:260539a94ad7 | 144 | printf("%f\n",pos); |
McGavick | 2:260539a94ad7 | 145 | }//ends for |
McGavick | 2:260539a94ad7 | 146 | for (pos =10.0; pos>=0.0; pos=pos-10.0) { |
McGavick | 2:260539a94ad7 | 147 | myservo=(pos/10.0); |
McGavick | 2:260539a94ad7 | 148 | wait (0.07); |
McGavick | 2:260539a94ad7 | 149 | printf("%f\n",pos); // pulls the trigger |
McGavick | 2:260539a94ad7 | 150 | }//ends for |
m210690 | 8:67b2471a7346 | 151 | z.speed(f); |
m210690 | 8:67b2471a7346 | 152 | wait(0.085);//continue motor small amount |
m210690 | 6:648b5e2ede73 | 153 | z.speed(0); |
m210690 | 1:cfefb1f7549e | 154 | }//if |
McGavick | 2:260539a94ad7 | 155 | |
m210690 | 8:67b2471a7346 | 156 | else if (w >= 9) {//if, high five |
McGavick | 2:260539a94ad7 | 157 | |
McGavick | 2:260539a94ad7 | 158 | |
McGavick | 2:260539a94ad7 | 159 | |
McGavick | 2:260539a94ad7 | 160 | |
McGavick | 2:260539a94ad7 | 161 | for (hpos =0.0; hpos<=40.0; hpos=hpos+10.0) { |
McGavick | 2:260539a94ad7 | 162 | hmyservo=(hpos/90.0); |
McGavick | 2:260539a94ad7 | 163 | wait (0.07); |
McGavick | 2:260539a94ad7 | 164 | printf("%f\n",hpos); |
m210690 | 8:67b2471a7346 | 165 | }//for |
McGavick | 2:260539a94ad7 | 166 | wait(1.0); //shows the hand |
McGavick | 2:260539a94ad7 | 167 | for (hpos =40.0; hpos>=60.0; hpos=hpos+10.0) { |
McGavick | 2:260539a94ad7 | 168 | hmyservo=(hpos/90.0); |
McGavick | 2:260539a94ad7 | 169 | wait (0.07); |
McGavick | 2:260539a94ad7 | 170 | printf("%f\n",hpos); |
m210690 | 8:67b2471a7346 | 171 | }//for |
McGavick | 2:260539a94ad7 | 172 | wait (1.0); // moves hand forward to hit users hand |
McGavick | 2:260539a94ad7 | 173 | for (hpos =60.0; hpos>=0.0; hpos=hpos-10.0) { |
McGavick | 2:260539a94ad7 | 174 | hmyservo=(hpos/90.0); |
McGavick | 2:260539a94ad7 | 175 | wait (0.07); |
McGavick | 2:260539a94ad7 | 176 | printf("%f\n",hpos); |
m210690 | 8:67b2471a7346 | 177 | }//for |
McGavick | 2:260539a94ad7 | 178 | |
McGavick | 2:260539a94ad7 | 179 | |
McGavick | 2:260539a94ad7 | 180 | }//end high five if |
McGavick | 2:260539a94ad7 | 181 | } //main |