How To Build a Flashlight With Perfect Modes (picture heavy)

Thank You for the files and the very fascinating soft-beacon function that You invented. One can almost feel the light beam sweeping by...

Your files worked right away, and mine also, after playing around with some editing - I never found out what the problem was.

About the mode shift:

I got an UltraFire WF501B with MC-E today and loved the mode shift, the mode is stored about half a second after turn-off. You don't have to think about starting a 2 sec period before You can switch to next level.

I think this is ideal in a light with only level shifts. If there are other functions, seldom used (Beacon etc.), perhaps it would be better to have no memory and start up in medium, followed by high - low1 - low2 - Beacon or whatever.

I wonder if there is time to write the eeprom during a brownout?. How do we address that?, Tido, agenthex, anyone?

Release v0.3 is ready for download, grab it here. Here is the changelog:

2010-12-09 Tido Klaassen <tido@4gh.eu>

* BLF-VLD: release v0.3

* driver.c: mode functions can now be en/disabled without rearranging
modeline offsets

* driver.c: EEPROM image configurations containing disabled mode
functions will result in compile time error

* driver.c: added support for battery monitoring and handling of low
battery voltage

* driver.c: added wear levelling to mode switching to protect EEPROM

* driver.c: added timer prescaling config option

* driver.c: added mode functions for SOS, alpine distress signal and
fading beacon

* README: added info on how to set up battery monitoring

It would take some modifications to the stock PCBs for this to work.

You could build a "simple" driver without the extended mode group and disable mode memory with the NOMEMORY #define.

