Reflex game, switch on random LEDs

Hello,

I am working on an Arduino project. I am making a reflex game.
To play you need to active the start button and then one of the two LEDs on the board will switch on and you need to switch it off with the corresponding button.
However, only one LED is switching on...
Can you tell me were is the problem? Thank you.
(Also, I'm sorry for the bad english, I'm an exchange student)

const int switchPin1 = 10;
const int switchPin2 = 8;
int switchStart = 12;
int ledPin = 11 ;
int ledPinP1 = 9 ;
int ledPinP2 = 7 ;
int buttonStateP1 = 0;
int buttonStateP2 = 0;
int lastButtonStateP1 = 0;
int lastButtonStateP2 = 0;
int scoreP1 = 0;
int randomPin;
int pins[] = {9,7};

boolean gameOn = false;
boolean startButtonState = LOW;
boolean p1Done = false;
boolean welcomeMsg = false;

long randomTime;
long startTime;
long endTimeP1;

float finalTimeP1;
float winningTime;

void setup()
{
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPinP1, OUTPUT);
  pinMode(ledPinP2, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (welcomeMsg == false){
    Serial.println(" ");
    Serial.println("Press the start button to begin");
    welcomeMsg = true;
  } 
  startButtonState = digitalRead(switchStart);
  if (startButtonState == HIGH && gameOn == false){
    Random();
  }
}

void Random(){
  Serial.println(" ");
  Serial.println("Get ready!");
  randomTime = random(4,10);
  randomTime = randomTime*1000;
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(randomTime);
  startGame();
}

void startGame(){
  gameOn = true;
  startTime = millis();
  randomPin = pins[random(0,1)];
  digitalWrite(randomPin, HIGH);

  if(digitalRead(pins[0]) == HIGH){
  while(pins[0] == HIGH || p1Done == false){

      buttonStateP1 = digitalRead(switchPin1);
      if (buttonStateP1 == HIGH && p1Done == false) {
        endTimeP1 = millis();
        p1Done = true;     
      }

      buttonStateP2 = digitalRead(switchPin2);
      if (buttonStateP2 == HIGH && p1Done == false) {
        p1Done = false;  
      }
    
  }
  }

  if(digitalRead(pins[1]) == HIGH){
  while(pins[1] == HIGH || p1Done == false){

      buttonStateP1 = digitalRead(switchPin1);
      if (buttonStateP1 == HIGH && p1Done == false) {
        p1Done = false;     
      }

      buttonStateP2 = digitalRead(switchPin2);
      if (buttonStateP2 == HIGH && p1Done == false) {
        endTimeP1 = millis();
        p1Done = true;  
      }
    
  }
  }
  
  endGame();
}

void endGame(){
  digitalWrite(ledPin, LOW);
  finalTimeP1 = (endTimeP1 - startTime);
  Serial.print("P1 time:"); 
  Serial.println(finalTimeP1/1000);
  winningTime = (finalTimeP1 + winningTime);
  Serial.println(winningTime/1000);
  digitalWrite(ledPinP1, LOW);
  digitalWrite(ledPinP2, LOW);
  delay(randomTime/2);
  
  buttonStateP1 = 0;
  buttonStateP2 = 0; 
  lastButtonStateP1 = 0; 
  lastButtonStateP2 = 0; 
  gameOn = false;
  startButtonState = LOW;
  p1Done = false;
  randomTime = 0;
  startTime = 0;
  endTimeP1 = 0;
  finalTimeP1 = 0;
  welcomeMsg = false;
}

However, only one LED is switching on...
Can you tell me were is the problem? Thank you.

randomPin = pins[random(0,1)];
  digitalWrite(randomPin, HIGH);

Please review the documentation for random() in the reference random() - Arduino Reference

The return value is exclusive of the upper bound of the function input parameters.

Use

randomPin = pins[random(0,2)];
  digitalWrite(randomPin, HIGH);

oh thank you !

You should use the concept of array's more. you have an array pins but that's just a copy op ledPinP1 and ledPinP2. And they need to be in order to make it work. But why not always use the array and drop ledpInP1 and ledPinP2? And even better, do the same for the button portion of the code.

See Gammons Tip 1 :wink:

A rule of thumb, if you start to write out something multiple times but slightly different (line 67 to 82 and 84 to 99) there is an easier way of coding it. And here you can make life a lot easier by using an array :wink: