Can't get Uno to digitalWrite to randomly selected pin.

Hello, I am new here and I have searched the arduino webpage for a solution to this problem and I have yet to find it, so here I am.

In my code I have an array with the numbers 3-9 in it. My code will shuffle the array so that these numbers are not in order. i also have a push button on digital pin 2. What I want to happen is that when I push the button, the program will look at what value is in array[0] and write high to that pin value. I also have leds wired up to every pin, 3-9. This way I can see which pin is activated.

void loop() {
if (digitalRead(2) == HIGH) { //Looks if pin 2 equals HIGH
int ButtonPress = Array[0]; //Sets ButtonPress to value in array[0]
pinMode(ButtonPress,OUTPUT); //Sets that pin number to output
digitalWrite(ButtonPress,HIGH); //That randomly selected pin number goes HIGH
}

else {
pinMode(array[0], INPUT): //After I stop pushing button, it changes that pin to input. so its not always on.

}
}

After running the Code my array shows these numbers:
array[0] = 5
array[1] = 4
array[2] = 7
array[3] = 9

Here is my problem. When i push the button and it reads the value in array[0] it should activate pin number 5 and set its output to HIGH. But when I push the button, more than one pin activates. Sometimes I have 2-4 leds light up. Does anyone have a solution to this problem? I can post the rest of the code when I get to the house tonight if anyone needs it. Thank you for your time.

In my code I have an array with the numbers 3-9 in it

We’ll just have to take your word for that.

Please post your code.
Use code tags.

int ButtonArray[7] = {3,4,5,6,7,8,9};


void setup()
{

  Serial.begin(9600);
  Serial.println("Start ");

  pinMode(2,INPUT);

  // fill array with values
  for (int i=0; i<8; i++)
  {
    ButtonArray[i] = i;
    
      //if (i==0) {
       //i= i + random(1,7); 
      //}
  }

  // fetch 10 random numbers
  for (int i=0; i<7; ++i)
  {
    int r = random(2, 7-i);    // select from a decreasing set
    Serial.print(ButtonArray[r]);
    Serial.print('\t');
    swap(r, 7-i);             // switch the chosen one with the last of the selection set.
  }
  
   Serial.println("\ndone");
}


void swap(int a, int b)
{
  int t = ButtonArray[a];
  ButtonArray[a] = ButtonArray[b];
  ButtonArray[b] = t;
}


void loop()
{
  
  if (digitalRead(2) == HIGH) {
    
    int ButtonPress = ButtonArray[0];
    pinMode(ButtonPress,OUTPUT);
    digitalWrite(ButtonPress,HIGH);
  }

  else{
    //waiting for Input
    pinMode(ButtonArray[0],INPUT);
 
  }


}

Ok here is my code, after a little bit of modification I came out with new values but same thing is still happening:

ButtonArray[0] = 4
ButtonArray[1] = 3
ButtonArray[2] = 7
ButtonArray[3] = 2
ButtonArray[4] = 5
ButtonArray[5] = 6
ButtonArray[6] = 6

When I push the button on pin 2, it should write HIGH to pin 4. but it is not.

int ButtonArray[7] = {3,4,5,6,7,8,9};``  for (int i=0; i<8; i++)
How many?

  for (int i=0; i<7; i++)

Sorry, should have changed that earlier. I’m new to arduino and programming in general so bare with me here. The array should have 7 values in it, 3,4,5,6,7,8,9. These values represent the digital pins. I used some code found to shuffle the array so that these numbers would be in a different order. When the button on pin 2 is pressed, the code should look at the first value in the array. then set that value to and int ButtonPress. ButtonPress should then be a number somewhere between 3-9. It should then select that pin and set its output to HIGH.

so bare with me

Never, ever, on a first date.

Fix the really obvious stuff, retest, report back.
Debug prints are useful.

Your problem is here:

if (digitalRead(2) == HIGH) {

The loop function runs over and over and since it isn't doing much it will turn over and over really fast. It will run that code several times over before you can get your finger off the button. It reads the pin is HIGH, lights up a pin, repeats and sees that pin2 is still HIGH, lights up another pin, repeats and sees that pin2 is still HIGH and lights up another pin, etc etc.

What you should do is to react to the button when it first becomes pressed and not anytime it is pressed. That will involve remembering what state the pin was in the last time it was checked. See the "State Change" example for some inspiration.

Thank you for the reply. I will give this a shot over the next couple and days and report back.