Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.

Triggering effects with NEOPIXEL
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Triggering effects with NEOPIXEL

by MillenniumThalken on Fri Nov 08, 2019 11:09 am

I am using CircuitPython and the CPX along with a NEOPIXEL light strip to try and program a prop effect, I will later be transferring the code to a Feather M4 Express. The desired effect is to have a blue pixel rundown the length of the strip and when it reaches the end start over from the top, so basically it is in a loop. I call this the IDLE effect. The IDLE effect is triggered with a toggle switch. I then want to to push a different button to trigger what I call the POWER-UP effect, which starts at the end of the light strips and fills the pixels one by one until the whole strip is lit and then transitions from blue to red. The problem I am running into is I have the IDLE effect working, but I can't seem to trigger and other effects via button while the code is in the IDLE function. Any help or suggestions would be greatly appreciated.

Code: Select all | TOGGLE FULL SIZE
import time
import board
import neopixel
from digitalio import DigitalInOut, Direction, Pull

pixel_pin = board.A2
num_pixels = 30

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.5, auto_write=False)

button_1 = DigitalInOut(board.BUTTON_B)

button_2 = DigitalInOut(board.BUTTON_A)

# switch = DigitalInOut(board.D7)
# switch.direction = Direction.INPUT
# switch.pull = Pull.UP
# switch_state = switch.value

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
OFF = (0, 0, 0)

#this effects acts as the 'idle' function for the weapon. it will have a group of pixels that travel down the LED strip
#together, when the pixels reach the end of the LED strip they will start over from the beginning, giving an infinite loop effect
#the counter variable in the postion of the LED strip that the pixel is currently at. The counter is applied to the pixel1_counter
#and updated to make it travel. the lit_pixels variable determines how many pixels are in the group of pixels traveling down
#the LED strip.
def idle_power(wait):
    counter = 0
    while counter < num_pixels:
        pixel1_counter = counter
        lit_pixels = counter - 5
        pixels[pixel1_counter] = CYAN
        pixels[lit_pixels] = OFF
        pixel1_counter = pixel1_counter + 1
        lit_pixels = lit_pixels + 1
        counter = counter + 1

        if counter == num_pixels:
            counter = 0
def pixels_off():
    for i in range(num_pixels):
        pixels[i] = OFF

while True:
    if not button_1.value:
    if not button_2.value:

Posts: 17
Joined: Mon Oct 28, 2019 2:07 pm

Re: Triggering effects with NEOPIXEL

by dastels on Fri Nov 08, 2019 11:19 am

You can't trigger the other effect while in the idle function because the button inputs are being completely ignored during that time. One approach yould be to check the button value in the idle() loop and return if it's False. That would take you back to the main loop and the other buttons would be processed.


Posts: 2757
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.