this is

Dependencies:   mbed Servo Motor

Committer:
McGavick
Date:
Tue Oct 16 17:44:02 2018 +0000
Revision:
3:a7c54d27d7f7
Parent:
2:260539a94ad7
Child:
4:6888480c5133
Added the timer variable rather than having to go through and change every wait time; ; - Marco

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m210690 0:df0146f8b257 1 #include "mbed.h"
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 2:260539a94ad7 6 DigitalOut leds[4] = {p25, p24, p23, p28};
McGavick 2:260539a94ad7 7 DigitalOut ledz[2] = {p27,p26};
m210690 1:cfefb1f7549e 8 DigitalIn button[4] = {p17, p18, p19, p20};
McGavick 2:260539a94ad7 9 Servo myservo(p21);
McGavick 2:260539a94ad7 10 Servo hmyservo(p22);
McGavick 2:260539a94ad7 11 float hpos;
McGavick 2:260539a94ad7 12 float pos;
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 3:a7c54d27d7f7 22 float timer;
m210690 0:df0146f8b257 23
m210690 0:df0146f8b257 24 int main() {
McGavick 2:260539a94ad7 25
McGavick 2:260539a94ad7 26 myservo.calibrate(0.0009,90.0);
McGavick 2:260539a94ad7 27 myservo=0.0;
McGavick 2:260539a94ad7 28 hmyservo.calibrate(0.0009,90.0);
McGavick 2:260539a94ad7 29 hmyservo=0.0;
McGavick 2:260539a94ad7 30 printf("Enter ten digit number\n\r");
McGavick 2:260539a94ad7 31 scanf("%10d\n\r",&s);
McGavick 2:260539a94ad7 32 srand(s);
McGavick 2:260539a94ad7 33 k=0;
McGavick 3:a7c54d27d7f7 34 timer = .02;
McGavick 2:260539a94ad7 35 wait(3);
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