/*
Arduino Reaction Timer using an RGB LCD 16x2 Character Display
Derek Molloy, DCU, Ireland.
Using LCD library from:
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
#include <TM1637Display.h>
// Set up the display with 4 bits - R/W tied to GND
int CLK1 = 5;
int DIO1 = 6;
int CLK2 = 7;
int DIO2 = 8;
int CLK3 = 9;
int DIO3 = 10;
int ledPin = 2; // red stop LED
int buttonPin = 3; // reaction timer button
TM1637Display display1 = TM1637Display(CLK1, DIO1); // LED display
TM1637Display display2 = TM1637Display(CLK2, DIO2); // LED display
TM1637Display display3 = TM1637Display(CLK3, DIO3); // LED display
// States of execution
long randomDelayTime; // holds the random time amount
boolean prepareState = true; // in introduction mode
boolean isTiming = false; // timing the press state
long timerStartMillis; // the time when the timer started
long timerEndMillis; // the time when the timer finished
// makes a dash
const uint8_t blank[] = {
SEG_G, // -
};
// Setup function - called only once
void setup() {
display1.setBrightness(5);
display2.setBrightness(5);
display3.setBrightness(5);
pinMode(ledPin, OUTPUT); // red LED is an output
pinMode(buttonPin, INPUT); // button is an input
randomSeed(analogRead(0)); // use unconnected pin to seed random sequence
}
void loop() {
display1.clear();
if(prepareState){ // prepare state - give out the instruction to press button
//if (digitalRead(buttonPin)==true) // if the button is pressed
{
randomDelayTime = random(10000); // this is the random amount to be used 0-10 seconds
while (digitalRead(buttonPin)==true) {} // wait until the button is released
prepareState = false; // finished prepare state - lets move on
display2.clear();
display2.showNumberDecEx(randomDelayTime, false,2,4 );
}
}
else // not in prepare state
{
if (!isTiming) // the timer isn't running, so we are pausing for random amount
{
delay(randomDelayTime); // delay for the random amount
digitalWrite(ledPin, HIGH); // when finished - set red LED high
isTiming = true; // now we are ready to start timing reactions
timerStartMillis = millis(); // get the current time
}
else // now we are timing person's reaction
{
if (digitalRead(buttonPin)==true) // when they press the button
{
timerEndMillis = millis(); // get the current time
digitalWrite(ledPin, LOW); // turn off the red led
long difference = timerEndMillis - timerStartMillis; // time taken is difference between times
if (difference == 0) // If the difference is 0 they held the button - or are supermen!
{}
else // valid time
{ // Final message
display1.clear();
if (digitalRead(buttonPin)==true) // when they press the button
{}
display1.showNumberDecEx(difference, false,2,4 );}
long i = 7000 - difference;
delay(i);
// leave the message on the screen for 5 seconds
isTiming = false; // ready to start timing again
prepareState = true; // ready to start all over
}}}}