Updated Code
cpp
Copy code
#include <LiquidCrystal.h>
// Constants
const int numSensors = 4;
const int sensorTrigPins[numSensors] = {A0, A2, A4, 12};
const int sensorEchoPins[numSensors] = {A1, A3, A5, 13};
const int ledPins[numSensors] = {8, 9, 10, 11};
const float maxSensorDistance = 5.0; // maximum distance for touch detection in cm
const unsigned long trainingDuration = 60000; // 1 minute in milliseconds
// Global variables
int activeSensor = -1; // Declared globally so it can be accessed in both loop() and
changeActiveSensor()
unsigned long reactionTimes[numSensors] = {0}; // Store reaction times for each sensor
int reactionCount = 0; // Count of valid reactions
// LCD pins
LiquidCrystal lcd(6, 7, 5, 4, 3, 2);
void setup() {
// Initialize LED pins
for (int i = 0; i < numSensors; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(sensorTrigPins[i], OUTPUT);
pinMode(sensorEchoPins[i], INPUT);
}
// Initialize the LCD
[Link](16, 2);
[Link]("Training Active");
// Seed the random number generator
randomSeed(analogRead(0));
void loop() {
static bool trainingActive = true;
static unsigned long trainingStartTime = millis();
static unsigned long startTime = millis();
// Training session duration
if (millis() - trainingStartTime >= trainingDuration) {
trainingActive = false;
if (trainingActive) {
// Check if it's time to change the active sensor
if (millis() - startTime >= random(2000, 5000)) { // Random time between 2 to 5 seconds
startTime = millis();
changeActiveSensor();
}
// Check if the active sensor detects a touch
if (activeSensor != -1 && detectTouch(activeSensor)) {
unsigned long reactionTime = millis() - startTime;
reactionTimes[reactionCount++] = reactionTime; // Store reaction time
[Link]();
[Link](0, 0);
[Link]("Reaction Time:");
[Link](0, 1);
[Link](reactionTime);
[Link](" ms");
delay(3000); // Display the result for 3 seconds
// Reset for the next round
activeSensor = -1;
startTime = millis();
changeActiveSensor();
} else {
[Link]();
[Link](0, 0);
[Link]("TrainingComplete");
delay(3000); // Display message for 3 seconds
// Calculate and display average reaction time
unsigned long totalTime = 0;
for (int i = 0; i < reactionCount; i++) {
totalTime += reactionTimes[i];
unsigned long avgReactionTime = totalTime / reactionCount;
[Link]();
[Link](0, 0);
[Link]("Avg Time:");
[Link](0, 1);
[Link](avgReactionTime);
[Link](" ms");
delay(3000); // Display average time for 3 seconds
// Display grade
char grade = getGrade(avgReactionTime);
[Link]();
[Link](0, 0);
[Link]("Grade: ");
[Link](grade);
delay(3000); // Display the grade for 3 seconds
// Stop further execution
while (true) {
delay(1000);
delay(100); // Adjust the delay as needed
}
void changeActiveSensor() {
// Turn off all LEDs
for (int i = 0; i < numSensors; i++) {
digitalWrite(ledPins[i], LOW);
delay(1000); // 1-second pause before turning on the new LED
// Select a new random sensor
activeSensor = random(numSensors);
// Turn on the new active sensor LED
digitalWrite(ledPins[activeSensor], HIGH);
// Display active sensor on LCD
[Link]();
[Link](0, 0);
[Link]("Active Sensor:");
[Link](0, 1);
[Link](activeSensor + 1);
bool detectTouch(int sensorIndex) {
// Trigger the ultrasonic sensor
digitalWrite(sensorTrigPins[sensorIndex], LOW);
delayMicroseconds(2);
digitalWrite(sensorTrigPins[sensorIndex], HIGH);
delayMicroseconds(10);
digitalWrite(sensorTrigPins[sensorIndex], LOW);
// Read the echo pin
long duration = pulseIn(sensorEchoPins[sensorIndex], HIGH);
float distance = duration * 0.0343 / 2; // Calculate distance in cm
return (distance > 0 && distance <= maxSensorDistance);
char getGrade(unsigned long reactionTime) {
if (reactionTime < 500) {
return 'A';
} else if (reactionTime < 1000) {
return 'B';
} else if (reactionTime < 1500) {
return 'C';
} else if (reactionTime < 2000) {
return 'D';
} else {
return 'F';
Wiring Diagram
LEDs:
LED1: Pin 8 (through 220-ohm resistor)
LED2: Pin 9 (through 220-ohm resistor)
LED3: Pin 10 (through 220-ohm resistor)
LED4: Pin 11 (through 220-ohm resistor)
Ultrasonic Sensors:
Sensor1 Trig: A0
Sensor1 Echo: A1
Sensor2 Trig: A2
Sensor2 Echo: A3
Sensor3 Trig: A4
Sensor3 Echo: A5
Sensor4 Trig: 12
Sensor4 Echo: 13
LCD:
RS: Pin 6
EN: Pin 7
D4: Pin 5
D5: Pin 4
D6: Pin 3
D7: Pin 2