odd behivor of loop function

  1. Serial.available() returns the number of characters already in the serial input buffer. It doesn't wait for characters to arrive on the serial interface or anything like that.

  2. The Serial.flush() is completely unnecessary. It waits till all characters in the outgoing buffer are sent. As you haven't sent anything, it does nothing.

  3. Don't use a delay() if it's not necessary.

  4. Don't use the String class, it clobbers your memory and the current IDE has a bug that produces a memory leak.