Reactiontimetest not working

Hey :slight_smile: ,

So i wrote a code for a Reactiontimetest but when i assembled the parts and tested it,
i wasn’t even able to press the button when the i heard the sound because it instantly got triggered.

So that means my problem is:

I cant press the button because the Arduino thinks that the button is instantly pressed after
the sound played.

I hope that you can help me :frowning:

Heres my Code:

 #include <Wire.h>
  #include <LiquidCrystal_I2C.h>
  #include <SD.h>
  #include <SPI.h>
  #include <TMRpcm.h>
                                            
  LiquidCrystal_I2C lcd(0x27,20,4);
 
  const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  
  #define SD_ChipSelectPin 4

  TMRpcm tmrpcm;
  
  float times1;
  float timee1;
  float timee2;
  float times2;
  float reaction;

  const int buttonPin1 = 2;
  const int buttonPin2 = 3;
  
  int randNumb;
  int randNumbLR;
  int bs1 = 0;
  int bs2 = 0;
  int lock = 0;
  int counter = 0;
  int bestTry = 0;
  int average = 0;
  int start = 0;
  
void setup()
{
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  randomSeed(analogRead(0));
  lcd.setCursor(16,2);
  lcd.clear();
  tmrpcm.speakerPin = 9;
  if(SD.begin(SD_ChipSelectPin));
  {
    lcd.print("SDCard wurde erkannt");
    Serial.println("Die SD-Karte wurde erkannt");
    delay(1);
  }
  if(!SD.begin(SD_ChipSelectPin));
  {
    lcd.print("SDCard wurde nicht erkannt");
    Serial.println("Die SD-Karte wurde nicht erkannt");
    SD.open("sderkannt.txt");
    delay(1);
  }
}

void loop() 
{
  counter ++;
  
  if(lock == 0)
  {
    bs1 = 0;
    bs2 = 0;
    pinMode(buttonPin1, INPUT_PULLUP);
    pinMode(buttonPin2, INPUT_PULLUP);
    bs1 = digitalRead(buttonPin1);
    bs2 = digitalRead(buttonPin2);
    lcd.clear ();
    lcd.backlight();
    lcd.setCursor(0,0);
    Serial.println("Reaktionstest");
    lcd.print("** Reaktions-Test **");
    lcd.setCursor(3,1);
    lcd.print("Drueck die L/R");
    lcd.setCursor(2,2);
    lcd.print("Taste, sobald du");
    lcd.setCursor(3,3);
    lcd.print("den Ton hoerst");
    delay(5000);
    lcd.clear ();
    lock = 1;
  }
  lcd.backlight();
 Serial.println("Mach dich bereit!");
 lcd.setCursor(7,0);
 lcd.print("Mach");
 delay(150);
 lcd.setCursor(7,1);
 lcd.print("dich");
 delay(150);
 lcd.setCursor(6,2);
 lcd.print("bereit!");
 delay(1000);
 lcd.clear ();
 randNumb = 0;
 randNumbLR = 0;
 randNumbLR = random(1000, 3000);
 randNumb = random(500,10000);
 Serial.println(randNumb);
 delay(randNumb);
 
 if(randNumbLR > 2000){
        times1=millis();
        Serial.println("rechts");
        tmrpcm.setVolume(7);
        tmrpcm.play("rechts.wav");
        timee1=millis();
        if (bs2 == HIGH);{ //Here it should test if the button is pressed or not but it got triggered instanly.
         reaction = timee1-times1;
         Serial.println("Deine Reaktionszeit:");
         Serial.println(reaction);
         lcd.setCursor(0,0);
         lcd.print("Deine Reaktionszeit:");
         lcd.setCursor(4,2);
         lcd.print(counter);
         lcd.print(". Versuch:");
         lcd.setCursor(25,1);
         lcd.print(reaction);
         bs2 = 0;
        }
        }

 if(randNumbLR < 2000) {
        times2=millis();
        Serial.println("links");
        tmrpcm.setVolume(7);
        tmrpcm.play("links.wav");
        timee2=millis();
        if (bs1 == HIGH);{;
        reaction = timee2-times2;
         Serial.println("Deine Reaktionszeit:");
         Serial.println(reaction);
         lcd.setCursor(0,0);
         lcd.print("Deine Reaktionszeit:");
         lcd.setCursor(4,2);
         lcd.print(counter);
         lcd.print(". Versuch:");
         lcd.setCursor(25,1);
         lcd.print(reaction);
         bs1 = 0;
        }
}

     if(counter == 1)
     {
      bestTry = reaction;
     }
      
     if(reaction < bestTry) 
     {
       bestTry = reaction;
     } 

     average = (average * (counter - 1) + reaction) / counter;
     
     lcd.print(" ms");  
     delay (3000);
     lcd.clear ();

     if(counter == 10)
     {
       lcd.setCursor(0,0);
       lcd.print("Bester Wert:");
       lcd.setCursor(13,0);
       lcd.print(bestTry);
       lcd.print(" ms");
       lcd.setCursor(0,2);
       lcd.print("Durchschnitt:");
       lcd.setCursor(13,2);
       lcd.print(average);
       lcd.print(" ms");
     
       counter = 0;
       bestTry = 0;
       average = 0;
       lock = 0;
       start = 0;

       delay (20000);
       lcd.clear ();
     } 
     
     delay (500);
     
}

