// subsystems.us Reaction Timer // #define btnSTART 2 // Change these pins if you want to use different ones #define btnHIT 3 #define ledTrigger 4 void setup() { // setup the switches and LED pinMode(btnSTART, INPUT_PULLUP); pinMode(btnHIT, INPUT_PULLUP); pinMode(ledTrigger, OUTPUT); // Turn off the LED digitalWrite(ledTrigger, HIGH); // Start the Serial port Serial.begin(9600); Serial.println("Arduino Reaction Timer"); Serial.println(); // Randomize the random number generator randomSeed(analogRead(0)); } void loop() { long timeReaction; long timeTotal = 0; Serial.println("Press START button when ready"); // Wait for the START button while (digitalRead(btnSTART)) {} // Then wait for it to be released delay(10); while (!digitalRead(btnSTART)) {} delay(1000); // Start the testing round for (int i = 0; i < 5; i++) { delay(random(500, 5000)); timeReaction = millis(); digitalWrite(ledTrigger, LOW); // Wait for btnHit while (digitalRead(btnHIT)) {} timeReaction = millis() - timeReaction; timeTotal += timeReaction; delay(10); while (!digitalRead(btnHIT)) {} digitalWrite(ledTrigger, HIGH); Serial.print(i + 1); Serial.print(": "); Serial.print(timeReaction); Serial.println(" msec"); delay(1000); } // The set of 5 is done, show the results Serial.println(); Serial.print("Avg = "); Serial.print(timeTotal / 5); Serial.println(" msec"); Serial.println(); }