LCD shows garbage, usual fixes won't work

Thanks dc42, I'll try to use a do...while loop to avoid potential problems with that.

I made some experiments and came to some results. As I said I'm using pins 8, 9, 12 and 13 for the buttons. I came to know that 12 and 13 are used in SPI.h, and 8 is the reset pin. When I took out the SPI.h library, buttons 12 and 13 stopped causing problems, and only 8 did it. So it probably has to do with those pins serving for some other things. Next I'll try running it with SPI.h but with all the buttons as analog inputs rather than digitals, to see if it sorts it out.

Thanks guys