System.sleep(10) sends my electron into a black hole

I am experimenting with reducing noise levels on reading an analogue input. Currently I am getting a few bits of noise (readings vary by about 5 bits) even when I connect an AD input to a low impedance stable source like a 1.5V cell (for testing).

I thought I would try reducing RF noise by turning off the cellular modem before taking the reading. So I tried including the following in the loop() function:

// Turn off cellular for seconds.
// Keep application running.
// Low power usage.
System.sleep(seconds);
…as copied from the documentation (with seconds set to 10).

This causes the electron to loose connectivity over cellular and breathe what looks like white. I can see it is still running the app and have just tested it with the basic BlinkAnLed app. But I can no longer access the device e.g. using the diagnostics.

OK I realize I was being rather naive/dumb. By running in a loop lasting a few seconds (i.e. flashing an LED in the test scenario) and calling System.sleep(10) once per loop the device is attempting to reconnect after the time has elapsed (10s in this case) but failing to connect before the next System.sleep(10) call is made. Presumably this is causing all kinds of problems…

If I include a long delay in the loop, then the device starts to behave better as it has time to reconnect before the end of the loop.

One strange and confusing side-effect in the original scenario (without the added delay) is that when I try and update the application code OTA the Web IDE keeps reporting “Flash successful” when clearly it is not.

2 Likes