Software delay

Is there an option to simplify code to achieve delay:
local t = hardware.millis();
while(hardware.millis() - t < 20) {
in Arduino environment it’s simple:

Imp.sleep(0.02) would sleep for 20ms

ha, i just came here to ask the same thing, thanks!


imp.sleep, I suppose, also stop or change PWM timing.
Example shown by Hugo is more close to mentioned above “delay” - just need a bit to change way of thinking about code sequence.
Anyway - a bit complicated…

can I use this to run 3.3v out of pin 8 for 2 seconds then run 0v out of pin 8



This will make pin 8 go high, then the imp will SLEEP for 2 seconds (meaning no other code can be processed during this time), then pin 8 will go low.

There are better ways to do that for such a long delay (doesn’t seem long, but a microprocessor can do a lot in 2 seconds!!). But yes, this will do what you want.

imp.sleep(2000) will make it sleep for 2000 seconds. This will result in the imp being marked offline; imp.sleep should not be used for anything but very short delays as it suspends network traffic processing (and the parameter is seconds, not milliseconds).

If you want to pulse pin 8 high for 2 seconds in a squirrel-friendly way, you’d do this:

imp.wakeup(2.0, function() { hardware.pin8.write(0); });

…which sets it high, and queues a task to run in 2 seconds time which sets it low again. Network traffic still gets processed, and your code also continues to run :slight_smile:

Oops…forgot those units are seconds and not ms!!

The imp does PWM in hardware – it’s not affected by imp.sleep().


@ peter, are you sure? Documentation - during imp.sleep() timers are blocked.

The timer still runs, but timer callbacks registered with imp.wakeup() are queued, not executed, if the timer expires while Squirrel is already running (including waiting in imp.sleep).