Timer based code

Hi guys,
I have some firmware running, that I want to minimize power usage.

The code is monitoring a sensor, and then sending a notification when the sensor is activated. The core is powered by a battery, and I’m trying to maximize battery life. Right now I have 4 hours, I’d like to get it to 12 hours. That was the life when I first finished the firmware, without trying to conserve power.

My idea currently is to make the main loop basically not calculate anything, and an interrupt is used for the sensor pin. I have some calculations that go on also, but they only need to go on every 15 seconds or so.

I’d like to figure out how to tell the core to do the calculation every 15 seconds, instead of constantly checking the millis in the main loop to see if the 15 seconds has elapsed. Is there an easy way to do this? Should I put a “delay(15000)” in the main loop and call the calculations after? Will interrupts interrupt a delay?

Any suggestions about how to maximize lifespan, minimize power use?

Thanks,

J

PS, I have not experimented with putting the core to sleep, either, can I sleep the core and have it wake on interrupt?

1 Like

Going to sleep then waking up from a pin change interrupt is coming in the next firmware release!! :slight_smile:

The code is already in the master branch, so feel free to run :running: , grab that and build locally, then you’ll be able to use the new sleep functions that wakeup on interrupt!!

1 Like

Hi @jimbol,

  1. For your once-in-15-seconds calculation, it’s best to use a timer interrupt using https://github.com/pkourany/SparkIntervalTimer. This one’s a really nice library for that.

  2. You can do a sleep and wake-up on interrupt. That’s in the post above.

  3. In case you aren’t using the Spark Cloud for notifications in the meantime, you can disengage the connection to save a little power (http://docs.spark.io/firmware/#spark-disconnect). You can also control that better by using the Semi-automatic/Manual system mode. There’s good documentation of that in http://docs.spark.io/firmware/#advanced-system-modes.

I wouldn’t recommend switching the WiFi module on and off because it takes some time to connect.