Led and button simulation of a blink
The volatage level on the IO pin needs to change (from low to high and vice versa), only then can the microcontroller detect a button press. Without another connection to the button, IO2 is simply floating (unconnected).

There are two methods to implement this: external pull-up or external pull-down resistors and internal pull-up resistors (both methods are described for example in this tutorial).

I prefer the internal pull-up resitor method. You need to connect the second pin of the button to GND. Then change your setup code to

pinMode(buttonPin, INPUT_PULLUP);


If the button is not pressed, the internal pull up resistor inside the microcontroller pulls IO2 to +5V and a digitalRead(buttonPin) returns HIGH. When you press the button, IO2 is pulled to GND and digitalRead(buttonPin) returns LOW.

So in your case you need to check for a LOW reading, because you want the LEDs to blink when the button is pressed.

(Btw. you can replace the while loop with an if, since the loop() function is already executed in an infinite loop.)