this is
Dependencies: mbed Servo Motor
main.cpp
- Committer:
- McGavick
- Date:
- 2018-10-19
- Revision:
- 4:6888480c5133
- Parent:
- 3:a7c54d27d7f7
File content as of revision 4:6888480c5133:
#include "mbed.h" // required libraries #include "stdlib.h" #include "stdio.h" #include "Servo.h" DigitalOut leds[4] = {p25, p24, p23, p28}; // defining which pins go to the game's LEDs DigitalOut ledz[2] = {p27,p26}; // defining which pins go to the success/failure lights DigitalIn button[4] = {p17, p18, p19, p20}; // defining which pins receive input from the buttons Servo myservo(p21); // defining the nerf gun servo Servo hmyservo(p22); // defining the high-five servo float hpos; // position variable for the high-five servo float pos; // position variable for the nerf gun servo int r; //used for randomly lighting LEDs int i; //counter int s; // seed int k; // preventative variable int l; // preventative variable int m; // preventative variable int n; // preventatibe variable int o; // preventative variable int w = 0; //counter for successful attempts float timer; // variable used to easily change the pace of the game int main() { myservo.calibrate(0.0009,90.0); // calibrating nerf gun servo myservo=0.0; // initial position of nerf gun servo hmyservo.calibrate(0.0009,90.0); // calibrating high-five servo hmyservo=0.0; // initial position of high-five servo printf("Enter ten digit number\n\r"); // requesting user for ten digits scanf("%10d\n\r",&s); // scanning those digits into variable s srand(s); // seeding rand() with those ten digits k=0; // initializing timer = .02; // initializing wait(3); // give the player three seconds from putting in twn digits to the start of the game for (i=0; i<10; i++){ //for r = rand()%4;//generate random number 0 to 3 leds[r] = 1;//light up one of the leds wait(0.2);//have 0.5 seconds to respond if (button[r] == 1) {//if good response printf("noice\n\r");//good ledz[0]=1; wait(.2); ledz[0]=0; leds[r] = 0; k=1; // ensure fail code isn't activated }//turn off again and loop wait(timer);//have 0.5 seconds to respond if (button[r] == 1) {//if good response printf("noice\n\r");//good ledz[0]=1; wait(.2); ledz[0]=0; leds[r] = 0; l=1; }//turn off again and loop wait(timer);//have 0.5 seconds to respond if (button[r] == 1) {//if good response printf("noice\n\r");//good ledz[0]=1; wait(.2); ledz[0]=0; leds[r] = 0; m=1; }//turn off again and loop wait(timer);//have 0.5 seconds to respond if (button[r] == 1) {//if good response printf("noice\n\r");//good ledz[0]=1; wait(.2); ledz[0]=0; leds[r] = 0; n=1; }//turn off again and loop wait(timer);//have 0.5 seconds to respond if (button[r] == 1) {//if good response printf("noice\n\r");//good ledz[0]=1; wait(.2); ledz[0]=0; leds[r] = 0; o=1; }//turn off again and loop if (k==0 && l==0 && m==0 && n==0 && o==0) {printf("fail\n\r");//else you lose //activate nerf gun //end code leds[r]=0; ledz[1]=1; wait(.25); ledz[1]=0; }//if if (k==1 || l==1 || m==1 || n==1 ||o==1) { // if w = w + 1; }//end if leds[r]=0; k=0; l=0; m=0; n=0; o=0; printf("%d\n\r", r); }//for printf("you got %d out of 10 right!\n\r", w); if (w < 10) {//if, gun trigger for (pos =0.0; pos<=10.0; pos=pos+10.0) { myservo=(pos/10.0); wait (0.07); printf("%f\n",pos); }//ends for for (pos =10.0; pos>=0.0; pos=pos-10.0) { myservo=(pos/10.0); wait (0.07); printf("%f\n",pos); // pulls the trigger }//ends for }//if else if (w >= 10) {//if, high five for (hpos =0.0; hpos<=40.0; hpos=hpos+10.0) { hmyservo=(hpos/90.0); wait (0.07); printf("%f\n",hpos); } wait(1.0); //shows the hand for (hpos =40.0; hpos>=60.0; hpos=hpos+10.0) { hmyservo=(hpos/90.0); wait (0.07); printf("%f\n",hpos); } wait (1.0); // moves hand forward to hit users hand for (hpos =60.0; hpos>=0.0; hpos=hpos-10.0) { hmyservo=(hpos/90.0); wait (0.07); printf("%f\n",hpos); } }//end high five if } //main