// 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
// Connect the LCD display
// https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight
#include "rgb_lcd.h"
// 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;
rgb_lcd lcd;
void startGame() {
// Start with light OFF.
digitalWrite(pinLed, LOW);
bStart = true;
// Start message
// Print to first column, first row.
lcd.setCursor(0,0);
lcd.print("Click Button...");
// Print to first column, second row.
lcd.setCursor(0,1);
lcd.print("When Light ON!");
}
void setup()
{
// Setup the LCD's number of rows and columns
lcd.begin(16, 2);
// 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;
// Print to first column, first row.
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Your Time: ");
// Print to first column, second row.
lcd.setCursor(0,1);
lcd.print(reactTime);
lcd.print(" ms");
delay(3000);
// Play again!
startGame();
}
}