You can't write code that blocks waiting for input. It needs to use callbacks.
I'm not quite sure what you're trying to do, but this looks like it'd be an awful lot easier in JavaScript!
@Gordon started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
You can't write code that blocks waiting for input. It needs to use callbacks.
I'm not quite sure what you're trying to do, but this looks like it'd be an awful lot easier in JavaScript!