Hello Leo, Welcome to the forum. For correctly posting your code on your first post ++Karma;

You are setting your pins repeatedly in loop(); as inputs, you should do that once in setup();

I don't know if that's the cause of the problem, but it's the first obvious thing to deal with.

Thank you for your answer, but it didnt work :(

Next thing then is a schematic so I know how it is wired. Hand drawn and photographed is fine.

  counter ++; <------Should be counter++;

This:

        if (bs2 == HIGH);{ //Here it should test if the button is pressed or not but it got triggered instantly

You are using input pullup, which is good. That means the input is high while the button is NOT pressed. Instead of testing for HIGH try testing for LOW. Button pressed should give LOW, not HIGH.

Here you are :slight_smile:
**The Pins are not like they are in reallife

You have set the pinMode of the buttons to INPUT_PULLUP. This means the default state of the button is HIGH. You should have the button connected between the IO pin and ground so that when the button is pressed its state is LOW.

Leomss:         bs1 = 0;

The state of the pin will be either HIGH or LOW. Sure, you can probably get away with assuming that LOW == 0, but why do that? Your code will be much easier to understand if you just use the handy pre-defined digital pin state names.

Similar:

Leomss:  if(lock == 0)

It would make much more sense to make the data type of lock bool and then use true and false:

 if(lock == false)

or if you prefer:

 if(!lock)

Writing code so that it is as self-documenting as possible is a very important habit to get into. Sure, with a simple sketch like this one you can likely figure out what even obfuscated code does, but as you get into more complex projects you will be very grateful to not need to waste some time thinking "What does 0 signify here?" when you are coming back to the code after some time and trying to decipher what is going on.

PerryBebbington:

  counter ++; <------Should be counter++;

It probably should be “++counter;”, but I don’t see that a space makes any difference.


Thank you. Having mastered code tags on your first post I'd have thought you'd also manage to post images so we don't have to download them. Your next task is to learn how to do that (it's a pain).

Change your code to look for LOW instead of HIGH as per my last post.

2 things...
You don't need a battery, it will run off the 5V from the USB cable just fine.

Your button wiring is all wrong. You don't need the resistors, that is what the pullup resistor in the Arduino is for. All you need is one side of the button connected to 0V and the other side to the input. Nothing else.

It probably should be "++counter;", but I don't see that a space makes any difference.

I don't know for sure if the space matters. As far as I know ++variable;, variable++; and variable += 1; all do the same thing. Are they different?

I notice that most people prefer variable++; and that I am about the only person here who regularly uses ++variable;

if (bs1 == HIGH);{;

That's a lot of strange colons

I always heard/read that ++variable increments then tests, while variable++ tests then increments.

I always heard/read that ++variable increments then tests, while variable++ tests then increments.

Don't know. Never had unexpected behaviour using ++variable; (With apologies to Leo and our friendly moderator as we are now hijacking this discussion).

I tried everything but its still not working :c

Leomss:
I tried everything but its still not working :c

If that is true, then your problem is unsolvable. I hate it when that happens.

I tried everything but its still not working

Show us your current best effort including code and revised circuit.

So I’m not currently on My Computer but that’s what I tried:

**I also tried it with a lock so it the buttonstate gets tested when the lock is 0

#include <Wire.h>
  #include <LiquidCrystal_I2C.h>
  #include <SD.h>
  #include <SPI.h>
  #include <TMRpcm.h>
                                           
  LiquidCrystal_I2C lcd(0x27,20,4);
 
  const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
 
  #define SD_ChipSelectPin 4

  TMRpcm tmrpcm;
 
  float times1;
  float timee1;
  float timee2;
  float times2;
  float reaction;

  const int buttonPin1 = 2;
  const int buttonPin2 = 3;
 
  int randNumb;
  int randNumbLR;
  int bs1 = 0;
  int bs2 = 0;
  int lock = 0;
  int counter = 0;
  int bestTry = 0;
  int average = 0;
  int start = 0;
 
void setup()
{
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  randomSeed(analogRead(0));
  lcd.setCursor(16,2);
  lcd.clear();
  tmrpcm.speakerPin = 9;
  if(SD.begin(SD_ChipSelectPin));
  {
    lcd.print("SDCard wurde erkannt");
    Serial.println("Die SD-Karte wurde erkannt");
    delay(1);
  }
  if(!SD.begin(SD_ChipSelectPin));
  {
    lcd.print("SDCard wurde nicht erkannt");
    Serial.println("Die SD-Karte wurde nicht erkannt");
    SD.open("sderkannt.txt");
    delay(1);
  }
}

void loop()
{
  counter ++;
 
  if(lock == 0)
  {
    bs1 = 0;
    bs2 = 0;
    pinMode(buttonPin1, INPUT_PULLUP);
    pinMode(buttonPin2, INPUT_PULLUP);
    bs1 = digitalRead(buttonPin1);
    bs2 = digitalRead(buttonPin2);
    lcd.clear ();
    lcd.backlight();
    lcd.setCursor(0,0);
    Serial.println("Reaktionstest");
    lcd.print("** Reaktions-Test **");
    lcd.setCursor(3,1);
    lcd.print("Drueck die L/R");
    lcd.setCursor(2,2);
    lcd.print("Taste, sobald du");
    lcd.setCursor(3,3);
    lcd.print("den Ton hoerst");
    delay(5000);
    lcd.clear ();
    lock = 1;
  }
  lcd.backlight();
 Serial.println("Mach dich bereit!");
 lcd.setCursor(7,0);
 lcd.print("Mach");
 delay(150);
 lcd.setCursor(7,1);
 lcd.print("dich");
 delay(150);
 lcd.setCursor(6,2);
 lcd.print("bereit!");
 delay(1000);
 lcd.clear ();
 randNumb = 0;
 randNumbLR = 0;
 randNumbLR = random(1000, 3000);
 randNumb = random(500,10000);
 Serial.println(randNumb);
 delay(randNumb);
 
 if(randNumbLR > 2000){
        times1=millis();
        Serial.println("rechts");
        tmrpcm.setVolume(7);
        tmrpcm.play("rechts.wav");
        timee1=millis();
        if (bs2 == LOW);{ //Here it should test if the button is pressed or not but it got triggered instanly.
         reaction = timee1-times1;
         Serial.println("Deine Reaktionszeit:");
         Serial.println(reaction);
         lcd.setCursor(0,0);
         lcd.print("Deine Reaktionszeit:");
         lcd.setCursor(4,2);
         lcd.print(counter);
         lcd.print(". Versuch:");
         lcd.setCursor(25,1);
         lcd.print(reaction);
         bs2 = 0;
        }
        }

 if(randNumbLR < 2000) {
        times2=millis();
        Serial.println("links");
        tmrpcm.setVolume(7);
        tmrpcm.play("links.wav");
        timee2=millis();
        if (bs1 == LOW);{
        reaction = timee2-times2;
         Serial.println("Deine Reaktionszeit:");
         Serial.println(reaction);
         lcd.setCursor(0,0);
         lcd.print("Deine Reaktionszeit:");
         lcd.setCursor(4,2);
         lcd.print(counter);
         lcd.print(". Versuch:");
         lcd.setCursor(25,1);
         lcd.print(reaction);
         bs1 = 0;
        }
}

     if(counter == 1)
     {
      bestTry = reaction;
     }
     
     if(reaction < bestTry)
     {
       bestTry = reaction;
     }

     average = (average * (counter - 1) + reaction) / counter;
     
     lcd.print(" ms"); 
     delay (3000);
     lcd.clear ();

     if(counter == 10)
     {
       lcd.setCursor(0,0);
       lcd.print("Bester Wert:");
       lcd.setCursor(13,0);
       lcd.print(bestTry);
       lcd.print(" ms");
       lcd.setCursor(0,2);
       lcd.print("Durchschnitt:");
       lcd.setCursor(13,2);
       lcd.print(average);
       lcd.print(" ms");
     
       counter = 0;
       bestTry = 0;
       average = 0;
       lock = 0;
       start = 0;

       delay (20000);
       lcd.clear ();
     }
     
     delay (500);
     
}

Hi, Does your LCD Display work, can you make it display "Hello World"?

I think we need to do some code to check button performance.

Do you have a DMM to measure circuit voltages?

Thanks.. Tom.. :)

Yes, the Display is working and i also tried the buttons in another program and they worked :confused:

Thanks

Hi,
So this code will also work.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

#define SD_ChipSelectPin 4

TMRpcm tmrpcm;

float times1;
float timee1;
float timee2;
float times2;
float reaction;

const int buttonPin1 = 2;
const int buttonPin2 = 3;

int randNumb;
int randNumbLR;
int bs1 = 0;
int bs2 = 0;
int lock = 0;
int counter = 0;
int bestTry = 0;
int average = 0;
int start = 0;

void setup()
{
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("========React1=======");
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("========React1=======");
  delay(1000);
}

void loop()
{
  bs1 = digitalRead(buttonPin1);
  bs2 = digitalRead(buttonPin2);
  lcd.clear ();
  Serial.print("bs1 ");
  Serial.print(bs1);
  Serial.print("  bs2 ");
  Serial.println(bs2);

  lcd.setCursor(0, 0);
  lcd.print("bs1");
  lcd.setCursor(5, 0);
  lcd.print(bs1);

  lcd.setCursor(0, 1);
  lcd.print("bs2");
  lcd.setCursor(5, 1);
  lcd.print(bs2);
  delay(100);
}

Tom… :slight_smile:

So i removed all the cables and i looked at the output via the Serial Output and i saw that its not the buttons, displays or sd card readers fault and that means its the Sketches fault :o

** Thank you for the Test program and with your Test Program, it works, that means that its 100% the fault of my code