Hi, I'm trying to make a reaction game that chooses a random time when I press a button and when the time runs out I want to display a message on an LCD that I see and then press the button as fast as possible. Here comes the tricky part I want to display the time it took me before I pressed the button and display it in seconds and milliseconds on the LCD.
This is the code so far:
#include <Adafruit_LiquidCrystal.h>
Adafruit_LiquidCrystal lcd(0);
int randy;
int stat = LOW;
int stats = LOW;
void setup() {
lcd.begin(16, 2);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
}
void loop() {
float T = 0;
if (digitalRead(1) == LOW) {
randy = random(500, 2000);
stat = !stat;
lcd.clear();
delay(50);
}
while (stat == HIGH) {
randy--;
}
if (randy <= 1) {
lcd.setCursor(3, 1);
lcd.print("PRESS NOW!!");
delay(100);
lcd.clear();
}
while (randy <= 1 && digitalRead(2) == HIGH) {
T++;
lcd.setCursor(3, 2);
lcd.print(T);
}
}
Now not to confuse anyone this is how I want the circuit to ideally look like:
well, I want a delay for a random number of seconds then display a message that tells me to press the button and counts the time it took me before I pressed the button and shows it.
unsigned long t0, t1, deltaT;
delay(random(1000, 5001)); // wait between 1s and 5s
if (digitalRead(6) == HIGH) {
lcd.clear();
lcd.setCursor(3, 1);
lcd.print(F("NOW!!"));
t0 = micros();
while (digitalRead(6) == HIGH); // wait for the button press
t1 = micros();
deltaT =(t1 - t0) / 1000;
// here deltaT is your reaction time in ms
... do something with it
} else {
// player pressed too early
...
}