// For this circuit make sure you have an LED and a 100-300 ohm
// Reaction time game!
// Author: David Hogg
// Uses the Grove - Button to control the Grove - LED.
// Connect the Grove - Button to the socket marked D3
// Connect the Grove - LED to D7
// Defines the pins to which the button and LED are connected.
const int pinButton = 3;
const int pinLed = 7;
long randNumber;
long startTime;
long endTime;
boolean bStart;
void startGame() {
// Start with light OFF.
digitalWrite(pinLed, LOW);
bStart = true;
// Start message
Serial.println("Get Ready. Watch the light...");
Serial.println(" -> Click Button when it goes on!");
}
void setup()
{
// Setup the serial monitor
Serial.begin(9600);
// Seed random number generator
randomSeed(analogRead(0));
// Configure the button's pin for input signals.
pinMode(pinButton, INPUT);
// Configure the LED's pin for output.
pinMode(pinLed, OUTPUT);
// Call the start game function.
startGame();
}
void loop()
{
if (bStart) {
// Wait for between .5 seconds and 5 seconds!
randNumber = random(500, 5000);
delay(randNumber);
// Turn ON the light and get start time.
digitalWrite(pinLed, HIGH);
startTime = millis();
bStart = false;
}
// NOW check for when person clicks button!
if(digitalRead(pinButton))
{
// Person had pressed the button! Calculate reaction time.
endTime = millis();
long reactTime = endTime - startTime;
Serial.println("Your Reaction Time....");
Serial.print(reactTime);
Serial.println(" ms");
delay(3000);
// Play again!
startGame();
}
}