this is

Dependencies:   mbed Servo Motor

Committer:
m209230
Date:
Fri Oct 19 14:13:50 2018 +0000
Revision:
5:c2d2c1804d2e
Parent:
2:260539a94ad7
Child:
6:648b5e2ede73
Motor addition

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