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

Sure. There are 5 usable pins for input or output, PWM control uses one of them. You could wire the mode switch to any other pin and write a firmware to react to that.

The driver of my previous post which I modified the battery monitor to a temperature monitor is configured as a 3 level driver, High 0x08, Med 0x07 and Low 0x05. The battery monitor is configured as below;

// Config for battery monitoring
#define MONITOR_BAT // enable battery monitoring
#define LOWBAT_TRIG 130 // trigger level for low battery, see README
//#define LOWBAT_RAMPDOWN // decrease output gradually when battery fails
#define LOWBAT_MIN_LVL 0x07 // minimal PWM level to use in low battery situation
#define LOWBAT_MAX_LVL 0x08 // maximum PWM level to start ramping down from
#define ADC_MUX 0x01 // ADC channel to use, see README
#define ADC_DIDR ADC1D // digital input to disable, see README
#define ADC_PRSCL 0x06 // ADC prescaler of 64

The outcome is that the light level is switched to Low instead of staying at Med when the voltage of the voltage divider is smaller or equal to the threshold. Would like to ask someone to help me to fix the issue. Thanks.

LOWBAT_MIN_LVL and LOWBAT_MAX_LVL are PWM levels, not mode numbers. If LOWBAT_MAX_LVL is 8, then low-batt mode starts at 8/255=3% which is quite low already.

I'd try:

#define LOWBAT_RAMPDOWN // decrease output gradually when battery fails <--- on
#define LOWBAT_MAX_LVL 0x80 // maximum PWM level to start ramping down from <--- start lowbatt mode at half brightness
#define LOWBAT_MIN_LVL 0x02 // minimal PWM level to use in low battery situation <--- gradually ramping down to very low

Thanks again,

I see what you meant. I have confused with the mode number and the PWM level.

I am finally able to flash my drivers. Many thanks to Tido for supplying the tools and info to make this possible and thanks to Jonnyc for walking me through the problem I have been stuck on for a couple months.

Just signed up to say thank you for a excellent thread. I've successfully flashed tido's files, working on compiling so i can modify and build files which will allow me to create the perfect (for me!) driver. It's not easy as computers and me dont really get along but it's interesting and great to learn about something I've been curious about for a while. . BTW the last time i did any programming was in the 1980's with a computer that had 1k of memory how crazy was that!

Now your flashlight (with an Attiny13) has 1K of flash memory! Though, I bet it's a little smaller than the computers you were using then.

Kris

Hi Major,

Welcome to BLF!

Your comments bring back memories, this thread makes me think about trying my hand at programming again. My first attempts were in BASIC on a TRS-80 Model 1, back around that time too. Geez, seems like a while back.

Glad to see so much interest, and so many "mature" users in our ranks. 8-)

later,




Update! So i am able to compile and flash tido's files, incase it helps someone else who's just starting out i had the wrong filename in the makefile (doh!) also i had the chip as a tiny13a switching to tiny13 allowed avrdude and my cheapo ebay $6 programmer to work correctly. Nice moment when it all worked. I'm getting there but am having trouble with the #define's! I would like no memory, programmable, pin switch mode switching, but i'm not sure the mode setting when off for 1second will work with programmable modes, which set when on for a few seconds, please let me know if you've already tried and it does/doesnt work thanks. Better go now, work tomorrow,yawn. Ta

For no mode memory, uncomment the "#define NOMEMORY". Mode programming works with both switching methods. Of course, pin-state based click detection only works if you have added the external capacitor/diode/resistor circuitry.

Hello, everyone.

First of all, thanks for the excellent work you're doing here!

I'm a bit desperate. I'm new to all this stuff (although I've already built many bike lights it never involved programming). I was able to understand most of critical parts of the code, I succeeded in compiling it and making a 2-mode version with no memory. But now I'm really stuck (despite several tons of information I had to learn and process).

I need to switch modes with a momentary pushbutton without interrupting power to the driver. As I understand from the schematics I've seen, the button should short-circuit ground (Pin 4) and some other pin (Pin 3, for example). It seems it has something to deal with the Pin Change Interrupt PCINT0, but, alas, I'm not a coder and I've never used C++ before (only wrote some elementary BASIC stuff many years ago and have already forgotten it all...). Can you just, please, throw some code at me I could use? I'll be most grateful if you also point me on how to tell a short press from a 3sec press and hold.

Thanks in advance.

Hi everybody,

This is seemed off topic. Since I have tried Tido's wonderful program in July, three switches are worn out.

The 2 pieces on the left which I bought from DX only survive for 5 days. The right one which I bought in August finally the power on lock was worn out yesterday. Suppose I switch it 100 cycles everyday, 100 x 30 x 4 = 12,000 cycles. However, the switch is rated 200,000 cycles. Did you guys also have the same problem?

@stronge: That would require a substantial rewrite, as it has to react on an additional switch instead of being switched off. When I get my TK35 clone, I'll probably mod it to a parallel setup with an NANJG105C and then I need to do exactly that. I can post my code then.

@Microa: I doubt it's the firmware's fault... I actually never had to replace a switch yet.

@DrJones: I replaced with a new switch and the light works perfectly well. I think that it would be better to use a on/off switch to control the power, another momentary pushbutton to change mode. The Con is we have to modify the code and the driver board. The clicky switch seems to wear out quickly while changing mode in fast half press.

new here, and only half new to electronics ;) after some analog pwm issues, i think i need to step into the world of micro controllers.

what do i need to add to get a attiny13a to work for the bread board like in your picture above?

edit-- didn't read on ahead to see that this is a 9-page thread. i'll do some more reading in case it answers my questions.

To run the ATtiny:

  • the solderless breadboard
  • the µC
  • some kind of 1.8-5.5V power source

To give it a possibility for some output:

  • a LED and a resistor, like in the image
  • lots of other things are available of course

To give it some input:

  • a switch (like in the image)
  • lots of other options (e.g. sensors) here, too

To program it:

  • a programmer (like USBasp)
  • some wires to connect the µC to the programmer
  • Software on your PC (Compiler, IDE, programmer utility)

Just got the usb programmer module and installed AVR studio. Got the general idea. I'm stuck however in identifiying pins on the programmer, ribbon cable.

I plan on going without the SOIC clip and use clips to the appropriate legs. I cannot however figure out the pin numbering.

which one is pin 1?

Or better with the stock ribbon cable on you get this:

From wiki. Which wire is what?

Upon idnetifiying those does the results relate to?:

That's all. :)

I'll really appreciate if someone is kind enough to solve the pin numbering mystery to me. :)

Pin Function Function Pin
1 MOSI VCC 2
3 NC GND 4
5 RST GND 6
7 SCK GND 8
9 MISO GND

10

So on t he 5-8 side of the clip you only use 2 wires 5 to RST and 7 to SCK on the 1-4 side use all 4 wires 1to MOSI, 2 to VSS, 3 to NC and 4 to GND............ clear as mud.

SOCKET PIN 1 MOSI --> IC PIN 5 _________ SOCKET PIN 2 VCC --> IC PIN 8

PIN 3 __I I PIN 4 GND --> IC PIN 4

PIN 5 RST --> IC PIN 1 I I

PIN 7 SCK --> IC PIN 7 I__ I

PIN 9 MISO --> IC PIN 6 I I

_________