Step 4: Firmware Functions
Regulate Joule Thief output voltage (microcontroller supply voltage)
Firmware monitors the supply voltage by using the A/D converter - instead of connecting one of the input pins to the supply voltage, A/D converter samples built in fixed reference voltage using the supply voltage as the A/D reference voltage. The result is the inverse number of the supply voltage. The A/D result works out to be: 512/supply_voltage. Comparing this number against predetermined threshold to turn on/off the Joule Thief circuit effectively regulates the supply voltage. This voltage is set slightly below the Zener diode voltage of 5.1V to avoid letting the Zener diode shunt the voltage - wasting energy.
Check If the Button Switch is Pushed
The firmware also routinely check to see if the button is pushed. Since the button switch is multiplexed with the pin that controls the Joule Thief, the job is a bit tricky.
The firmware first changes the pin from output to input mode. Which means that the pin becomes high impedance. Put it another way, Joule Thief circuit gets turned off. At this point, the pin voltage should go low, unless the button switch is closed. So the firmware simply reads the pin status, and turns the pin back to output mode. This happens roughly every 8 milliseconds or 125 times/second.
Now the button state is checked against the timer to see if it's held long enough. If too short, the button push is ignored as a noise. If the push is long enough, a counter is incremented to check for single or double taps. Firmware waits for a predetermined period for another button tap. When it times out, an event is created as to single/double tap, or button held down.
Animate the LEDs
Now the most important function of all - animate the LEDs! I use the word animate because Wave JT doesn't just "blink" the LEDs, but changes LED brightness smoothly up and down in full 8 bit levels.
The animation engine works in a parametric manner. It takes a few parameters to specify; base update rate (speed), the direction of the movement (left/right), threshold level that one LED triggers the adjacent LED to start changing brightness level among other things. By changing these parameters you can create endless patterns of animation.