Code for button press detection

In what way does it not work ?
Have you tried putting some Serial.prints in there to see what the value of y is at various points ?