I do not know how to make the Arduino check two if statements

Hello, I'm very new, and I do not know how to make the Arduino to check two if statements. Here is the code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
int time1 = 0;
int time2 = 0;
int time3 = time2 - time1;

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
lcd.print("REACTION TEST");
lcd.setCursor(0, 1);
lcd.print("press to start");
}
void loop() {
switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
      reply = random(200, 4000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Press...");
      lcd.setCursor(0, 1);
      delay(reply);
      lcd.print("NOW");
      time1 = millis(); 
      switchState = prevSwitchState;
      return;
      if (switchState != prevSwitchState) {
        time2 = millis();
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Reaction Speed:");
        lcd.setCursor(0, 1);
        lcd.print(time3 + "ms");
       }
    }
  }

Please don't be harsh, this is my first time coding outside tutorials :slight_smile:

Your second "if" can never be executed because there's a "return" immediately before it.

Can you explain in non-programming terms what you want to do?

It looks like you want to alternate between a button press starting the reaction timer and the button press being the response to the reaction timer. You need a variable to keep track of which meaning the button has. How about calling it "TimerRunning to mean the test has started.
boolean TimerStarted = false;

Then, if the variable is 'false' you use the button to start a reaction timer test and if it is 'true' you use the button to end the reaction timer.

Your sketch is only looking for a change in the button input. That means it will react when the button is pressed AND when the button is released. You probably only want to react when the button is pressed:

switchState = digitalRead(switchPin);
  if (switchState == HIGH && prevSwitchState == LOW) {

Don't forget to set 'prevSwitchState' to 'switchState' at the bottom of loop().

Thank you for the help, I have changed the code like this, but nothing changed.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
boolean TimerStarted = false;
int time1 = 0;
int time2 = 0;
int time3 = time2 - time1;

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
lcd.print("REACTION TEST");
lcd.setCursor(0, 1);
lcd.print("press to start");
}
void loop() {
switchState = digitalRead(switchPin);
  if (TimerStarted == false && switchState != prevSwitchState) {
      reply = random(200, 4000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Press...");
      lcd.setCursor(0, 1);
      delay(reply);
      lcd.print("NOW");
      time1 = millis(); 
      TimerStarted = true;
      switchState = digitalRead(switchPin);
        if (TimerStarted == true && switchState == HIGH && prevSwitchState == LOW)  {
        time2 = millis();
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Reaction Speed:");
        lcd.setCursor(0, 1);
        lcd.print(time3 + "ms");
       }
    }
  switchState = prevSwitchState;
}

You have unreachable code because you test for TimerStarted == true inside a block that has already passed TimerStarted == false.

The principle of exclusion says, something cannot be both true and false.

That isn't doing what you seem to think. Because it is outside loop() or any other function it only does anything at compile time. So time3 is always 0. You need to specifically set time3 = time2 - time1 inside loop() just before you lcd.print() it. And print the "ms" separately. Adding a String to an integer isn't going to get you anything sensible.

Steve

I'd suggest before you start writing your code that you write a simple explanation of what you want the program to do; a "top down design" in "pseudocode".
eg
Display message
Start timer
if the switch is activated{
display the time}
...

Try this:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;

boolean TimerStarted = false;
unsigned long time1 = 0;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  
  pinMode(switchPin, INPUT);
  
  lcd.print("REACTION TEST");
  lcd.setCursor(0, 1);
  lcd.print("press to start");
}

void loop()
{
  switchState = digitalRead(switchPin);
  if (switchState == HIGH && prevSwitchState == LOW)
  {
    // Button was just pressed
    if (TimerStarted)
    {
      // The timer is running so this button press is
      // the end of the reaction test
      unsigned long reactionTime = millis() - time1;
      
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Reaction Speed:");
      lcd.setCursor(0, 1);
      lcd.print(reactionTime);
      lcd.print("ms");

      delay(5000);

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("REACTION TEST");
      lcd.setCursor(0, 1);
      lcd.print("press to start");
      TimerStarted = false;
    }
    else
    {
      // The timer is not yet running to this button
      // press is the start of the reaction timer
      unsigned long randomDelay = random(200, 4000);
      
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Press...");
      lcd.setCursor(0, 1);
      delay(randomDelay);
      lcd.print("NOW");
      time1 = millis();
      TimerStarted = true;
    }
  }
  switchState = prevSwitchState;
}