This cannot be done with the brown-out detection. The BOD works by stopping the µC once Vcc drops below the configured threshold, and resetting it when the power comes back. The only difference to a normal start-up is a flag set in a status register. Besides, to use the BOD for switching modes, the buffer cap would have to be chosen with the exactly right value, which depends on things like power consumption of the µC and resistance of the battery monitoring circuit. (BTW, I did a calculation some time ago on how long a 100nF buffer cap would keep the µC running. The voltage divider alone would drain it below the µC's minimal operation voltage in about 2ms. I don't think it's possible to use BOD for mode switching on a stock 101-AK.)

The only way to get this working would be to add a really big cap (10-100µF), remove the voltage divider and add a wire from the battery to one input pin, bypassing the protective diode. This way an interrupt could be triggered when the light is switched off (battery voltage is zero, but µC can still run from the buffer cap). If the power comes back before the cap is drained, it was just a short tap. But this would take some fundamental changes to the driver software to get it working.

Hi Tido,

I think You just disencouraged me to dig further into this. But as I know myself I can't let it be.

I thought about using the Brown-out interrupt vector rather than polling the flag BORF. If I could find out how to catch Brown-out interrupt (I don't know much about this special processor) I would turn off the load and count 4 watch dog interrupts (1 sec) and then store the tap_none flag to the EEPROM. I thought that it could be that simple.

I went through my collection of lights with memory and found 7(6) that saves the mode after power-down and only 3(2) that does it after power-up:

Store mode after power-down:

TrustFires: EF23: 1 sec, F20: 0.5 sec, Z1: 1 sec

UltraFires: C3 SS: 10 sec, another C3 SS: 5 sec, 501B MC-E: 2sec

Skyray S-A1: 1 sec

Store mode after power-up:

Tank007 TK-566: 2 sec, TrustFire R5-A3: 2 sec, Romisen RC-C6: <1 sec but always go to next.

Now I should disassemble the first group and spy on the configuration, but the only thought of it......

I am not the only one that like store after power-down (=only one tap for next mode), this is from brted's review of UltraFire 501B-MC:

Pros:

  • Bright
  • Cheap
  • Mode memory works well <<
  • Nice Low

I'd be glad if you proved me wrong, as I also prefer this kind of mode switching. But the PCB needs to be designed for this and the available driver boards using an ATtiny just aren't.

Using an interrupt routine to trigger on the BOD is impossible, as there is no such interrupt. When Vcc drops below the threshold, the µC's internal reset line is pulled low and kept this way until Vcc reaches operating level again. BORF is there to inform the program that it is not starting up from a complete loss of power. This is useful, if there are peripheral components that need to be initialized or explicitly reset in such a situation.

That's too bad, but thanks anyway for clarifying thing to me

Could'nt we just use a diode from plus on ATtiny (pin8) to a capacitor to ground, then a few hundred kiloOhms from the capacitor to an unused input pin, fx. PB3 (pin2) . Then at power-up, if the pin reads as '1' there was a short tap and if '0' there was a longer pause.

Sure, that would work, if it takes longer to charge the empty capacitor to logic high level than the program needs for starting up and reading the pin. Or we could charge the capacitor through the pin while the light is on and read the state on start up. I just measured the resistance of the I/O pins when the chip is powered down and they are tri-stated, so there would be no discharging through the ATtiny.

Problem is, this would require altering the PCB and a significant rewrite of the driver software.

Yeah, something strikes me!. Do you remember my problems with the Manafont BLF? I had unsoldered two components. A diode was going from pin 2 in parallel with 100 kOhm, they were connected to the capacitor at the other end of the ATtiny and nowhere else.

I think that those components are just what we are talking about!!!!

Yes, the capacitor could be rapidly charged across the diode and slowly drained via the resistor. Although I don't see how the capacitor would lose its charge in an exactly defined time if the ATtiny was switched off.

It is a small capacitor, perhaps 100 nF, and there will always be a (very) small leak current into the powered-off ATtiny port. About accuracy, look at the different timings on my lights a few post's backwards, there are two 'identical' C3-SS, one with 5 sec and one with 10 sec untill storing the mode. They properly have equal circuits.

I can see no other use for this circuit than providing this storing-timing. I really think we spotted 'how they do it'.

According to the datasheet, there is a maximum leak current of 1µA into a tri-stated I/O pin at 5.5V, which equals a minimal resistance of 5.5MΩ. I measured ~12MΩ on one of my chips. A high level is >= 0.6 * Vcc, so if we take these two values as lower and upper bounds, and assume the capacitor is 100nF, this circuit would register taps from 0.3s to 0.6s in length. That sounds plausible.

I expect this could work with just the capacitor alone. This would be the poor mans version, but more easy to implement in hardware. The AK-47 already have solder pads ready for this, connected to pin 2 (look at the left end of ATtiny). Pin 2 is also connected to the first star underneath, where it is also easy to mount a capacitor. On the NANJG 106 (from KD, 8x7135), the first star is likewise connected to pin 2 and second star to pin 3, so here it is also easy to add the condenser. I expect the same possibility on 101-AK, but my last DX order on theese gave me AK-47C instead with PIC uC.

In SW, could'nt we just omit the WD counting to 8 and instead read pin 2 at start-up?

It would ne nice to have two versions (#define ?), one (the present version) with power-up-memory and one with power-down-memory for those that have an already equipped driver (as the BLF-light) or don't mind adding a capacitor.

I tried a similar circuit (470kΩ and a diode in parallel to a 1µF capacitor) on my breadboard model this morning. It works reliably for detecting short clicks. Removing the diode breaks the circuit, as the I/O pins seems to lose the high impedance state during start-up. With just the resistor it either takes much too long to charge the cap or it is drained before the program has a chance to test it, depending on the resistor used.

Adding this kind of mode switching to the program shouldn't be too hard, although it already contains far too man #ifdef-ed code blocks for my taste.

*Sigh* just when I thought I could go job hunting without any more distractions from this project, you come along and hand me an interesting problem...

I think that interesting problems is just why we have this forum , good hunting! - eventually...

Mode memory after power-down.

I now have the power-off memory working in my BLF AA-Y4E with the original components (a diode in parallel with 100 kOhm going from pin 2 (port B.3) to a capacitor looking like a 100nF). All taps shorter than 1 sec shifts to next mode, longer taps turn on in the same mode. It seems to work fine and an oscilloscope show that the condensers voltage fall to half value in about a second. The coding took only a few lines and the omission of watch dog counting as this got obsolete. I have used Tido's program version 2 for the testing.

Next, I will try to implement similar function on AK-47, 101-AK, NANJG 106 with the fewest possible extra components and report back here.

Yes, it can be done in a few lines of code. The hard part is not getting it to work, but not to break other configurations in the process. The real head scratcher is how to implement the mode programming. With the cap-switching, there is only one type of tap. We could fall back to mem-switching just for the acknowledging sequence, but memory is so tight that there would be only enough space for a single mode function.

I see what you mean, my preference is for the time being just a simple, 3-4- or 5-level light. Spacing the levels with less than a factor of 4 is in my opinion overkill because of the logarithmic behaviour of the eye.

I focuses much on the memory after power-off because I find the opposite annoying: you often tap through levels to find the level that suits you, at least I do. If you are a little indecisive the next tap often does'nt work because the memory kicked in and the time window has to be re-started first.

This seldom happens with memory after power-off because you don't have to judge anything during the off period.

About my experiments, they confirm what you have found earlier, that a condenser alone get drained by the port during power-off, that calls for at least 3 components -too bad.

Okay, I've thought about the acknowledging sequence a bit and I think I've found a way to do it without falling back to mem-switching. We could keep the short-short-long-short sequence and abort programming once the light stays on for more than two seconds. This would effectively prevent any accidental re-programming and should be quite easy to implement. Although there will be yet another case of #ifdef...

This is probably the last feature I'll be adding to the driver, as it has already reached a state where it's really hard to read the code and see what's going on.

A WF502B-R5 was modded with version 3 of Tido's BLF-VLD program. I choosed to make a simple 4 mode light with low battery detector. The modes was layed out as 1.5%-6%-25%-100%, the result in (my own)lumens was 3.5-16-66-240.

The program now has battery low detection. When the battery voltage drops under about 3V the light will revert to lowest level and will blink every 5 sec's. This is a matter of taste, perhaps it should only blink as a warning, but then it could not be left alone without damaging a battery without protection. Perhaps the best would be that the light stepped down a step at a time (how about that, Tido?). This is easily done manually by just restarting in a lower mode, the detector will not cut during the first few sec's.<(Edited): The Low Bat function is now changed so that the output repeatedly will be cut to half every time the battery voltage fall to 3V. That way the output will be optimized to the remaining charge until the LED only glows. This works very nice.>



At the top a fourth 7135 is added to obtain 1.4 Amp. At the bottom, the wire connecting the chip to the memory circuit on the other side is visible. At the right a new, round diode - I used the original, smaller doide in the circuit underneath as the round one was too long. <(Edited): I have changed the diode back again! the original diode is a Shottky type to keep the chip running at low voltage.>



The 3 added components to make the memory_after_off (as in TR-801, EF23, Skyray S-A1, F20, C3 SS, Z1, WF501B-MC-E, ...). A diode is soldered 'piggy-back' on top of a resistor of 10MOhm (invisible in the picture), both soldered to a capacitor of 100nF to the outer ring. The isolator underneath is a piece of tape for painting. These components are not nesessary if you prefer a memory_after_on type (as in Tank-566, TrFi.R5-A3, WF502B-R5, ...). The PIN_SWITCH variable in the program should be changed accordingly.



The pill in place. In the tube two full rounds of Pepsi-Max alu-can is fitted for better cooling.

EDIT:
After a few broken capacitors, I changed the mounting method to a more rigid one. Here shown on a NANJG 105C which already (like the AK47) has a star that is connected to the MCU pin 2 so a wire is not needed. This makes the mounting of components and also a later reprogramming of the MCU easier.
I put a piece of Kapton tape besides the rightmost star to isolate the other stars and solder the capacitor to the rightmost star. Then from the free end of the capacitor I solder the resistor and the diode (cathode) to the outer ring (see picture). This circuit has exactly the same function as the former but is more mechanical stable.