Global brightness variable/function

I am looking for a pattern-accessible variable (not human UI) that sets GLOBAL brightness.

Hear me out…

I’ve made some PB interface PCBs and boxes for each of my kids, connected to light strips around their bed. My interface board has some header pins to allow them to connect some buttons on top up to different GPIO pins, with the goal being allowing them to expand their coding as they are able.

Currently, there is a toggle switch that turns the board on and off, but I’d like it to stay on eventually, to allow the brightness to be controlled by one of the momentary buttons on the box. The purpose for this is to allow my smart home to control the lights as well.
Bedtime - the lights come on, to a given pattern.
Lights-out time - the lights fade out
Wake-up time - the lights turn on with a sunrsie pattern to gradually wake them up
Harsh wake-up time - after a given amount of time, the room lights turn on (their current abrupt wake-up)

If the box toggle switch was used, then the board would be off, and the house couldn’t turn the bed lights on/off/etc.
I already have my own bed working with the smart house to control its lights for sunrise, using the wonderful python library, so this piece will be easy to expand.

What I’m hoping for is a piece of code that can be pasted into each of the patterns they would be using (as it seems it isn’t possible to have something more global running in the background) that can set the global brightness in a manner similar to the slider bar at the top, or the python library brightness command. Is there an exposed global brightness variable that can be used within a pattern?

I recognize that the usually more sane approach is to simply change the brightness within the render function - however I’m wanting both for this feature to be minimally destructive for the destination patterns, as well as for it to modify the same brightness variable that the smart house/python library modifies.

For anyone who would like to run with this, my eagle files, fusion 360 files, photos, and parts links are below

and some photos of the box are here. Yes, the kids were helping me solder it up, taking most of my attention and distracting me from the mess in the photos.

The code likely would be something like (with the exception of fixing the ‘brightness=’ portion, and buttonProcess() being called from beforeRender):

// buttonSection
var BUTTON_PIN = 25
export var buttonValue, buttonPressed
export var buttonIndex
var brightness1 = 1
var brightness2 = 0.5
var brightness0 = 0

export function buttonProcess() {
buttonValue = digitalRead(BUTTON_PIN)
if (buttonValue ==0) {
if (buttonPressed ==0) {
buttonPressed = 1 // pause watching for buttonpress
if (buttonIndex == 0) {
buttonIndex = 1
else if (buttonIndex == 1) {
buttonIndex = 2
else { // both wrap around, as well as initialize if value unasigned
buttonIndex = 0
else {
buttonPressed = 0 // reset watching for buttonpress
// end buttonSection

edit: updated to correctly read a INPUT_PULLUP press event as buttonValue=0
The pattern correctly reads button presses, but does not adjust brightness.

1 Like

Hey @bdm!

That’s an awesome project! I love the idea and that your kids are helping you with it.

I can see where an internal-to-pattern-code API for adjusting the global brightness would be useful to you in this. I also think it kind of breaks an expectation that patterns can’t mess up a global device setting for the next pattern.

If Pixelblaze adds a shared library or “background” code, kind of like an interrupt handler outside of patterns, I can see it being useful there.

For the time being, I’d recommend that if you’re already going to have this snippet in each pattern, you may as well just modify each pattern’s call to hsv(h, s, v) to hsv(h, s, v * brightness).

Alternately, you can have the button reading code in every pattern maintain an exported var that stores the current brightness state you want (it’s kind of already doing that above). Have your Python external process poll getVars to monitor its value, and then set the global brightness appropriately. This would at least maintain the state between patterns. I’d recommend NOT re-setting the brightness unless the state has changed, as the global brightness setting is persisted to flash memory, and tens of thousands of writes will degrade the flash memory.