led-toggle-dim.xodball (29.8 KB)
As I mentioned in the initial response…this is not a trivial program. For those who don’t want to download the xodball, here is a picture:
The false at center-top is a tweak-boolean. It should be replaced with a button node. The watch directly below it near the bottom should be replaced with an led node.
The nodes between these two are just for switching led on/off. The nodes to the left are controlling what mode the program is in (toggle led on/off, make it brighter, make it dimmer). The nodes on the right are for controlling how bright the LED is when it is on.
This section of code on the right:
Is just to initialize the LED brightness to 1 when program starts, then use the stored value in buffer.
When the program starts, it is in toggle mode with LED at full brightness. A quick press of the button will turn the LED off. Another quick press will turn it back on.
Hold the button down for for 2 seconds, and the debounce node will send a pulse to the count node to switch to the next mode (make LED brighter). Entering brighter mode will also force the LED to turn on so we can see how bright it is. It will continue to get brighter as long as the button is held down. When you release the button, the pulse-on-false node in the upper-left will send a pulse to the count node to switch to the next mode (make the LED dimmer). Pressing the button again will make the LED dimmer as long as it is held. Releasing the button causes the pulse-on-false in the upper-left to again pulse the count node and return us to toggle mode.
On the right side is a buffer node to store the brightness for the LED when it is on. If we are NOT in toggle mode, the gate node above the buffer node allows the clock node above it to update the buffer every 2 seconds. The if-else below the buffer node determines whether we are adding a positive 0.2 (brighter) or a negative 0.2 (dimmer). The clip node below the add keeps us within our 0-1 range.
If you want the long-press to enter bright/dim mode, then the next button press to start making it brighter, you will need to add a “dummy” mode; when count=1, you actually do nothing, then when you release the button, it will advance to count=2, which will now be the brighter mode. You will need to reset when count>3 instead of the current count>2. To improve the user interface, you could flash the LED when count=1 so user knows they are now in bright/dim mode and they can release the button. led-toggle-dim2.xodball (35.5 KB)