Should input be defined as input_pullup ?
There are two ways to connect a button/switch to a pin.
1) Button between pin and 5volt (assuming 5volt Arduino).
Then you MUST add an external pull DOWN resistor (between pin and ground),
to prevent the pin from 'floating' when the button is NOT pushed (open switch).
A common value is 10k.
No setup() code needed, because a pin defaults to INPUT.
The pin is normally LOW (pulled low by the resistor), and HIGH when the button is pushed.
2) Button between pin and ground, with internal pull up enabled in pinMode.
pinMode(buttonPin, INPUT_PULLUP); // button between pin and ground, NO external resistors.
Inverted logic.
The pin is now normally HIGH, and LOW when the button is pushed.
Adjust the code for that.
In noisy electrical environments or with long wiring it could be needed to lower the pull up/down to 1k with an extra external resistor, and/or add a 10-100n cap from pin to ground.
Leo..