The sketch does not work because you are working on an Arduino mega 2560, and you are facing the "!!!" Problem. So the upload hang. Remove one ! Or update you bootloader And it should be ok.
Also if you work in auto mode you should put a delay of lower then 120 seconde time out other ways your browser will just wait for 2 minutes before showing any thing. But I agree the sketch stink. ![]()