this is

Dependencies:   mbed Servo Motor

Committer:
McGavick
Date:
Fri Oct 19 14:08:50 2018 +0000
Revision:
4:6888480c5133
Parent:
3:a7c54d27d7f7
Contains timer variable;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
McGavick 4:6888480c5133 1 #include "mbed.h" // required libraries
m210690 1:cfefb1f7549e 2 #include "stdlib.h"
m210690 1:cfefb1f7549e 3 #include "stdio.h"
McGavick 2:260539a94ad7 4 #include "Servo.h"
m210690 0:df0146f8b257 5
McGavick 4:6888480c5133 6 DigitalOut leds[4] = {p25, p24, p23, p28}; // defining which pins go to the game's LEDs
McGavick 4:6888480c5133 7 DigitalOut ledz[2] = {p27,p26}; // defining which pins go to the success/failure lights
McGavick 4:6888480c5133 8 DigitalIn button[4] = {p17, p18, p19, p20}; // defining which pins receive input from the buttons
McGavick 4:6888480c5133 9 Servo myservo(p21); // defining the nerf gun servo
McGavick 4:6888480c5133 10 Servo hmyservo(p22); // defining the high-five servo
McGavick 4:6888480c5133 11 float hpos; // position variable for the high-five servo
McGavick 4:6888480c5133 12 float pos; // position variable for the nerf gun servo
m210690 1:cfefb1f7549e 13 int r; //used for randomly lighting LEDs
m210690 1:cfefb1f7549e 14 int i; //counter
McGavick 2:260539a94ad7 15 int s; // seed
McGavick 2:260539a94ad7 16 int k; // preventative variable
McGavick 2:260539a94ad7 17 int l; // preventative variable
McGavick 2:260539a94ad7 18 int m; // preventative variable
McGavick 2:260539a94ad7 19 int n; // preventatibe variable
McGavick 2:260539a94ad7 20 int o; // preventative variable
McGavick 2:260539a94ad7 21 int w = 0; //counter for successful attempts
McGavick 4:6888480c5133 22 float timer; // variable used to easily change the pace of the game
m210690 0:df0146f8b257 23
m210690 0:df0146f8b257 24 int main() {
McGavick 2:260539a94ad7 25
McGavick 4:6888480c5133 26 myservo.calibrate(0.0009,90.0); // calibrating nerf gun servo
McGavick 4:6888480c5133 27 myservo=0.0; // initial position of nerf gun servo
McGavick 4:6888480c5133 28 hmyservo.calibrate(0.0009,90.0); // calibrating high-five servo
McGavick 4:6888480c5133 29 hmyservo=0.0; // initial position of high-five servo
McGavick 4:6888480c5133 30 printf("Enter ten digit number\n\r"); // requesting user for ten digits
McGavick 4:6888480c5133 31 scanf("%10d\n\r",&s); // scanning those digits into variable s
McGavick 4:6888480c5133 32 srand(s); // seeding rand() with those ten digits
McGavick 4:6888480c5133 33 k=0; // initializing
McGavick 4:6888480c5133 34 timer = .02; // initializing
McGavick 4:6888480c5133 35 wait(3); // give the player three seconds from putting in twn digits to the start of the game
McGavick 2:260539a94ad7 36
McGavick 2:260539a94ad7 37 for (i=0; i<10; i++){ //for
m210690 1:cfefb1f7549e 38 r = rand()%4;//generate random number 0 to 3
m210690 1:cfefb1f7549e 39 leds[r] = 1;//light up one of the leds
McGavick 2:260539a94ad7 40 wait(0.2);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 41
McGavick 2:260539a94ad7 42 if (button[r] == 1) {//if good response
McGavick 2:260539a94ad7 43 printf("noice\n\r");//good
McGavick 2:260539a94ad7 44 ledz[0]=1;
McGavick 2:260539a94ad7 45 wait(.2);
McGavick 2:260539a94ad7 46 ledz[0]=0;
McGavick 2:260539a94ad7 47 leds[r] = 0;
McGavick 2:260539a94ad7 48 k=1; // ensure fail code isn't activated
McGavick 2:260539a94ad7 49 }//turn off again and loop
McGavick 3:a7c54d27d7f7 50 wait(timer);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 51
McGavick 2:260539a94ad7 52 if (button[r] == 1) {//if good response
McGavick 2:260539a94ad7 53 printf("noice\n\r");//good
McGavick 2:260539a94ad7 54 ledz[0]=1;
McGavick 2:260539a94ad7 55 wait(.2);
McGavick 2:260539a94ad7 56 ledz[0]=0;
McGavick 2:260539a94ad7 57 leds[r] = 0;
McGavick 2:260539a94ad7 58 l=1;
McGavick 2:260539a94ad7 59 }//turn off again and loop
McGavick 3:a7c54d27d7f7 60 wait(timer);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 61
McGavick 2:260539a94ad7 62 if (button[r] == 1) {//if good response
McGavick 2:260539a94ad7 63 printf("noice\n\r");//good
McGavick 2:260539a94ad7 64 ledz[0]=1;
McGavick 2:260539a94ad7 65 wait(.2);
McGavick 2:260539a94ad7 66 ledz[0]=0;
McGavick 2:260539a94ad7 67 leds[r] = 0;
McGavick 2:260539a94ad7 68 m=1;
McGavick 2:260539a94ad7 69 }//turn off again and loop
McGavick 3:a7c54d27d7f7 70 wait(timer);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 71
m210690 1:cfefb1f7549e 72 if (button[r] == 1) {//if good response
m210690 1:cfefb1f7549e 73 printf("noice\n\r");//good
McGavick 2:260539a94ad7 74 ledz[0]=1;
McGavick 2:260539a94ad7 75 wait(.2);
McGavick 2:260539a94ad7 76 ledz[0]=0;
McGavick 2:260539a94ad7 77 leds[r] = 0;
McGavick 2:260539a94ad7 78 n=1;
McGavick 2:260539a94ad7 79 }//turn off again and loop
McGavick 3:a7c54d27d7f7 80 wait(timer);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 81
McGavick 2:260539a94ad7 82 if (button[r] == 1) {//if good response
McGavick 2:260539a94ad7 83 printf("noice\n\r");//good
McGavick 2:260539a94ad7 84 ledz[0]=1;
McGavick 2:260539a94ad7 85 wait(.2);
McGavick 2:260539a94ad7 86 ledz[0]=0;
McGavick 2:260539a94ad7 87 leds[r] = 0;
McGavick 2:260539a94ad7 88 o=1;
McGavick 2:260539a94ad7 89 }//turn off again and loop
McGavick 2:260539a94ad7 90
McGavick 2:260539a94ad7 91
McGavick 2:260539a94ad7 92
McGavick 2:260539a94ad7 93 if (k==0 && l==0 && m==0 && n==0 && o==0) {printf("fail\n\r");//else you lose
m210690 1:cfefb1f7549e 94 //activate nerf gun
m210690 1:cfefb1f7549e 95 //end code
McGavick 2:260539a94ad7 96 leds[r]=0;
McGavick 2:260539a94ad7 97 ledz[1]=1;
McGavick 2:260539a94ad7 98 wait(.25);
McGavick 2:260539a94ad7 99 ledz[1]=0;
McGavick 2:260539a94ad7 100 }//if
McGavick 2:260539a94ad7 101
McGavick 3:a7c54d27d7f7 102 if (k==1 || l==1 || m==1 || n==1 ||o==1) { // if
McGavick 3:a7c54d27d7f7 103 w = w + 1;
McGavick 2:260539a94ad7 104 }//end if
McGavick 3:a7c54d27d7f7 105
McGavick 2:260539a94ad7 106 leds[r]=0;
McGavick 2:260539a94ad7 107 k=0;
McGavick 2:260539a94ad7 108 l=0;
McGavick 2:260539a94ad7 109 m=0;
McGavick 2:260539a94ad7 110 n=0;
McGavick 2:260539a94ad7 111 o=0;
McGavick 2:260539a94ad7 112 printf("%d\n\r", r);
McGavick 2:260539a94ad7 113
McGavick 2:260539a94ad7 114
McGavick 2:260539a94ad7 115
McGavick 2:260539a94ad7 116
McGavick 2:260539a94ad7 117 }//for
McGavick 2:260539a94ad7 118
McGavick 2:260539a94ad7 119 printf("you got %d out of 10 right!\n\r", w);
McGavick 2:260539a94ad7 120
McGavick 2:260539a94ad7 121 if (w < 10) {//if, gun trigger
McGavick 2:260539a94ad7 122
McGavick 2:260539a94ad7 123
McGavick 2:260539a94ad7 124 for (pos =0.0; pos<=10.0; pos=pos+10.0) {
McGavick 2:260539a94ad7 125 myservo=(pos/10.0);
McGavick 2:260539a94ad7 126 wait (0.07);
McGavick 2:260539a94ad7 127 printf("%f\n",pos);
McGavick 2:260539a94ad7 128 }//ends for
McGavick 2:260539a94ad7 129 for (pos =10.0; pos>=0.0; pos=pos-10.0) {
McGavick 2:260539a94ad7 130 myservo=(pos/10.0);
McGavick 2:260539a94ad7 131 wait (0.07);
McGavick 2:260539a94ad7 132 printf("%f\n",pos); // pulls the trigger
McGavick 2:260539a94ad7 133 }//ends for
m210690 1:cfefb1f7549e 134 }//if
McGavick 2:260539a94ad7 135
McGavick 2:260539a94ad7 136 else if (w >= 10) {//if, high five
McGavick 2:260539a94ad7 137
McGavick 2:260539a94ad7 138
McGavick 2:260539a94ad7 139
McGavick 2:260539a94ad7 140
McGavick 2:260539a94ad7 141 for (hpos =0.0; hpos<=40.0; hpos=hpos+10.0) {
McGavick 2:260539a94ad7 142 hmyservo=(hpos/90.0);
McGavick 2:260539a94ad7 143 wait (0.07);
McGavick 2:260539a94ad7 144 printf("%f\n",hpos);
McGavick 2:260539a94ad7 145 }
McGavick 2:260539a94ad7 146 wait(1.0); //shows the hand
McGavick 2:260539a94ad7 147 for (hpos =40.0; hpos>=60.0; hpos=hpos+10.0) {
McGavick 2:260539a94ad7 148 hmyservo=(hpos/90.0);
McGavick 2:260539a94ad7 149 wait (0.07);
McGavick 2:260539a94ad7 150 printf("%f\n",hpos);
McGavick 2:260539a94ad7 151 }
McGavick 2:260539a94ad7 152 wait (1.0); // moves hand forward to hit users hand
McGavick 2:260539a94ad7 153 for (hpos =60.0; hpos>=0.0; hpos=hpos-10.0) {
McGavick 2:260539a94ad7 154 hmyservo=(hpos/90.0);
McGavick 2:260539a94ad7 155 wait (0.07);
McGavick 2:260539a94ad7 156 printf("%f\n",hpos);
McGavick 2:260539a94ad7 157 }
McGavick 2:260539a94ad7 158
McGavick 2:260539a94ad7 159
McGavick 2:260539a94ad7 160 }//end high five if
McGavick 2:260539a94ad7 161 } //main