Hello! I want to create a Reaction Timer game using an IR Remote. I have an LCD that shows "Press OK to play", you press OK and then the game starts. It shows "Press X", where X is a random number from 0 to 9. You have to press the number and then it must show how much it took you to press the button.
The problem is: I can't seem to make it work, my code so far is:
#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
#include <LiquidCrystal.h>
// set up your codes for the buttons
#define Button_0 0xFF9867
#define Button_1 0xFFA25D
#define Button_2 0xFF629D
#define Button_3 0xFFE21D
#define Button_4 0xFF22DD
#define Button_5 0xFF02FD
#define Button_6 0xFFC23D
#define Button_7 0xFFE01F
#define Button_8 0xFFA857
#define Button_9 0xFF906F
#define Button_ok 0xFF38C7
#define Button_x 0xFF6897
int IR_PIN = 11;
IRrecv irrecv(IR_PIN);
decode_results results;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
unsigned long startTime = 0;
unsigned long endTime = 0;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // start the receiver
lcd.begin(16, 2); // start the LCD
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Press OK");
lcd.setCursor(0, 1);
lcd.print("to play");
// check if there's an IR signal
if (irrecv.decode(&results)) {
switch (results.value) {
case Button_ok: {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ready");
delay(1000);
lcd.print(".");
delay(1000);
lcd.print(".");
delay(1000);
lcd.print(".");
delay(2000);
lcd.clear();
startTime = millis();
int randomNumber = random(0, 9);
lcd.print("Press ");
lcd.print(randomNumber);
delay(1000);
if (results.value == randomNumber) {
endTime = millis();
break;
}
Serial.println(startTime);
Serial.println(endTime);
Serial.println(endTime - startTime);
delay(10000);
}
//default : //Serial.println(results.value, HEX); break;
}
irrecv.resume(); // obtain next value
}
}
but the endTime is always 0. How can I measure the reaction time correctly in this case?
Thank you!