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

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

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))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

//**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() {
analogWrite(ledPin,mode1);
}

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

//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() {
digitalWrite(ledPin,HIGH);
delay(15);
digitalWrite(ledPin,LOW);
delay(100);
}


void loop() {

switch(mode){
default:
case 0:
modeone();
break;
case 1:
modetwo();
break;
case 2:
modethree();
break;
}
}
//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.