In short: I have an Input Capture code and a Power-Saving code combined into one program. Both are adapted from Nick Gammon’s examples that work fine independently. When I combined them into one code it only reads the data every other time a single pulse is sent to be captured every 20 seconds. The board sleeps during one idle cycle between the 20 second readings, then wakes up but misses the first reading, and then it stays awake during the next idle cycle and reads the pulse correctly at the 40 second mark for the second reading. And repeats. The references of these two Nick Gammon codes are listed in the program.
Details: Adafruit BlueFruit Feather 32u4 w/nrf51 in the Arduino 1.8.10 IDE. Using Nick Gammon’s example for Input Capture an external incoming pulse of around 800 us (micro sec) long pulse is being measured by (ICP3) Input Capture #3 (rewritten from ICP1) on Pin D13 (PC7) (removed the LED). Works good.
Then I had a separate program that puts the board into deep-sleep and using INT0 (D3) to wake it up with an external interrupt signal. Since I was not able to figure out how to use the Input Capture as an ISR Interrupt to wake up the board, I ran the same external pulse signal also to the D3 (INT0) pin. So when the pulse hits both D3 (INT0) and D13 (ICP3) at the same time, and D3 wakes up the board via INT0 and then the ICP3 can capture the pulse and measure it. Then right after that, or several seconds later, can go back to sleep until the next pulse arrives on D13 (ICP3). I spaced the external 800us single pulses 20 seconds apart to make it easier to diagnose. If I remove the line that places the board into sleep “sleep_cpu ();” I get a nice single-shot signal pulse measurement every 20 seconds. Since I am not using the USB-detach and reconnect feature, after the board goes to sleep it disconnects, but I am monitoring the power usage via a Fluke mA meter and I can see when it sleep and when it wakes up, and also monitoring the external pulse going to D13 (ICP3) and D3 (INT0) on the Oscope.
Notes: The Input Capture is listed under the Interrupts, but at a much lower priority then INTo or INT1. I do not need an active USB during the readings or sleep, because using BLE to send the data. The code I posted has the BLE portion removed the make it simpler to troubleshoot. The 800us external single-shot pulse is rock solid I have it showing on the Oscope, at the proper magnitude (0 to 3.3V) and intervals. I am trying to have the CPU sleep between readings. Can be right after the reading or a few seconds later. Interrupts have to be alive and ready to capture in case the 20 seconds becomes 1 or 2 seconds intervals (in real life).
1-InputCaptueCounter_andSleep.ino (8.43 KB)