// Initialize leds, buttons int leds[] = {A1, A2, A3, A4, A5}; int numLeds = 5; int startBtn = 2; int reactBtn = 3; void setup() { // Sets the LEDs to output mode, turns them off. for (int i = 0; i < numLeds; i++) { pinMode(leds[i], OUTPUT); digitalWrite(leds[i], LOW); } // Sets the buttons to INPUT_PULLUP mode pinMode(startBtn, INPUT_PULLUP); pinMode(reactBtn, INPUT_PULLUP); Serial.begin(9600); delay(500); // Clears screen, turns on backlight, hides cursor (for lcd) clearScreen(); Serial.write(157); Serial.write(22); // Writes on the screen Serial.print("F1 Reaction"); Serial.print(" Speed Test"); } void loop() { // Checks for start buton clicked if (digitalRead(startBtn) == LOW) { delay(200); runTest(); } } // This is the function that occurs when the start button is clicked, it clears the screen and writes "Get Ready..." on the screen void runTest() { clearScreen(); Serial.println("Get ready..."); // Checks that all Leds are off for (int i = 0; i < numLeds; i++) { digitalWrite(leds[i], LOW); } // Turns leds on one by one for (int i = 0; i < numLeds; i++) { digitalWrite(leds[i], HIGH); beep(150); delay(800); } // Assigns Random delay tiome for lights to be turned off int delayTime = random(200, 4000); unsigned long waitStart = millis(); // IF False Start, this happens if the player hits the button before the lights go out. The lights flash 2 times and the speaker beeps as well. It says "Jump Start!" on the screen and after 2 seconds it all goes away and is replaced by the title again. while (millis() - waitStart < delayTime) { if (digitalRead(reactBtn) == LOW) { clearScreen(); Serial.println("Jump Start!"); for (int j = 0; j < 3; j++) { for (int i = 0; i < numLeds; i++) digitalWrite(leds[i], HIGH); beep(500); for (int i = 0; i < numLeds; i++) digitalWrite(leds[i], LOW); beep(500); } delay(2000); clearScreen(); Serial.print("F1 Reaction"); Serial.print(" Speed Test."); return; } } for (int i = 0; i < numLeds; i++) { digitalWrite(leds[i], LOW); } unsigned long start = millis(); // If Correct Start, this is what happens when ytou hit the button once the lights go out, it calculates your time in ms, we divide by 1000 to get time in 0.1 second format, then that time is displayed, after 2 seconds it goes back to the title. while (true) { if (digitalRead(reactBtn) == LOW) { delay(20); unsigned long elapsed = millis() - start; float reaction = elapsed / 1000.0; clearScreen(); Serial.print("Time: "); Serial.print(reaction, 3); Serial.println(" sec"); delay(3000); clearScreen(); Serial.print("F1 Reaction"); Serial.print(" Speed Test"); return; } } } // Beep Function, called each time a light goes on in a sequence void beep(int duration) { Serial.write(230); delay(duration); } // Clear Screen Function, clears the screen void clearScreen() { Serial.write(12); // Clear display delay(5); // Wait for LCD to process Serial.write(22); // Hide cursor, no blink Serial.write(17); // Backlight ON }