How to break out of a loop when data arrives on and InputPort?

Hey guys,

I’m a bit of a newbie, so bear with me.

I have my imp controlling an RGB LED with PWM. The LED can morph between different colors.

You program it using a form on a webpage where you can input new colors to morph to. The morphing happens inside of a “while(1){…}” loop, so it repeats on and on forever.

The problem is that when a new message arrives from the web form over the input port, the imp is stuck inside of its color-morphing loop. How do I get it to break out of the loop when a new message comes in from the web?


The answer is, you can’t… you shouldn’t have a while(1) loop for your morphing.

You should instead use wakeup to call your morph routine on every loop, eg:

`function morph() {
imp.wakeup(0.02, morph);
// set leds here

// Start callbacks at 50Hz

Every time morph runs, it re-queues itself to run 20ms later. As control falls out of the loop and back to the OS, TCP data gets processed and incoming messages can be dealt with.

Awesome, thanks!