How is the button wired ?
Do you have a resistor in place to keep it in a known state at all times or is the input floating at an uncertain voltage ?
If you do not have a resistor in place then try
pinMode(buttonPin, INPUT_PULLUP);
to turn on the built in pullup resistor and wire the switch to take the pin to GND when the button is pressed.