Variable flickering LED

Hey everyone,

So this is a really simple idea, but I can't seem to get it just right. I'm attempting to make a program that will cause an LED to flicker using PWM, but I want it to flicker more intensely when a button is pressed. This is what I have so far. It does work, the only issue I'm having is a delay.

What it does is the LED will slicker dimly, but if I connect a wire between pin 6 and 12, it will flicker much brighter. The problem is that when I disconnect the wire between pin 6 and 12, it will still flicker brightly for a second or two. I want to make it so that once I disconnect it, it immediately goes back to the original dim flickering.

void setup(){
  
pinMode(5,OUTPUT);
pinMode(12,OUTPUT);
pinMode(6,INPUT);
digitalWrite(6,LOW);
digitalWrite(12,HIGH);
}
void test2(){ // function when pins not connected
  analogWrite(5, (random(1,5)));
  digitalWrite(6,LOW);
}


  void test(){ // function when pins are connected
  analogWrite(5, (random(100,150)));
  digitalWrite(6,LOW);
}

void loop()
{
  if (digitalRead(6) == LOW) // test condition
  test2();
 
 else // if condition isn't met
 test();
 
 delay(50);
 
}

Thank you for your help!

Not sure what you're doing with 6.
You set it up as an input, with internal pullup resistor, and then you turn the internal pullup off by writing 6 Low.
Eventually it floats to some level and is seen as having changed state.

Perhaps you meant to write 12 high & low to be read as the 6 input?

I don't believe so. What I meant to do was have a high voltage going to pin 12, and establish 6 as a low input. I wanted it to be read as high when the high voltage pin 12 is connected to pin 6

Call 6 an input then, which you did, and put a pulldown resistor on it.
Do not write to pin 6.

Or, change your logic - HIGH is the non-connected state, drive pin 12 low, and treat low as the connected state.
Do not write to pin 6. Only write to pin 12.

Naming the pin numbers just might give us a clue what is attached where on the Arduino, so we have some idea how to help you.