Reaction time game help

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:

You have plenty of spare pins on the Uno. Don't use pin 1, that's needed for uploading your sketch and debugging it using serial monitor.

Thanks didn't know that

#include <Adafruit_LiquidCrystal.h>

Adafruit_LiquidCrystal lcd(0);

int randy;
int stat = LOW;
int stats = LOW;

void setup() {
  lcd.begin(16, 2);
  pinMode(7, INPUT_PULLUP);
  pinMode(6, 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);
  }
}  

Seems you are listening but not taking input into account…

How long do you think you’ll stay in this while loop?

I think I have fixed all the issues in the code but yet it still doesn't work


#include <Adafruit_LiquidCrystal.h>

Adafruit_LiquidCrystal lcd(0);

int randy;

void setup() {
  lcd.begin(16, 2);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  lcd.setCursor(1, 0);
  lcd.print("Loading");
  delay(1000);
  lcd.clear();
}

void loop() {
  float T = 0;
  if (digitalRead(6) == LOW) {
    randy = random(500, 2000);
    lcd.clear();
    delay(50);
  }
  while (randy <= 500) {
    randy--;
  }
  if (randy <= 1) {
    lcd.setCursor(3, 1);
    lcd.print("PRESS NOW!!");
    delay(100);
    lcd.clear();
  }
  while (randy <= 1 && digitalRead(7) == HIGH) {
    T++;
  }
  if (randy <= 1 && digitalRead(7) == LOW) {
    lcd.setCursor(3, 2);
    lcd.print(T);
  }
}

so randy is above or at 500

➜ how many iteration do you expect to have with such a test?

the delay(100); won't help either...

--

what you want to do

  • delay() for a random number of ms
  • check if the button is pressed, if it is, the player pressed ahead of time so loses
  • display the message to press now
  • record the value of micros() in an unsigned long t0 variable
  • keep reading the button's state until it's pressed
  • record the value of micros() in an unsigned long t1 variable

➜ the ∆t = t1 - t0 is the reaction time

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.

exactly what I said - something like this

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
  ...
}

Thanks this helped a lot ive fixed some of the code and it works as expected

as a curtesy for other visitors of the forum, post your full solution. It can help others

have fun!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.