Light an LED when battery below 50%

Probably a simple question, can anyone show me the basic style code to light an LED red when battery is below 50%

If you want to have other effects, just add this layer to existing style.

AlphaL<Red,IsLessThan<BatteryLevel,Int<16384>>>

If you just want the LED to only do this just add a Black base layer like so:

Layers<Black,AlphaL<Red,IsLessThan<BatteryLevel,Int<16384>>>>

If it’s a single color LED, replace “Red” with “White” as White is full power on a single color LED, Red would be 33% power.

One Note, BatteryLevel isn’t exactly linear so 16384 may not actually be 50% of your battery. It could be more and it could be less.

awesome cheers, what value is theoretically 100%? 32768?

32768 is full value

1 Like

Sorry quick tweak to that, how would I make it blink red if over 16384 and orange if below it?

There are several ways depending on EXACTLY what you want.

Blink the same but change color below value? Should it fade from Red to Orange or just a swap?

Change blinking and color below value? Gradual or instant?

Etc?

The more specific you can be on behavior the easier it is to choose best approach.

Basically just blinking red but if battery below 50ish % that it changes to blinking orange.

If I can get that basic functionality working I can probably fiddle etc to get any fades

Layers<
  AlphaL<Blinking<Orange,Black,1000,500>,Int<32768>>,
  AlphaL<Blinking<Red,Black,1000,500>,IsGreaterThan<BatteryLevel,Int<16384>>>
>

It’s simply covering (hiding) the orange based layer with the Red one when the conditions are right.

1 Like

The “basic” would vary per what you’re trying to do, NoSloppy’s just covers one layer with another, you can’t get any fade on that type of approach. If his is all you need you can probably make it more efficient by just swapping out the color with a function in one layer versus duplicating the full style in two layers, but again if you wanted it to fade or transition then I’d go a completely different approach. If the above is good then no need for more, but if you’re trying to do more you’ll be limited with above in which case more specifics on what you’re wanting will make it easier to give direction.

I used this

StylePtr<Layers<
  AlphaL<Blinking<Orange,Black,1000,500>,Int<32768>>,
  AlphaL<Blinking<Red,Black,1000,500>,IsGreaterThan<BatteryLevel,Int<16384>>>
>>(),

But get this error when uploading

could not convert ‘((StyleHelper<RGBA_nod>*)this)->StyleHelper<RGBA_nod>::getColor2(i)’ from ‘RGBA_nod’ to ‘OverDriveColor’

oh yeah duh. remove the alpha from the base layer like

StylePtr<Layers<
  Blinking<Orange,Black,1000,500>,
  AlphaL<Blinking<Red,Black,1000,500>,IsGreaterThan<BatteryLevel,Int<16384>>>
>>(),

sorted :smiley: tx