I am new to the world of Arduino, I work on a system that measures and displays the response time of an athlete, for that I must use 7 LED that light up in a random manner or on a selected sequence and each time an lED is on the athlete must hit the corresponding plate to turn off the lED and measured the time between switching on and off of the led, I'm between using a piezoelectric sensor as the impact sensor or a proximity sensor
thank you for supporting me
/* REACTION TIME (with 2 leds) v2.1 beta
Luis Andrés Gonzalez
Reaction time original version from http://www.instructables.com/id/Arduino-Reaction-Time-Tester/?ALLSTEPS
Send data to processing via the Serial Port original from By Elaine Laguerta http://url/of/online/tutorial.cc
*/
const int switchPin = 6; // pin where the button will be connected
const int ledPin1 = 2 ; // Left LED
const int ledPin2 = 8 ; // Middle LED
const int ledPin3 = 12; // Right LED
// declare some variables:
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean gameStarted = false; // true if game has started
boolean timerFlag = false; // true if timer is running
long startTime;
long endTime;
int randomSeconds;
long beginTime;
float elapsedTime;
int maxTimer = 10 * 1000;
void setup() {
// Setup button and LEDs:
pinMode(switchPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// Begin serial communication
Serial.begin(9600);
}
void loop() {
// Check https://www.arduino.cc/en/Tutorial/StateChangeDetection to understand the following code.
// read the pushbutton input pin in the current loop:
currentButton = buttonPressed(lastButton);
// if there's a change from low to high (comparing last and current loop) then the following is true
if (currentButton == HIGH && lastButton == LOW) { // outer IF
if (gameStarted == false) { // middle IF. starts the game
gameStarted = true;
randomSeconds = random(3, 8) * 1000; // generates a random number of seconds between 3 and 8
Blink(1);
Serial.println("9090"); // signal code for start sound
beginTime = millis();
} else {
if ((millis() - beginTime) >= randomSeconds) {
Stop();
gameStarted = false;
timerFlag = false;
} else if ((millis() - beginTime) < randomSeconds ) {
gameStarted = false;
timerFlag = false;
Serial.println("1010"); // signal code for early response
Blink(3);
}
}
} // end outer if
// save the current state as the last state,
//for next time through the loop
lastButton = currentButton;
// If true, starts the response time timer and lights up the LED
if (gameStarted == true && (millis() - beginTime) >= randomSeconds && timerFlag == false) {
timerFlag = true;
Start();
}
}
boolean buttonPressed(boolean last) { //button debouncing function
boolean current = digitalRead(switchPin);
if (last != current) {
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void Start() {
startTime = millis();
Light(ledPin1, "on");
}
void Stop() {
if ( (millis() - beginTime) <= maxTimer ) {
endTime = millis();
elapsedTime = (endTime - startTime) + 5;
float elapsedSeconds = elapsedTime / 1000;
Serial.println(elapsedSeconds);
}
else Serial.println("Timed out");
Light(ledPin1, "off");
}
void Blink(int repetitions) {
for (int i = 0; i < repetitions; i++) {
digitalWrite(ledPin2, HIGH);
delay(100);
digitalWrite(ledPin2, LOW);
delay(100);
}
}
void Light(int lednumber, String onOff) {
if (onOff == "on") {
digitalWrite(lednumber, HIGH);
} else if (onOff == "off") {
digitalWrite(lednumber, LOW);
}
}