while(Serial.available()==0) {} loop falls through

There is lots more code. I don't understand what part of the code you would need. This is a function, not a snipet. The entire program is not what you could call "open source".