button fails

mackintoshli:
after 4 or 5 seconds it automatically exits the while loop.

For that to happen, digitalRead() must be returning a non-zero value. Have you got an external pull-down resister on pin 10? If not, presumably the pin is floating (not connected to anything) when the switch is open - in this case reading the pin state will return arbitrary values. The solution to this is to use a pull-up or pull-down resister.

The Arduino provides an internal pull-up resister which you could use for this, but to use that you'd need to reverse the polarity of this pin so that it is normally held high by the pull-up resister and is pulled low by the switch. You'd have to change "while(digitalRead(button1) == 0)" too so that it waits for LOW rather than HIGH.