/*
William Rice, USA
Based on Arduino Reaction Timer using an RGB LCD 16x2 Character Display
Derek Molloy, DCU, Ireland.
*/
#include <TM1637Display.h> // Include the TM1637Display library for controlling LED displays
// Define pin assignments for the three LED displays
int CLK1 = 8; // Clock pin for the first display
int DIO1 = 9; // Data pin for the first display
int CLK2 = 10; // Clock pin for the second display
int DIO2 = 11; // Data pin for the second display
int CLK3 = 12; // Clock pin for the third display
int DIO3 = 13; // Data pin for the third display
int resetPin = A1; // Pin connected to reset function
int count = 0; // Variable to store count
// Define pin assignments for LEDs, buttons, and program selection
int ledPin1 = A3; // Pin for the first red stop LED
int ledPin2 = A2; // Pin for the second red stop LED
int sel1 = 2; // Pin for selecting program 1
int sel2 = 3; // Pin for selecting program 2
int sel3 = 4; // Pin for selecting program 3
int start = 5; // Pin for starting program
int buttonPin1 = 6; // Pin for reaction timer button1
int buttonPin2 = 7; // Pin for reaction timer button2
// Initialize display objects for the three LED displays
TM1637Display display1 = TM1637Display(CLK1, DIO1); // First LED display object
TM1637Display display2 = TM1637Display(CLK3, DIO3); // Second LED display object
TM1637Display display3 = TM1637Display(CLK2, DIO2); // Third LED display object
// Define states for the program
enum State {prepareState, waitState, timingState}; // Enumeration for different states
State currentState = prepareState; // Initial state is prepareState
// Setup function - called only once
void setup() {
// Set display brightness
display1.setBrightness(5);
display2.setBrightness(5);
display3.setBrightness(5);
// Set reset pin high
digitalWrite(resetPin, HIGH); // Set the reset pin to HIGH initially
pinMode(resetPin, OUTPUT); // Set the reset pin as an output
// Set pin modes
pinMode(ledPin1, OUTPUT); // Set ledPin1 as an output
pinMode(ledPin2, OUTPUT); // Set ledPin2 as an output
pinMode(buttonPin1, INPUT); // Set buttonPin1 as an input
pinMode(buttonPin2, INPUT); // Set buttonPin2 as an input
pinMode(sel1, INPUT); // Set sel1 as an input
pinMode(sel2, INPUT); // Set sel2 as an input
pinMode(sel3, INPUT); // Set sel3 as an input
pinMode(start, INPUT); // Set start as an input
// Seed random number generator
randomSeed(analogRead(0)); // Seed the random number generator with an analog reading
}
// Loop function - runs continuously
void loop() {
count = millis(); // Get current time in milliseconds
count++; // Increment count
// Check if sel2 is true
if (digitalRead(sel2) == true) { // Check if select pin 2 is HIGH
switch (currentState) { // Check the current state
case prepareState: // If in prepare state
digitalWrite(ledPin1, LOW); // Turn off LED 1
digitalWrite(ledPin2, LOW); // Turn off LED 2
long randomDelayTime = random(1000, 5000); // Generate random delay between 1 to 5 seconds
while ((digitalRead(buttonPin1) == true) || (digitalRead(buttonPin2) == true)) {} // Wait until both buttons are released
currentState = waitState; // Change state to wait state
display1.clear(); // Clear display 1
display2.clear(); // Clear display 2
display3.clear(); // Clear display 3
display2.showNumberDecEx(randomDelayTime, false, 2, 4); // Show random delay time on display 2
break;
case waitState: // If in wait state
delay(randomDelayTime); // Wait for the random delay time
digitalWrite(ledPin1, HIGH); // Turn on LED 1
digitalWrite(ledPin2, HIGH); // Turn on LED 2
currentState = timingState; // Change state to timing state
long timerStartMillis = millis(); // Record the start time
break;
case timingState: // If in timing state
if (digitalRead(buttonPin1) == true) { // Check if button 1 is pressed
long timerEndMillis = millis(); // Record the end time
digitalWrite(ledPin1, LOW); // Turn off LED 1
long difference = timerEndMillis - timerStartMillis; // Calculate the difference in time
display1.clear(); // Clear display 1
display1.showNumberDecEx(difference, false, 2, 4); // Show reaction time on display 1
delay(5000); // Delay for 5 seconds
currentState = prepareState; // Change state to prepare state
break;
}
else if (digitalRead(buttonPin2) == true) { // Check if button 2 is pressed
long timerEndMillis = millis(); // Record the end time
digitalWrite(ledPin2, LOW); // Turn off LED 2
long difference = timerEndMillis - timerStartMillis; // Calculate the difference in time
display1.clear(); // Clear display 1
display3.showNumberDecEx(difference, false, 2, 4); // Show reaction time on display 3
delay(5000); // Delay for 5 seconds
currentState = prepareState; // Change state to prepare state
break;
}
long countt = count - randomDelayTime; // Calculate the difference between count and random delay time
if (countt >= 5000) { // If countt is greater than or equal to 5000
digitalWrite(resetPin, LOW); // Reset timer
}
}
}
}