this is

Dependencies:   mbed Servo Motor

main.cpp

Committer:
m209230
Date:
2018-10-19
Revision:
5:c2d2c1804d2e
Parent:
2:260539a94ad7
Child:
6:648b5e2ede73

File content as of revision 5:c2d2c1804d2e:

#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,p26};
DigitalIn button[4] = {p17, p18, p19, p20};
Servo myservo(p21);
Servo hmyservo(p22);
Motor m(p29);
float hpos;
float pos;
float f; //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

int main() {
    
    myservo.calibrate(0.0009,90.0);
    myservo=0.0;
    hmyservo.calibrate(0.0009,90.0);
    hmyservo=0.0;
    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.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(0.02);//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(0.02);//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(0.02);//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(0.02);//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
       for (x=0; x<1; x++){
        f = -1.0;
        m.speed(f);
        wait(3);
        m.speed(-f);
        wait(3);
        }
        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