How to index multiple non-sequential NeoPixel LEDs at once?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
vishwanathevs
 
Posts: 2
Joined: Wed Feb 08, 2023 11:16 pm

How to index multiple non-sequential NeoPixel LEDs at once?

Post by vishwanathevs »

Hello!

Apologies if this is already covered in the documentation, I couldn't find a solution to this.
So, I have a 30 LED Neopixel strip that I'm wiring up to a Raspberry pi pico. I'm coding on the pico using Circuitpython.

Now, I initialise my pixels using the following code

Code: Select all

import board
import neopixel

num_pixels = 30
pixels = neopixel.NeoPixel(board.GP0, num_pixels)
pixels.fill((283, 80, 0))
Now, I want to change the value of specific individual pixels. I see that I can select either one pixel to change, or a slice of pixels to change, as shown below:

Code: Select all

pixels[5] = (28, 8, 0) #Dimming a single pixel
pixels[5:7] = 2*(28, 8, 0) #Dimming a sequence of pixels 
However, I'd like to change multiple, no sequential pixels. Something like so

Code: Select all

pixels[5, 7, 15] = 3*(28, 8, 0)

But doing so gives me an error about how neopixel indices must be integers and not tuples. So I wanted to ask if there's any way I could index multiple, non-sequential neopixel LEDs?

Thanks!

User avatar
neradoc
 
Posts: 542
Joined: Wed Apr 27, 2016 2:38 pm

Re: How to index multiple non-sequential NeoPixel LEDs at once?

Post by neradoc »

Hi, you have to loop on them:

Code: Select all

for led in [5, 7, 15]:
    pixels[led] = (28, 8, 0)
If you always need to update groups of pixels, you can use the PixelMap class from the adafruit_led_animation library to set them as one, but that's mostly if you want to use animations, it's not faster: it just does the same loop.
https://learn.adafruit.com/circuitpython-led-animations

If the issue is setting multiple LEDs "at the same time" you can set auto_write to False and call pixels.show() when you want to update the strip.

Code: Select all

pixels.auto_write = False

for led in [5, 7, 15]:
    pixels[led] = (28, 8, 0)
pixels.show()

User avatar
vishwanathevs
 
Posts: 2
Joined: Wed Feb 08, 2023 11:16 pm

Re: How to index multiple non-sequential NeoPixel LEDs at once?

Post by vishwanathevs »

Perfect, this is exactly what I was looking for, thank you!
neradoc wrote: Thu Feb 09, 2023 7:03 am Hi, you have to loop on them:

Code: Select all

for led in [5, 7, 15]:
    pixels[led] = (28, 8, 0)
If you always need to update groups of pixels, you can use the PixelMap class from the adafruit_led_animation library to set them as one, but that's mostly if you want to use animations, it's not faster: it just does the same loop.
https://learn.adafruit.com/circuitpython-led-animations

If the issue is setting multiple LEDs "at the same time" you can set auto_write to False and call pixels.show() when you want to update the strip.

Code: Select all

pixels.auto_write = False

for led in [5, 7, 15]:
    pixels[led] = (28, 8, 0)
pixels.show()

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”