What's new
VORON Design

Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members!

Neopixel Toggle Macro

mocho

Member
Hello All,
I've put some gcode buttons on my 2.4, Switchwire and 0.2 but the macro to toggle lights with buttons that I've found online all use output_pin to activate gcode like:


Code:
#####################################################################
#  Caselight pin Definition
#####################################################################
## Caselight - XYE board, HB Connector
[output_pin caselight]
pin: P2.5
pwm: true
hardware_pwm: true
shutdown_value: 0
cycle_time: 0.0001

#####################################################################
#  Macros
#####################################################################
[gcode_macro _CASELIGHT_ON]
description: Helper: Light on
gcode:
  SET_PIN PIN=caselight VALUE={printer['gcode_macro _USER_VARIABLE'].peripheral.caselight.on_val}
  {action_respond_info("Caselight on")}
  
[gcode_macro _CASELIGHT_OFF]
description: Helper: Light off
gcode:
  SET_PIN PIN=caselight VALUE=0.0
  {action_respond_info("Caselight off")}

[gcode_macro CASELIGHT]
description: Toggle light
gcode: {% if printer['output_pin caselight'].value == 0 %} _CASELIGHT_ON {% else %} _CASELIGHT_OFF {% endif %}

My problem is that my case ligths are all neopixels and do not work with output_pin macros. Does anybody have a toggle macro to neopixels lights or could help to adapt this macros as i'm not any good at writing macros.

I Can Make On and OFF with
Code:
[gcode_macro _CASELIGHT_ON]
description: Helper: Light on
gcode:
#  SET_PIN PIN=caselight VALUE={printer['gcode_macro _USER_VARIABLE'].peripheral.caselight.on_val}
  SET_LED LED="caselight" RED=1 GREEN=1 BLUE=1 SYNC=0 TRANSMIT=1 #VALUE={printer['gcode_macro _USER_VARIABLE'].peripheral.caselight.on_val}
  {action_respond_info("Caselight on")}
    
[gcode_macro _CASELIGHT_OFF]
description: Helper: Light off
gcode:
#  SET_PIN PIN=caselight VALUE=0.0
  SET_LED LED="caselight" RED=0 GREEN=0 BLUE=0 SYNC=0 TRANSMIT=1
  {action_respond_info("Caselight off")}

But I don't know how to toggle it


Best Regards
 
Last edited:
already looked beafore. there is no macro to toggle ON/OFF and that's what I need to make button work
 
already looked beafore. there is no macro to toggle ON/OFF and that's what I need to make button work
Here the macros for LED effects for turning LEDs on/off. In this case nozzle LEDs to pure white at 100% power.

[led_effect set_nozzle_leds]
leds:
neopixel:sb_leds (9,10)
autostart: false
frame_rate: 24
layers:
static 0 0 top (0.0, 0.0, 0.0, 1.0)


[gcode_macro set_nozzle_leds_on]
gcode:
SET_LED_EFFECT EFFECT=set_nozzle_leds

[gcode_macro set_nozzle_leds_off]
gcode:
STOP_LED_EFFECTS LEDS="neopixel:nozzle_leds"
 
@Yezariael,
Thank you but as I said before I could already do an On and an OFF neopixels trough two macros. What I need is a Macro that if neopixel lights are OFF turn them ON and if they are ON turn them OFF! I need a TOGGLE macro! It as to be the same macro so I can invoque it with the same gcode_macro button (or 3 macros if the toggle as to invoke 2 macros to on and off).
As I can understand the macros that you show have ONE MACRO to turn it on AND ANOTHER MACRO to off, right?

Regards
 
There is only a small change required to adapt the toggle macro from output pins to neopixel (Or other led versions). Specifically, you can access the status of the led using the color_data for each led:

Code:
# Assuming you have
[neopixel name_of_your_led]
  ...

[gcode_macro CASELIGHT]
gcode:
  {% set brightness = printer["neopixel name_of_your_led"].color_data[0][1] %}
  {% if brightness == 0 %}
    _CASELIGHT_ON
  {% else %}
    _CASELIGHT_OFF
  {% endif %}

The indices in color_data[a][b] are
  • a: the location of the led in the chain. E.g., if you have multiple neopixel leds in series, you can query each of them individually.
  • b: the color channel. 0 for RED, 1 for GREEN, 2 for BLUE, 3 for WHITE.

So the macro above looks at the RED channel of the first led as an indicator of whether the led is on or not.
Personally, I'm using the WHITE channel here (color_data[0][3]), together with SET_LED WHITE=1. But it looks like you are not setting the WHITE value in your on/off macros.
 
There is only a small change required to adapt the toggle macro from output pins to neopixel (Or other led versions). Specifically, you can access the status of the led using the color_data for each led:

Code:
# Assuming you have
[neopixel name_of_your_led]
  ...

[gcode_macro CASELIGHT]
gcode:
  {% set brightness = printer["neopixel name_of_your_led"].color_data[0][1] %}
  {% if brightness == 0 %}
    _CASELIGHT_ON
  {% else %}
    _CASELIGHT_OFF
  {% endif %}

The indices in color_data[a][b] are
  • a: the location of the led in the chain. E.g., if you have multiple neopixel leds in series, you can query each of them individually.
  • b: the color channel. 0 for RED, 1 for GREEN, 2 for BLUE, 3 for WHITE.

So the macro above looks at the RED channel of the first led as an indicator of whether the led is on or not.
Personally, I'm using the WHITE channel here (color_data[0][3]), together with SET_LED WHITE=1. But it looks like you are not setting the WHITE value in your on/off macros.
Thank you qvD.
I have 88 caselight RGB (without white) neopixels. Using your macro, only modifying the name it works to toggle it.

Many thanks and best regards
 
Top