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

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

Can you provide an example of a more modern processor Mike C?

Try this. Not sure where your source came from - appears to have some Arduino-based I/O functions? Are you doing this in Atmel Studio with the compiler or in Arduino?

#include <avr/sleep.h>

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

//**here you set brightness of modes
int mode1 = 255;
int mode2 = 8;

const int ledPin = 0; // setting LED pin

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

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

void setup() {
if(key==12345){ //check if RAM is still valid from last time
mode++; //yes, advance to next mode
if(mode>2) {mode=0;}
else{ //long power loss. default to first mode
mode=0; //default mode after long power off
key=12345; //initialize key for next power loss

pinMode(ledPin, OUTPUT); // Sets LED as output

sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
sbi(PCMSK,PCINT1); // Which pins are affected by the interrupt

//mode one: LED is set to 100% brightness by default
int modeone() {

//mode two: LED is set to your preference of brightness (my default is about 30% = 76)
int modetwo() {

//mode three: strobe light, I set it to be the most disorienting in darkness but feel free to change on and off times (in [ms])
int modethree() {

void loop() {

case 0:
case 1:
case 2:
//loop end

void system_sleep() {
cbi(ADCSRA,ADEN); // Switch Analog to Digital converter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode
sleep_mode(); // System sleeps here
sbi(ADCSRA,ADEN); // Switch Analog to Digital converter ON

ISR(PCINT0_vect) {

Newbie here

What coding language is this?

Yes I am using Arduino IDE.

I’m using the ATtiny3217 for all of my own projects, ever since BLF user gchart paved the way with the 1-series: Adventures in TinyAVR 1-Series
I don’t know of anyone else here using the 1-series but I haven’t been keeping up much lately.

Also for what he is aiming of firmware functionality the 25 should be well enough space and connectivity
Maybe even the 13a is enough for ultra cheap build

there we have BLF firmware he can partially use or modify

C / C. So you’re thinking of starting with C?

BTW - There’s an ATTINY85 dev board that plugs directly into USB and is supported by Arduino. Unfortunately, the bootloader takes up ~2K of ROM.

Can someone refer to me more about this, what light is used and how to you “hook the light up to each voltage”? This is in the Bistro-tripledown code -

* To find out what values to use, flash the driver with battcheck.hex
* and hook the light up to each voltage you need a value for. This is
* much more reliable than attempting to calculate the values from a
* theoretical formula.
* Same for off-time capacitor values. Measure, don’t guess.

The light that is used is the one you’re writing the firmware for! You hook the light up to one voltage at a time by connecting it to a regulated supply, and write out the ADC result for each voltage you care about.

you don’t need to do any voltage calibrations if the code you are using is the one you posted - it has no battery measurement functions in it

I’ve been working on this all week, can seem to get a working hex file. I’ve tried it in AVR Studio 7 and Arduino, no luck. In Arduino, I get errors that end with “#include nested too deeply”.

Can someone help me out with a hex file?

Trying to use Bistro HD firmware here [UPDATE:v1.7.1,Q8&1chanOTSM]bistro-HD, bistro your way. OTSM, eswitch(devel), Vcc reads, safe_presses, turbo timeout...

I thought you said the file you posted before was working to output the pwm you needed. Did you try the modified version I replied with?

tterev3 yes I tried it and couldn’t get it to compile in arduino. It did build in AVR Studio, but couldn’t get it to load due to signature differences (yes the correct chip ATtiny25 was selected.

I set the fuses to
Low: 0xd2
High: 0xde
Ext: 0xff

It threw a warning and I proceeded with programming the fuses, and it has bricked 4 chips so far. I’ve overnighted more chips so I should have them tomorrow.