#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int numLeds = 6;
const int ledPins[numLeds] = {2, 3, 4, 5, 6, 7};
const int buttonPins[numLeds] = {8, 9, 10, 11, 12, 13};
const int ultrasonicTrigPin = A0;
const int ultrasonicEchoPin = A1;
const int lcdColumns = 16;
const int lcdRows = 2;
int activeLed = -1;
unsigned long startTime = 0;
unsigned long endTime = 0;
unsigned long reactionTime = 0;
unsigned long totalReactionTime = 0;
int tryCount = 0;
bool handDetected = false;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); // Change the address (0x27) if required
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(ultrasonicTrigPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
Serial.begin(9600);
lcd.begin(lcdColumns, lcdRows);
lcd.print("Reaction Time:");
randomSeed(analogRead(0)); // Seed the random number generator with an analog reading.
}
void loop() {
// Wait for the hand to be detected over the ultrasonic sensor.
handDetected = detectHand();
if (handDetected) {
int ledCount = 0;
while (ledCount < 5) {
// Choose a random LED to turn on.
activeLed = random(numLeds);
// Turn on the selected LED and record the start time.
digitalWrite(ledPins[activeLed], HIGH);
startTime = millis();
// Wait for the corresponding button to be pressed (reaction).
while (digitalRead(buttonPins[activeLed]) == HIGH) {
// You can add other tasks here if needed, but avoid using delay().
}
// Record the end time and calculate the reaction time.
endTime = millis();
reactionTime = endTime - startTime;
// Turn off the active LED.
digitalWrite(ledPins[activeLed], LOW);
// Display the reaction time on the Serial Monitor.
Serial.print("LED ");
Serial.print(activeLed + 1);
Serial.print(": Reaction Time (ms): ");
Serial.println(reactionTime);
// Add the reaction time to the total.
totalReactionTime += reactionTime;
// Increment the LED count and try count.
ledCount++;
tryCount++;
// Wait for a brief pause before starting the next test.
delay(1000);
}
// Calculate the average reaction time and display on the LCD.
unsigned long averageReactionTime = totalReactionTime / 5;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Avg Reaction:");
lcd.setCursor(0, 1);
lcd.print(averageReactionTime);
// Reset the total reaction time and try count for the next set of 5 tries.
totalReactionTime = 0;
tryCount = 0;
// Wait for a brief pause before starting the next set of 5 tries.
delay(3000);
}
}
bool detectHand() {
// Send a trigger signal to the ultrasonic sensor.
digitalWrite(ultrasonicTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrigPin, LOW);
// Measure the time it takes for the ultrasonic signal to bounce back.
unsigned long duration = pulseIn(ultrasonicEchoPin, HIGH);
// Calculate the distance (in cm) based on the speed of sound (343 m/s).
// Divide by 2 because the sound travels to the object and back.
unsigned int distance = duration * 0.0343 / 2;
// If the detected distance is less than 10 cm, consider it as a hand detected.
if (distance < 10) {
delay(3000); // Wait for at least 3 seconds to confirm the hand presence.
return true;
} else {
return false;
}
}