/*
William Rice, USA
Based on Arduino Reaction Timer using an RGB LCD 16x2 Character Display
Derek Molloy, DCU, Ireland.
*/
#include <TM1637Display.h>
// Set up the display with 4 bits - R/W tied to GND
int CLK1 = 8;
int DIO1 = 9;
int CLK2 = 10;
int DIO2 = 11;
int CLK3 = 12;
int DIO3 = 13;
int reset = A1;
int count = 0;
int ledPin1 = A3; // red stop LED
int ledPin2 = A2; // red stop LED
int sel1 = 2; // select program 1
int sel2 = 3; // select program 2
int sel3 = 4; // select program 3
int start = 5; // start program
int buttonPin1 = 6; // reaction timer button1
int buttonPin2 = 7; // reaction timer button1
TM1637Display display1 = TM1637Display(CLK1, DIO1); // LED display
TM1637Display display2 = TM1637Display(CLK3, DIO3); // LED display
TM1637Display display3 = TM1637Display(CLK2, DIO2); // LED display
// States of execution
long countt;
long randomDelayTime; // holds the random time amount
long timerStartMillis; // the time when the timer started
long timerEndMillis; // the time when the timer finished
enum State {prepareState, waitState, timingState};
State myProgrammState = prepareState;
// Setup function - called only once
void setup() {
display1.setBrightness(5);
display2.setBrightness(5);
display3.setBrightness(5);
digitalWrite(reset , HIGH);
pinMode(reset, OUTPUT);
pinMode(ledPin1, OUTPUT); // red LED is an output
pinMode(ledPin2, OUTPUT); // red LED is an output
pinMode(buttonPin1, INPUT); // button is an input
pinMode(buttonPin2, INPUT); // button is an input
pinMode(sel1, INPUT); // button is an input
pinMode(sel2, INPUT); // button is an input
pinMode(sel3, INPUT); // button is an input
pinMode(start, INPUT); // button is an input
randomSeed(analogRead(0)); // use unconnected pin to seed random sequence
}
void loop() {
count = millis();
count++;
if (digitalRead(sel2) == true)
switch (myProgrammState) {
case prepareState:
digitalWrite(ledPin1, LOW); // turn off the red led
digitalWrite(ledPin2, LOW); // turn off the red led
randomDelayTime = random(1000, 5000); // this is the random amount to be used 0-10 seconds
randomDelayTime = random(1000, 5000); // this is the random amount to be used 0-10 seconds
while ((digitalRead(buttonPin1) == true) ||
(digitalRead(buttonPin2) == true)) {} // wait until the buttons are released
myProgrammState = waitState; // finished prepare state - lets move on
display1.clear();
display2.clear();
display3.clear();
display2.showNumberDecEx(randomDelayTime, false, 2, 4 );
break;
case waitState:
delay(randomDelayTime); // delay for the random amount
digitalWrite(ledPin1, HIGH); // when finished - set red LED high
digitalWrite(ledPin2, HIGH); // when finished - set red LED high
myProgrammState = timingState; // now we are ready to start timing reactions
timerStartMillis = millis(); // get the current time
break;
case timingState:
if (digitalRead(buttonPin1) == true) { // when they press the button
timerEndMillis = millis(); // get the current time
digitalWrite(ledPin1, LOW); // turn off the red led
long difference = timerEndMillis - timerStartMillis; // time taken is difference between times
// valid time
// Final message
display1.clear();
display3.clear();
digitalRead(buttonPin1) == true;
display1.showNumberDecEx(difference, false, 2, 4 );
delay(5000); // leave the message on the screen for 5 seconds
myProgrammState = prepareState; // ready to start all over
break;
}
// else if (digitalRead(sel1) == true) {
// // Placeholder for sel1 functionality
// // Implement routine for sel1
// 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
// 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
// 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
// }
// }
// else if (digitalRead(sel3) == true) { // Check if select pin 3 is HIGH
// // Placeholder for sel3 functionality
// // Implement routine for sel3
// }
// else if (digitalRead(sel3) == true) {
// // Placeholder for sel3 functionality
// // Implement routine for sel3
// 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
// 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
// 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
// }
// }
else if (digitalRead(buttonPin2) == true) { // when they press the button
timerEndMillis = millis(); // get the current time
digitalWrite(ledPin2, LOW); // turn off the red led
long difference = timerEndMillis - timerStartMillis; // time taken is difference between times
// valid time
// Final message
display1.clear();
display3.clear();
digitalRead(buttonPin2) == true;
display3.showNumberDecEx(difference, false, 2, 4 );
delay(5000); // leave the message on the screen for 5 seconds
myProgrammState = prepareState; // ready to start all over
break;
}
countt = count - randomDelayTime; //reset timer
if (countt >= 5000) {
{
digitalWrite(reset , LOW);
} }
}
}