This project is the evolution of a previous micro controller flashlight that i built about 8 months previous.. The are completely different in hardware and firmware... just felt i should say something about its predecessor.
Originally this flashlight was going to be very small, sort of like the photon 3 (or similar) flashlights with processors built in. That original idea changed quite a bit before the final product was completed... I wanted to use red, green, and blue led's so that I could produce any color, and also efficiently produce white light... Which is what i anticipated using most (the flashlight is meant partly for hiking) and white light is always nice.. But because you have all 3 primary colors available.. I wanted to have the flashlight do some fun things also :) like scan through all the colors and some unique flashing patterns... I even incorporated sound output !
It is now roughly the size of an AA battery, and fits in the space meant for one.. In the upper portion of an AA Mini Mag flashlight, I removed the threads on the inside of the flashlight head, and filled in the male threads on the flashlight body with epoxy putty. I also made a groove on the front of the body to accept a set screw that is threaded into the head, to keep it in place, but still allow the head to spin freely. So now when you turn the head, a 24 detent rotary encoder is being turned.. It was really tricky to get that to work right.. And to fit the encoder in the space available.
The firmware is also very complex, it has to:
1. generate the PWM signal, which is on an interrupt
2. read the input devices; rotary encoder and momentary contact switch.
3. set the current mode, change settings accordingly
4. calculate the current PWM level of each LED, based on the hue, saturation, and brightness variables
There are many other functions that the firmware takes care of, but those are the most basic, that have to happen every cycle.
Brief explanation of the different modes and functions of switches:
1. To turn on the flashlight you hold down the mom switch for ~ 2 seconds.
2. The starting mode is always the "white_mode" as i call it... The encoder just varies the brightness, and the saturation is set at 255 (white)
3. If you hold down the mom switch for about 2 seconds (while in any mode) it will toggle between the white_mode and the normal mode
4. When in the normal mode, there are 4 different modes that can be entered by shortly pressing the mom switch
5. The first mode is Hue.. the hue is set to the encoder value
6. The second mode is Saturation 0 = full color saturation (no white) 255 = full white
7. The third is Brightness. Brightness basically lowers the color values calculated in the above two modes, at 255 they are just left alone
8. The fourth is a hue scan mode... the hue is just incremented, at a speed determined by the encoder value. Its nice for demonstrating the capabilities of the flashlight.
9. If at any time you want to turn off the flashlight, just hold down the mom switch for at least 4 seconds and it will be turned off.
There is also an advanced mode.. for changing some settings of the flashlight.. it also has some cool effects.. look at my source if you're interested in learning more about them, its heavily commented.