this is

Dependencies:   mbed Servo Motor

main.cpp

Committer:
m210690
Date:
2018-10-19
Revision:
6:648b5e2ede73
Parent:
5:c2d2c1804d2e
Child:
8:67b2471a7346

File content as of revision 6:648b5e2ede73:

#include "mbed.h"
#include "stdlib.h"
#include "stdio.h"
#include "Servo.h"
#include "Motor.h"

DigitalOut leds[4] = {p25, p24, p23, p28};
DigitalOut ledz[2] = {p27,p12};
DigitalIn button[4] = {p17, p18, p19, p20};
Motor z(p26,p30,p29);
Servo myservo(p21);
Servo hmyservo(p22);

float hpos;
float pos;
float f = 1; //Motor speed
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
int x; //count the number of times the motor runs
float timer;

int main() {
    
    myservo.calibrate(0.0009,90.0);
    myservo=0.0;
    hmyservo.calibrate(0.0009,90.0);
    hmyservo=0.0;
    printf("how hard(0.05 is difficult, 0.2 is easier)\n\r");
    scanf("%f", &timer); 
    printf("Enter ten digit number\n\r");
    scanf("%10d\n\r",&s);
    srand(s);
    k=0;
    wait(3);
    
    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.3);//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(0.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) {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
       
        f = -1.0;
        z.speed(f);
        wait(1);
        z.speed(0);
        
        
        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
        z.speed(-f);
        wait(1);
        z.speed(0);
    }//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