Following yesterday’s trial, here is the code which uses the pin-change interrupt to run in a continuous cycle:
The main loop is empty, since everything now runs on interrupts. The output signal is the same, so it’s working.
Could we somehow increase the cycle frequency, to get a higher time resolution? Sure we can!
The above code discharges the cap to 0V, but if we were to discharge it a bit less, it’d reach that 1.66V “1″ level more quickly. And sure enough, changing the “50″ loop constant to “10″ increase the rate to 500 Hz, a 2 ms cycle:
As you can see, the cap is no longer being discharged all the way to 0V. A shorter discharge cycle than this is not practical however, since the voltage does need to drop to a definite “0″ level for this whole “cap trick” to work.
So how do we make this consume less power? Piece of cake: turn the radio off and go to sleep in the main loop!
The reason this works, is that the whole setup continuously generates (and processes) pin-change interrupts. As a result, this JeeNode SMD now draws about 0.23 mA and wakes up every 2 ms using nothing more than a single 0.1 µF cap tied to an I/O pin. Mission accomplished – let’s declare (a small) victory!
PS. Exercise for the reader: you could also use this trick to create a simple capacitance meter :)