Problems with calibration.

PaulS:
This appears to be trying to set the state of an input pin. Why would you be doing that? More importantly, why is an output pin called buttonPin? Switches are NOT output devices.

Internal pullup resistor perhaps?