I'm willing to pay $200 (not commercial)

It also doesn’t show where the PWM outputs go. A FET? Banks of linear drivers?

It goes to a buck driver. The driver works and is a separate circuit so I didn’t include it in the schematics. Assume the PWM output goes to a functioning driver.

The switch is a clicky/on/off type where a half-press simply opens the circuit. True no off time capacitor exists because I’m unsure how to integrate one and make the calculations for the firmware. This is why I’m asking for help :wink:

So what do you have for firmware right now? Start with something simple that turns the output pin on and off every 200 ms, then add PWM and get it to drive the output at quarter / half duty cycle alternating. Can you get that far?

Well, you wrote you where troubleshooting an already designed driver, trying to get the firmware to work. As you designed the driver, how did you design the driver to work in terms of mode changing?

#treellama
Yes, I can get an appropriate PWM setting. Just can’t figure out how to get the modes to change.

#Mike C
The driver is the part that regulates power to the LED. The microcontroller and it’s firmware sends the signal to control the driver. The driver is designed. I need assistance with the microcontroller and it’s firmware.

If I put in a signal to the driver, the driver does what it’s told. I’m not sure how to get the clicky button to tell the microcontroller how to change modes.

I’ve read that I need to use capacitors to allow the micro to stay on briefly between clicks/half presses, but haven’t found technical information on how to implement this. Perhaps if someone had both a schematic and firmware of a clicky (on/off switch) flashlight I would figure it out from there and that’s all it would take so I’d send that person the promised money.

Usually if the microcontroller has a normal decoupling capacitor on its supply you can use the RAM retention trick like this: Declare two variables that the compiler won’t automatically initialize to zero upon bootup:

unsigned int key attribute ((section (“.noinit”)));

unsigned char mode attribute ((section (“.noinit”)));

And then at the beginning of your loop, so that it executes once each time the processor boots up:

if(key==12345){ //check if RAM is still valid from last time

mode++; //yes, advance to next mode

if(mode>max_mode) {mode=0;}

}

else{ //long power loss. default to first mode

mode=default_mode;

key=12345; //initialize key for next power loss

}

Thanks for your helpful response tterev3

I am currently using a 47uF decoupling capacitor, is that adequate our should I double, quadruple, etc the value?

For the first two lines, do I insert that before void setup? Or within void setup? I’ve tried several places and can’t get it to compile. (Or am I supposed to fill in a variable?)

Thanks!

You will have to test the capacitance in your system and see, there are a lot of variables with the other electronics loading down the rail. The first two are variable declarations, they should be put outside of any functions to make them global (assuming you have both a setup() and main() that will need to access them). Sorry, I think the forum formatting ruined the syntax. It should be

unsigned int key __attribute__ ((section (".noinit")));

unsigned char mode __attribute__ ((section (".noinit")));

Thanks for that, that updated syntax compiled correctly.

Is there a target capacitance that this requires? Or a certain mcu ontime after the button opens the circuit?

Thanks!

The trick is that there is a lower voltage limit below which the processor won’t operate, and a lower voltage limit below which RAM won’t be maintained, but they are not the same voltage. You can look it up for your specific part, but generally somewhere around 2.5V it will stop running, but it will maintain RAM values until about 1.5V. So when you open up the switch and it loses power, the voltage will start to fall. It will fall very fast at first while the LEDs etc. can still operate and draw current, but once you cross the threshold where the LEDs and processor shut off, voltage starts to fall much more slowly since nothing is drawing significant current. The time it takes for voltage to fall from this first threshold to the second is how long you have to reconnect power with the switch (or the length of your tap on a reverse-clicky). In my designs I like to target about 500ms for this time period. If you have stuff like resistor dividers, they will drain the power faster and you may need to increase capacitance to extend the time. On some designs there is almost no power drain below 2.5V and you would need to add resistance to intentionally drain the capacitor (and/or reduce the capacitance) so that the part can fully reset in a reasonable amount of time.

Where in my code specifies the length of the tap on the clicky switch? Feel free to PM me, because if we get this working soon I’ll square up with the house.

Nothing in code specifies it, you just may need to adjust your hardware to make it an amount of time that works for you. Essentially you need to set how long the variable key “lives” for before it gets scrambled. If the microcontroller wakes up and sees that key still has the specific value it was assigned last time, then it knows the power loss was very short (because it was a tap on the switch) and therefore the value of mode is still valid (still holds what was used last time) and that the user would like to advance to the next mode. No timing needs to be specified for any of it in the code, it just needs to figure out every time it boots up if it’s a “warm boot” from a quick tap, or a “cold boot” from actually being off and unused for a while.

Here is a complete working example that uses this method. It’s for a different architecture but I tried to write it to be very human-readable so you can get a better understanding of the process:

Would it be too much to ask for you to put this together in my code so I can have something to test my hardware with? This is a bit over my head. That said it would take me days and days of testing this firmware to get something that probably won’t work well, whereas you can likely do it in minutes.

can you post your code that you have now with the PWM output working?

Heres a link, it wouldn’t post correctly in the forum Dropbox - flashlight attiny85.txt - Simplify your life

Ok, now I got ya. Here we call “driver” the entire schematics with microcontroller and everything, not just what you call led “driver”.

Without a specific cap for offtime, the method tterev3 mentions is your best bet. Timing can vary a little, but RAM in older generation ATtiny’s (13a/25/85) usually decay within a second or so. The newer series 1 don’t decay for ages even with 1uF input cap. My “no_init” RAM lasts over 10 minutes, I havn’t bothered testing further.

you can also use parts of Bistro HD OTSM code without an OTC
It has the MCU powered during off time in a low drain state from a 47uF capacitor

I can help you with hardware all the way from design, to prototyping and final production in small quantities
I am pretty familiar to use existing BLF firmware but I can’t program much mainly config the settings code

.

if you once tried Bistro HD OTSM you may consider rather modify it that writing something on your own