this is

Dependencies:   mbed Servo Motor

Committer:
McGavick
Date:
Tue Oct 16 15:43:05 2018 +0000
Revision:
2:260539a94ad7
Parent:
1:cfefb1f7549e
Child:
3:a7c54d27d7f7
Child:
5:c2d2c1804d2e
Working version

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
m210690 0:df0146f8b257 22
m210690 0:df0146f8b257 23 int main() {
McGavick 2:260539a94ad7 24
McGavick 2:260539a94ad7 25 myservo.calibrate(0.0009,90.0);
McGavick 2:260539a94ad7 26 myservo=0.0;
McGavick 2:260539a94ad7 27 hmyservo.calibrate(0.0009,90.0);
McGavick 2:260539a94ad7 28 hmyservo=0.0;
McGavick 2:260539a94ad7 29 printf("Enter ten digit number\n\r");
McGavick 2:260539a94ad7 30 scanf("%10d\n\r",&s);
McGavick 2:260539a94ad7 31 srand(s);
McGavick 2:260539a94ad7 32 k=0;
McGavick 2:260539a94ad7 33 wait(3);
McGavick 2:260539a94ad7 34
McGavick 2:260539a94ad7 35 for (i=0; i<10; i++){ //for
m210690 1:cfefb1f7549e 36 r = rand()%4;//generate random number 0 to 3
m210690 1:cfefb1f7549e 37 leds[r] = 1;//light up one of the leds
McGavick 2:260539a94ad7 38 wait(0.2);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 39
McGavick 2:260539a94ad7 40 if (button[r] == 1) {//if good response
McGavick 2:260539a94ad7 41 printf("noice\n\r");//good
McGavick 2:260539a94ad7 42 ledz[0]=1;
McGavick 2:260539a94ad7 43 wait(.2);
McGavick 2:260539a94ad7 44 ledz[0]=0;
McGavick 2:260539a94ad7 45 leds[r] = 0;
McGavick 2:260539a94ad7 46 k=1; // ensure fail code isn't activated
McGavick 2:260539a94ad7 47 }//turn off again and loop
McGavick 2:260539a94ad7 48 wait(0.02);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 49
McGavick 2:260539a94ad7 50 if (button[r] == 1) {//if good response
McGavick 2:260539a94ad7 51 printf("noice\n\r");//good
McGavick 2:260539a94ad7 52 ledz[0]=1;
McGavick 2:260539a94ad7 53 wait(.2);
McGavick 2:260539a94ad7 54 ledz[0]=0;
McGavick 2:260539a94ad7 55 leds[r] = 0;
McGavick 2:260539a94ad7 56 l=1;
McGavick 2:260539a94ad7 57 }//turn off again and loop
McGavick 2:260539a94ad7 58 wait(0.02);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 59
McGavick 2:260539a94ad7 60 if (button[r] == 1) {//if good response
McGavick 2:260539a94ad7 61 printf("noice\n\r");//good
McGavick 2:260539a94ad7 62 ledz[0]=1;
McGavick 2:260539a94ad7 63 wait(.2);
McGavick 2:260539a94ad7 64 ledz[0]=0;
McGavick 2:260539a94ad7 65 leds[r] = 0;
McGavick 2:260539a94ad7 66 m=1;
McGavick 2:260539a94ad7 67 }//turn off again and loop
McGavick 2:260539a94ad7 68 wait(0.02);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 69
m210690 1:cfefb1f7549e 70 if (button[r] == 1) {//if good response
m210690 1:cfefb1f7549e 71 printf("noice\n\r");//good
McGavick 2:260539a94ad7 72 ledz[0]=1;
McGavick 2:260539a94ad7 73 wait(.2);
McGavick 2:260539a94ad7 74 ledz[0]=0;
McGavick 2:260539a94ad7 75 leds[r] = 0;
McGavick 2:260539a94ad7 76 n=1;
McGavick 2:260539a94ad7 77 }//turn off again and loop
McGavick 2:260539a94ad7 78 wait(0.02);//have 0.5 seconds to respond
McGavick 2:260539a94ad7 79
McGavick 2:260539a94ad7 80 if (button[r] == 1) {//if good response
McGavick 2:260539a94ad7 81 printf("noice\n\r");//good
McGavick 2:260539a94ad7 82 ledz[0]=1;
McGavick 2:260539a94ad7 83 wait(.2);
McGavick 2:260539a94ad7 84 ledz[0]=0;
McGavick 2:260539a94ad7 85 leds[r] = 0;
McGavick 2:260539a94ad7 86 o=1;
McGavick 2:260539a94ad7 87 }//turn off again and loop
McGavick 2:260539a94ad7 88
McGavick 2:260539a94ad7 89
McGavick 2:260539a94ad7 90
McGavick 2:260539a94ad7 91 if (k==0 && l==0 && m==0 && n==0 && o==0) {printf("fail\n\r");//else you lose
m210690 1:cfefb1f7549e 92 //activate nerf gun
m210690 1:cfefb1f7549e 93 //end code
McGavick 2:260539a94ad7 94 leds[r]=0;
McGavick 2:260539a94ad7 95 ledz[1]=1;
McGavick 2:260539a94ad7 96 wait(.25);
McGavick 2:260539a94ad7 97 ledz[1]=0;
McGavick 2:260539a94ad7 98 }//if
McGavick 2:260539a94ad7 99
McGavick 2:260539a94ad7 100 if (k==1 || l==1 || m==1 || n==1 ||o==1) {w = w + 1;
McGavick 2:260539a94ad7 101 }//end if
McGavick 2:260539a94ad7 102 leds[r]=0;
McGavick 2:260539a94ad7 103 k=0;
McGavick 2:260539a94ad7 104 l=0;
McGavick 2:260539a94ad7 105 m=0;
McGavick 2:260539a94ad7 106 n=0;
McGavick 2:260539a94ad7 107 o=0;
McGavick 2:260539a94ad7 108 printf("%d\n\r", r);
McGavick 2:260539a94ad7 109
McGavick 2:260539a94ad7 110
McGavick 2:260539a94ad7 111
McGavick 2:260539a94ad7 112
McGavick 2:260539a94ad7 113 }//for
McGavick 2:260539a94ad7 114
McGavick 2:260539a94ad7 115 printf("you got %d out of 10 right!\n\r", w);
McGavick 2:260539a94ad7 116
McGavick 2:260539a94ad7 117 if (w < 10) {//if, gun trigger
McGavick 2:260539a94ad7 118
McGavick 2:260539a94ad7 119
McGavick 2:260539a94ad7 120 for (pos =0.0; pos<=10.0; pos=pos+10.0) {
McGavick 2:260539a94ad7 121 myservo=(pos/10.0);
McGavick 2:260539a94ad7 122 wait (0.07);
McGavick 2:260539a94ad7 123 printf("%f\n",pos);
McGavick 2:260539a94ad7 124 }//ends for
McGavick 2:260539a94ad7 125 for (pos =10.0; pos>=0.0; pos=pos-10.0) {
McGavick 2:260539a94ad7 126 myservo=(pos/10.0);
McGavick 2:260539a94ad7 127 wait (0.07);
McGavick 2:260539a94ad7 128 printf("%f\n",pos); // pulls the trigger
McGavick 2:260539a94ad7 129 }//ends for
m210690 1:cfefb1f7549e 130 }//if
McGavick 2:260539a94ad7 131
McGavick 2:260539a94ad7 132 else if (w >= 10) {//if, high five
McGavick 2:260539a94ad7 133
McGavick 2:260539a94ad7 134
McGavick 2:260539a94ad7 135
McGavick 2:260539a94ad7 136
McGavick 2:260539a94ad7 137 for (hpos =0.0; hpos<=40.0; hpos=hpos+10.0) {
McGavick 2:260539a94ad7 138 hmyservo=(hpos/90.0);
McGavick 2:260539a94ad7 139 wait (0.07);
McGavick 2:260539a94ad7 140 printf("%f\n",hpos);
McGavick 2:260539a94ad7 141 }
McGavick 2:260539a94ad7 142 wait(1.0); //shows the hand
McGavick 2:260539a94ad7 143 for (hpos =40.0; hpos>=60.0; hpos=hpos+10.0) {
McGavick 2:260539a94ad7 144 hmyservo=(hpos/90.0);
McGavick 2:260539a94ad7 145 wait (0.07);
McGavick 2:260539a94ad7 146 printf("%f\n",hpos);
McGavick 2:260539a94ad7 147 }
McGavick 2:260539a94ad7 148 wait (1.0); // moves hand forward to hit users hand
McGavick 2:260539a94ad7 149 for (hpos =60.0; hpos>=0.0; hpos=hpos-10.0) {
McGavick 2:260539a94ad7 150 hmyservo=(hpos/90.0);
McGavick 2:260539a94ad7 151 wait (0.07);
McGavick 2:260539a94ad7 152 printf("%f\n",hpos);
McGavick 2:260539a94ad7 153 }
McGavick 2:260539a94ad7 154
McGavick 2:260539a94ad7 155
McGavick 2:260539a94ad7 156 }//end high five if
McGavick 2:260539a94ad7 157 } //main