STAR Firmware by JonnyC - Source Code and Explanation

Search for the line:

if (low_voltage(ADC_LOW)) {

Replace ADC_LOW with ADC_CRIT. Should work…

Thanks - I’ll give it a try now.

Tried the above.

Near 3v the light blinks before stepping down to what I assume is low mode.

What I’m hoping to do is skip the low mode and go straight to shutoff. Being able to keep the blinks would be helpful as well.

Any additional thoughts?

I suspect the problem lies in the next line of code:

if (mode_idx == 0 && ALT_PWM_LVL <= modes[mode_idx])

By changing ADC_LOW to ADC_CRIT I think we’ve just changed the trigger to take us to the line of code above.

I think instead of looking for an alternate mode it should instead blink and then shut down.

The version of Star I have doesn’t have ALT_PWM level, so I’m sure we are not looking at the same code.

However, you are right. I forgot about that part. As it is now it goes to step down on critical, then shuts off. You want it to shut off straight away.

So, another quick fix without extensive editing… Keep the last change and then change:
if (mode_idx == 0 && ALT_PWM_LVL <= modes[mode_idx])

to:
if (1)

Not a pretty solution, but requires minimal editing… unless I’ve missed something else…

Success! Repeatable shutoffs at 2.994v.

Thanks for your help.

To improve myself a bit. What does the last change to “if (1)” actually do?

This should work:

Comment out “#define ADC_LOW” and replace the voltage-mon section with this one:

EDIT: Too late :~ , but its a cleaner soultion :slight_smile:

#ifdef VOLTAGE_MON
#ifdef ADC_LOW
if (low_voltage(ADC_LOW)) {
// We need to go to a lower level
if (mode_idx == 0 && ALT_PWM_LVL <= modes[mode_idx]) {
// Can't go any lower than the lowest mode
// Wait until we hit the critical level before flashing 10 times and turning off
#endif
while (!low_voltage(ADC_CRIT));
i = 0;
while (i++<10) {
set_output(0);
_delay_ms(250);
set_output(modes[0]);
_delay_ms(500);
}
// Turn off the light
set_output(0);
// Disable WDT so it doesn't wake us up
WDT_off();
// Power down as many components as possible
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
#ifdef ADC_LOW
} else {
// Flash 3 times before lowering
hold_pwm = ALT_PWM_LVL;
i = 0;
while (i++<3) {
set_output(0);
_delay_ms(250);
set_output(hold_pwm);
_delay_ms(500);
}
// Lower the mode by half, but don't go below lowest level
if ((ALT_PWM_LVL >> 1) < modes[0]) {
set_output(modes[0]);
mode_idx = 0;
} else {
set_output(ALT_PWM_LVL >> 1);
}                   
// See if we should change the current mode level if we've gone under the current mode.
if (ALT_PWM_LVL < modes[mode_idx]) {
// Lower our recorded mode
mode_idx--;
}
}
// Wait 3 seconds before lowering the level again
_delay_ms(3000);
}
#endif
#endif

Nice.

The short explanation: “if (1)” means “do this”.

The long explanation: “if” expressions check the terms you put between the (). If those terms are true, the result passed back is 1. So by replacing the terms with “1” I’ve sort of passed the result “true” to an “if” check.
The if(1) line itself is not needed, but in order to remove it you need to remove more stuff. For example, this is what it looks like now:

if (1) {
-do this code
}
else {
-code here will never be executed because if (1) never fails
}

You can make it look like this:

-do this code

If you remove the if check you have to remove the {} characters and else statement along with all the code in that else statement.

FWIW, the step-down may allow the light to run for a significantly longer time, since voltage often recovers after decreasing amperage. It also acts as a way of getting more warning before shut-off. But if you don’t like it, you don’t like it. :slight_smile:

BTW, did anyone try simply setting the LOW and CRIT values to the same number? It seems like a straightforward way to do it (but would probably not disable step-down).

Thanks all.

Toy I did try this and unfortunately the LOW always grabbed before the CRIT. Meaning it would switch to a lower level.

All of the tests I had successfully run for the low voltage shut off were done on Turbo that defaulted to Turbo Low.

I’m now running the tests again and the low voltage shut off is not working.

Should ADC_CRIT work in all modes?

I’m not sure… I haven’t tested STAR extensively. In my mods I usually cut out that part of the code because it takes too much space, and replace it with something smaller / simpler. The one I’ve tested the most is BLF-A6, since it’ll be factory-installed and I want to make damn sure it works correctly. It still needs a somewhat unfortunate amount of calibration for each different model of driver it runs on though.

If you have a bench power supply, it greatly helps testing. If not, a LiFePo4 cell or even a 3V CR123 or CR2032 cell can work for LVP tests. It can also be useful to temporarily bump up the LOW/CRIT levels to make it trigger at a higher voltage, just to make sure it works, and then put them back down where they should be.

The code I’m looking at should work, but I’m looking at standard star v1.1. I haven’t touched it for a long time though, I use my own firmware and it does everything differently.

Did you try my suggestion?
It removes all the stepping down part.

The cleanest would be to remove everything in the while(1) except shutdown code, and sleep code after if there is any:

while(1) {

if (LowVoltage(ADC_CRIT)) {
Shut down code…
}

Sleep code (if any)

}

If you can post your code somewhere easy to read I can give a go at a quick edit, but I can’t test it as I don’t have any ATtiny13A drivers anymore.

Odd - I have not yet tried your code but will today. I’ll report back.

Mike - Thanks for the suggestion. Let me see how Odd’s code works and I’ll try and find a way to post my code if it doesn’t work out.

Thanks all for the help.

How much work would it be to add dual-pwm to the dual-switch firmware?

Odd - so far so good on this. I’ll keep testing it and report back.

Odd - so far so good on the code. Thanks

Hmm… good question. I didn’t realize there was still a flavor of STAR without dual PWM support.

I’m not sure how much work it would be. I just checked for updates upstream, hoping it had already been done, and there haven’t been any since January. The methods used in other versions of STAR have varied, and the dual switch driver isn’t quite like any other. So, it would be more than just a simple merge or a cut-and-paste job. I think there’s at least room to add it though; I get 796 bytes when I compile STAR_dual_switch.