Mission 20 - Reaction Timer
 DuinoKit Essentials - View Only. See new "Mission Manual" forum for manual and code   Started by Daniel Alich   2017-06-27 21:21:13 -04:00   Comments: 1    Viewed: 697

  1. Daniel Alich
    Daniel Alich Member Staff Member
    252_1511884909_Mission 20 - Reaction Tim


    #include 

    LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
    // ( RS, E, D4, D5, D6, D7)


    float responseTime ;
    long startTime;
    long endTime;
    int wait;
    int button[] = { //ERROR needs to be an array
    2, 3, 4, 5}; //The four button input pins
    int ledpin[] = {
    8, 9, 10, 11}; // LED pins
    int led;
    boolean inGame = false;
    boolean Begin;
    int Rounds = 8;
    int x=0;


    void setup(){
    for(int x=0; x<4; x++) // LED pins are outputs
    {
    pinMode(ledpin[x], OUTPUT);
    }
    for(int x=0; x<4; x++)
    {
    pinMode(button[x], INPUT); // button pins are inputs
    digitalWrite(button[x], HIGH); // enable internal pullup; buttons start in high position; logic reversed
    }

    Serial.begin(9600);
    lcd.begin(16, 2);
    }

    void loop(){

    while (inGame)
    {
    randomSeed(analogRead(6)); //Added to generate "more randomness" with the randomArray for the output function
    // wait = random(100,3000); // Random wait time between 2 and 8 second
    led=random(8,12); // Pick a random number between 7 and 12 for LED
    delay(random (100,3000));
    Serial.print(led);
    digitalWrite(led,HIGH);
    startTime=millis(); //take currect millisecond reading
    Serial.print(" startTime");

    while (digitalRead(led-6) == HIGH) // or startTime+3000 < millis())
    {
    }
    endTime=millis();
    responseTime=responseTime+(endTime-startTime);
    Serial.println (responseTime/1000,3);
    digitalWrite(led,LOW);
    x=x+1;
    delay(1000);
    if ( x == Rounds)
    {
    inGame = false;
    }
    Serial.print ("inGame... Looking for button ");
    Serial.println (led-6);
    }

    while (!inGame){

    Serial.println("Press button to begin");
    Serial.println (responseTime/(1000*Rounds),4);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reaction Timer");
    lcd.setCursor(0, 1);
    lcd.print("Press to Begin");

    while (digitalRead(button[0]) > 0 &&digitalRead(button[1]) > 0 && digitalRead(button[2]) > 0 && digitalRead(button[3]) > 0 )
    {

    led=random(8,12); // Pick a random number between 8 and 12 for LED
    digitalWrite(led, HIGH); // turning it on,
    delay(random(50,400)); // pausing,
    digitalWrite(led, LOW); // and turning it off.
    }
    inGame = true;
    x=0;
    }
    }
    Daniel Alich, 2017-06-27 21:21:13 -04:00
  2. (You must log in or sign up to post here)

Report Post

Write your reason

Log in | Sign up
Terms & Conditions!
Help!