If I tap on my button the Serial Monitor tells that for about 13s

Hey, I just startet with programming Arduino and I am very confused because I'm not even able to make the Serial Monitor print that I am tapping my button.
Instead of writing that as long as I am pushing it, it tells that for about 13s after I stopped.
This is my code:

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  while (digitalRead(A0) == HIGH){
    Serial.println("S1 pushed");
    Serial.println(millis());
    digitalWrite(13, HIGH);
  }
}

The Serial Monitor looks like that after i tapped on the button:

Welcome to the forum

How is the button on pin A0 wired ?

It looks like you are testing whether it is connected to 5V to determine that it is pressed but is there a resistor in place to keep it at GND (LOW) when it is not pressed or is it floating at an unknown voltage, maybe HIGH, maybe LOW, maybe changing ?

Consider using INPUT_PULLUP in pinMode() for the button pin to turn on the built in pullup resistor, wire the button to take the pin LOW when the button is pressed and test for LOW to know when the button is pressed

millis() gives you the time since the sketch started.

So if you press the button after 13 seconds, you will get the value that you see and it will increase. If you don't release the button, that time will increase.

See further @UKHeliBob's answer about the INPUT_PULLUP and the wiring of the button; you can do a search on the web for 'floating inputs'.

Hey, thanks for your answer.
My button is connected to 5V on one side and to A0 on the other.

If I understood your advice right I should put a resistor in between but both the regular 220Ω resistor and the INPUT_PULLUP resistor didn't solve the problem.

@sterretje it starts with
S1 pushed
99
(I didn't have enough space on that screenshot)
and i just quickly tapped on it

So connect it to Gnd instead of 5V and add INPUT_PULLUP in pinMode

It sounds like you didn't

My suggestion was to use INPUT_PULLUP and wire your pushbutton as in S3 in the following diagram (Originator @LarryD)

Thank you, that solved it! I didn't change 5V to GND before :joy:
I honestly don't know why the INPUT_PULLUP is needed but I'm glad it works

That is the reason

You mean it stays on a random voltage lvl when i leave the button?
That is why the program still recognizes an input for a while.
And the INPUT_PULLUP resistor in contrast immediatly pulls it back down.

You got it

If a button used as an input is not held at a specific voltage level then it is said to be "floating" and will pick up electrical noise from its environment so will be at an unknown level