# Node-red-node-pi-neopixel - Control multiple LEDs at same time (not in succession)

**URL:** <https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157>\
**Category:** General\
**Created:** [24 November 2018 04:29 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157 "2018-11-24T04:29:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheSauce](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@TheSauce](https://discourse.nodered.org/u/TheSauce)\
**Post date:** [24 November 2018 04:29 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/1 "2018-11-24T04:29:39Z")

</div>

I'm playing around with a WS2812B LED strip. I have it working from following the fantastic guide located [here](https://randomnerdtutorials.com/node-red-with-ws2812b-addressable-rgb-led-strip/). What I can't figure out how to do though is control multiple LEDs at the same time. For example, I wan to turn the entire strip blue. I can send "0,0,255" and it will turn all of the LEDs blue or I could send "1,10,0,0,255" to make the first 10 LEDs blue but it turns the first one blue, then the second one, etc. Is there a way to turn them all (or selected LEDs) blue at the same time?

I know this can be done with FastLED on arduino but can't seem to find how to do that same thing with this node.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 November 2018 08:10 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/2 "2018-11-24T08:10:58Z")

</div>

What have you set the wipe time to ?

---

<div class="post-metadata">

**Author:** ![TheSauce](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@TheSauce](https://discourse.nodered.org/u/TheSauce)\
**Post date:** [24 November 2018 18:43 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/3 "2018-11-24T18:43:48Z")

</div>

I have it set to 0. On my one meter strand, it almost looks like they come on at the same time but on a longer strand (I plan on using a 5-6 meter strand), I think it would be pretty obvious that they aren't.

---

<div class="post-metadata">

**Author:** ![CaptClaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/captclaude/32/2125_2.png) [@CaptClaude](https://discourse.nodered.org/u/CaptClaude)\
**Post date:** [24 November 2018 20:32 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/4 "2018-11-24T20:32:15Z")

</div>

Are you using a Raspberry Pi? If so, it has been my experience that they cannot really control lots of Neopixels quickly. Instead of bit-banging with (semi) precise timing like with an Arduino or similar (Teensy, clones, ESPXXYY), the RPi uses a pulse-width modulation scheme to generate the bit stream to the pixels. Quoting from the [Adafruit Neopixel Advanced Coding Guide](https://learn.adafruit.com/adafruit-neopixel-uberguide/advanced-coding):

> The Raspberry Pi running Linux is a multitasking system, and control may switch among multiple running programs at any time. As such, it’s impossible to guarantee the strict 800 KHz signal required by NeoPixels. You may be able to fudge it for short intervals, but it’s not something that can be counted upon.

I had a project where I needed to control a strip about 10ft long and I programmed up an Arduino Nano clone to drive the pixels with the patterns I wanted and then switching between them by fiddling GPIO pins in parallel.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [24 November 2018 21:20 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/5 "2018-11-24T21:20:05Z")

</div>

> [@TheSauce](#):
>
> On my one meter strand, it almost looks like they come on at the same time but on a longer strand (I plan on using a 5-6 meter strand), I think it would be pretty obvious that they aren't.

I think the main reason for this is due to this code

```auto
def setPixels(strip, s, e, color, wait_ms=30):
    """Set pixels from s(tart) to e(nd)"""
    for i in range(s, e+1):
        strip.setPixelColor(i, color)
        strip.show()
        time.sleep(wait_ms/1000.0)

```

in the main python prog.

It updates and re-displays all the pixels each time  
It can probably be speeded up considerably by explicitly checking for a wait\_ms time of 0 so that all the pixel values are updated and only one refresh performed at the end

```auto
def setPixels(strip, s, e, color, wait_ms=30):
    """Set pixels from s(tart) to e(nd)"""
    if (wait_ms == 0):
        for i in range(s, e+1):
            strip.setPixelColor(i, color)
        strip.show()
    else:
        for i in range(s, e+1):
            strip.setPixelColor(i, color)
            strip.show()
            time.sleep(wait_ms/1000.0)

```

I'll test it out and issue a PR

Simon

---

<div class="post-metadata">

**Author:** ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)\
**Post date:** [24 November 2018 21:40 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/6 "2018-11-24T21:40:56Z")

</div>

I have an experimental version that uses a native node JS library (as opposed to Python) which has been working pretty good for me. It expects a full buffer for each frame in the payload so you will need to manage individual pixels upstream. You can find it here:

> **[natcl/node-red-contrib-rpi-ws281x-native](https://github.com/natcl/node-red-contrib-rpi-ws281x-native)**
>
> Contribute to natcl/node-red-contrib-rpi-ws281x-native development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![TheSauce](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@TheSauce](https://discourse.nodered.org/u/TheSauce)\
**Post date:** [25 November 2018 02:19 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/7 "2018-11-25T02:19:04Z")

</div>

Thanks @CaptClaude. I am using a Raspberry Pi (I meant to mention that in the original post). I could switch to Arduino if needed. I'll see if Simon's solution works.

---

<div class="post-metadata">

**Author:** ![TheSauce](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@TheSauce](https://discourse.nodered.org/u/TheSauce)\
**Post date:** [25 November 2018 02:21 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/8 "2018-11-25T02:21:02Z")

</div>

Thanks @nlecaude. I'll play around with that one a big as well.

---

<div class="post-metadata">

**Author:** ![TheSauce](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@TheSauce](https://discourse.nodered.org/u/TheSauce)\
**Post date:** [25 November 2018 02:38 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/9 "2018-11-25T02:38:15Z")

</div>

Thanks Simon. I'm curious what you'll find out.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 November 2018 16:51 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/10 "2018-11-25T16:51:52Z")

</div>

60 Neopixel strip showing start of strip and end of strip

Before speedup

After

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [25 November 2018 17:28 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/11 "2018-11-25T17:28:28Z")

</div>

Simon,,  
happy to just grab your code from above if you like (or wait for a PR :-). up to you.

(Also - I think same fix should also be applied to the wipe and 2 shift functions.)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 November 2018 17:49 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/12 "2018-11-25T17:49:57Z")

</div>

(Also - I think same fix should also be applied to the wipe and 2 shift functions.)

yep - just done and tested that

> [@dceejay](#):
>
> happy to just grab your code from above if you like (or wait for a PR :-). up to you.

Saves me a lot of effort 🙂

```auto
#!/usr/bin/python

# Import library functions we need
import sys
import time
try:
   from rpi_ws281x import __version__ , PixelStrip, Adafruit_NeoPixel, Color
except ImportError:
   from neopixel import Adafruit_NeoPixel as PixelStrip, Color
   __version__ = "legacy"

try:
   raw_input # Python 2
except NameError:
   raw_input = input # Python 3

# LED strip configuration:
LED_COUNT = 8 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # PWM channel
LED_GAMMA = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11,
11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
19, 19, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 27, 28,
29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 37, 38, 39, 40,
40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 88, 89,
90, 91, 93, 94, 95, 96, 98, 99,100,102,103,104,106,107,109,110,
111,113,114,116,117,119,120,121,123,124,126,128,129,131,132,134,
135,137,138,140,142,143,145,146,148,150,151,153,155,157,158,160,
162,163,165,167,169,170,172,174,176,178,179,181,183,185,187,189,
191,193,194,196,198,200,202,204,206,208,210,212,214,216,218,220,
222,224,227,229,231,233,235,237,239,241,244,246,248,250,252,255]

LED_COUNT = max(0,int(sys.argv[1]))
WAIT_MS = max(0,int(sys.argv[2]))
MODE = sys.argv[3]
LED_BRIGHTNESS = min(255,int(max(0,float(sys.argv[4])) * 255 / 100))
if (sys.argv[5].lower() != "true"):
   LED_GAMMA = range(256)

def getRGBfromI(RGBint):
   blue = RGBint & 255
   green = (RGBint >> 8) & 255
   red = (RGBint >> 16) & 255
   return red, green, blue

# Define functions which animate LEDs in various ways.
def setPixel(strip, i, color):
   """Set a single pixel"""
   strip.setPixelColor(i, color)
   strip.show()

def setPixels(strip, s, e, color, wait_ms=30):
   """Set pixels from s(tart) to e(nd)"""
   if (wait_ms > 0):
       for i in range(s, e+1):
           strip.setPixelColor(i, color)
           strip.show()
           time.sleep(wait_ms/1000.0)
   else:
       for i in range(s, e+1):
           strip.setPixelColor(i, color)
       strip.show()

def setBrightness(strip, brightness, wait_ms=30):
   """Set overall brighness"""
   strip.setBrightness(brightness)
   strip.show()
   time.sleep(wait_ms/1000.0)

def colorWipe(strip, color, wait_ms=30):
   """Wipe color across display a pixel at a time."""
   if (wait_ms > 0):
       for i in range(strip.numPixels()):
           strip.setPixelColor(i, color)
           strip.show()
           time.sleep(wait_ms/1000.0)
   else:
       for i in range(strip.numPixels()):
           strip.setPixelColor(i, color)
       strip.show()
   
def shiftUp(strip, color, wait_ms=30):
   """Shift all pixels one way."""
   oldcolour = strip.getPixelColor(0)
   strip.setPixelColor(0, color)
   strip.show()
   if (wait_ms > 0):
       time.sleep(wait_ms/1000.0)
       for i in range(1,LED_COUNT):
           newcolour = oldcolour
           oldcolour = strip.getPixelColor(i)
           strip.setPixelColor(i, newcolour)
           strip.show()
           time.sleep(wait_ms/1000.0)
   else:
       for i in range(1,LED_COUNT):
           newcolour = oldcolour
           oldcolour = strip.getPixelColor(i)
           strip.setPixelColor(i, newcolour)
       strip.show()

def shiftDown(strip, color, wait_ms=30):
   """Shift all pixels the other way."""
   oldcolour = strip.getPixelColor(LED_COUNT-1)
   strip.setPixelColor(LED_COUNT-1, color)
   strip.show()
   if (wait_ms > 0):
       time.sleep(wait_ms/1000.0)
       for i in range(LED_COUNT-2,-1,-1):
           newcolour = oldcolour
           oldcolour = strip.getPixelColor(i)
           strip.setPixelColor(i, newcolour)
           strip.show()
           time.sleep(wait_ms/1000.0)
   else:
       for i in range(LED_COUNT-2,-1,-1):
           newcolour = oldcolour
           oldcolour = strip.getPixelColor(i)
           strip.setPixelColor(i, newcolour)
       strip.show()

def wheel(pos):
   """Generate rainbow colors across 0-255 positions."""
   if pos < 85:
       return Color(pos * 3, 255 - pos * 3, 0)
   elif pos < 170:
       pos -= 85
       return Color(255 - pos * 3, 0, pos * 3)
   else:
       pos -= 170
       return Color(0, pos * 3, 255 - pos * 3)

def rainbow(strip, wait_ms=20, iterations=2):
   """Draw rainbow that fades across all pixels at once."""
   for j in range(256*iterations):
       for i in range(strip.numPixels()):
           strip.setPixelColor(i, wheel((i+j) & 255))
       strip.show()
       time.sleep(wait_ms/1000.0)

def rainbowCycle(strip, wait_ms=20, iterations=2):
   """Draw rainbow that uniformly distributes itself across all pixels."""
   for j in range(256*iterations):
       for i in range(strip.numPixels()):
           strip.setPixelColor(i, wheel(((i * 256 / strip.numPixels()) + j) & 255))
       strip.show()
       time.sleep(wait_ms/1000.0)

# Main loop:
if __name__ == ' __main__':
   # Create NeoPixel object with appropriate configuration.
   #strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
   if __version__ == "legacy":
       strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
   else:
       strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_GAMMA)# Intialize the library (must be called once before other functions).

   strip.begin()

   ## Color wipe animations.
   colorWipe(strip, Color(127, 0, 0), WAIT_MS) # Red wipe
   colorWipe(strip, Color(0, 127, 0), WAIT_MS) # Green wipe
   colorWipe(strip, Color(0, 0, 127), WAIT_MS) # Blue wipe
   colorWipe(strip, Color(0, 0, 0), WAIT_MS) # Off wipe

   ## Rainbow animations.
   #rainbow(strip)
   #rainbowCycle(strip)
   #colorWipe(strip, Color(0, 0, 0)) # Off wipe

   while True:
       try:
           data = raw_input()
           bits = data.split(',')
           if len(bits) == 2:
               if bits[0] == "brightness":
                   setBrightness(strip, min(255,max(0,int(bits[1]))), WAIT_MS)
           if len(bits) == 3:
               if MODE == "shiftu":
                   shiftUp(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS)
               elif MODE == "shiftd":
                   shiftDown(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS)
               else:
                   colorWipe(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS)
           if (MODE[0] == 'p' and len(bits) == 4):
               setPixel(strip, int(bits[0]), Color(int(bits[1]), int(bits[2]), int(bits[3]) ))
           if (MODE[0] == 'p' and len(bits) == 5):
               setPixels(strip, int(bits[0]), int(bits[1]), Color(int(bits[2]), int(bits[3]), int(bits[4]) ), WAIT_MS)
       except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program
           sys.exit(0)
       except Exception as ex:
           print("bad data: "+data)
           print(ex)

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [25 November 2018 18:27 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/13 "2018-11-25T18:27:10Z")

</div>

Hmm - apart from the fact you have changed all the indentation... so now I have a load of effort ☹

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 November 2018 18:37 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/14 "2018-11-25T18:37:36Z")

</div>

For some reason - it just didn't paste properly ☹  
I've raised PR instead but awaiting checks

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/ed902c47fa4789dd99f648f40a03631283d84537.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [25 November 2018 18:46 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/15 "2018-11-25T18:46:58Z")

</div>

too late... 🙂

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 November 2018 18:50 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/16 "2018-11-25T18:50:21Z")

</div>

it must have been the Github God's sending us a message about not taking shorcuts 🙂  
Just pasting again to see what happens this time

```auto
#!/usr/bin/python

# Import library functions we need
import sys
import time
try:
    from rpi_ws281x import __version__ , PixelStrip, Adafruit_NeoPixel, Color
except ImportError:
    from neopixel import Adafruit_NeoPixel as PixelStrip, Color
    __version__ = "legacy"

try:
    raw_input # Python 2
except NameError:
    raw_input = input # Python 3

# LED strip configuration:
LED_COUNT = 8 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # PWM channel
LED_GAMMA = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11,
11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
19, 19, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 27, 28,
29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 37, 38, 39, 40,
40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 88, 89,
90, 91, 93, 94, 95, 96, 98, 99,100,102,103,104,106,107,109,110,
111,113,114,116,117,119,120,121,123,124,126,128,129,131,132,134,
135,137,138,140,142,143,145,146,148,150,151,153,155,157,158,160,
162,163,165,167,169,170,172,174,176,178,179,181,183,185,187,189,
191,193,194,196,198,200,202,204,206,208,210,212,214,216,218,220,
222,224,227,229,231,233,235,237,239,241,244,246,248,250,252,255]

LED_COUNT = max(0,int(sys.argv[1]))
WAIT_MS = max(0,int(sys.argv[2]))
MODE = sys.argv[3]
LED_BRIGHTNESS = min(255,int(max(0,float(sys.argv[4])) * 255 / 100))
if (sys.argv[5].lower() != "true"):
    LED_GAMMA = range(256)

def getRGBfromI(RGBint):
    blue = RGBint & 255
    green = (RGBint >> 8) & 255
    red = (RGBint >> 16) & 255
    return red, green, blue

# Define functions which animate LEDs in various ways.
def setPixel(strip, i, color):
    """Set a single pixel"""
    strip.setPixelColor(i, color)
    strip.show()

def setPixels(strip, s, e, color, wait_ms=30):
    """Set pixels from s(tart) to e(nd)"""
    if (wait_ms > 0):
        for i in range(s, e+1):
            strip.setPixelColor(i, color)
            strip.show()
            time.sleep(wait_ms/1000.0)
    else:
        for i in range(s, e+1):
            strip.setPixelColor(i, color)
        strip.show()

def setBrightness(strip, brightness, wait_ms=30):
    """Set overall brighness"""
    strip.setBrightness(brightness)
    strip.show()
    time.sleep(wait_ms/1000.0)

def colorWipe(strip, color, wait_ms=30):
    """Wipe color across display a pixel at a time."""
    if (wait_ms > 0):
        for i in range(strip.numPixels()):
            strip.setPixelColor(i, color)
            strip.show()
            time.sleep(wait_ms/1000.0)
    else:
        for i in range(strip.numPixels()):
            strip.setPixelColor(i, color)
        strip.show()
    
def shiftUp(strip, color, wait_ms=30):
    """Shift all pixels one way."""
    oldcolour = strip.getPixelColor(0)
    strip.setPixelColor(0, color)
    strip.show()
    if (wait_ms > 0):
        time.sleep(wait_ms/1000.0)
        for i in range(1,LED_COUNT):
            newcolour = oldcolour
            oldcolour = strip.getPixelColor(i)
            strip.setPixelColor(i, newcolour)
            strip.show()
            time.sleep(wait_ms/1000.0)
    else:
        for i in range(1,LED_COUNT):
            newcolour = oldcolour
            oldcolour = strip.getPixelColor(i)
            strip.setPixelColor(i, newcolour)
        strip.show()

def shiftDown(strip, color, wait_ms=30):
    """Shift all pixels the other way."""
    oldcolour = strip.getPixelColor(LED_COUNT-1)
    strip.setPixelColor(LED_COUNT-1, color)
    strip.show()
    if (wait_ms > 0):
        time.sleep(wait_ms/1000.0)
        for i in range(LED_COUNT-2,-1,-1):
            newcolour = oldcolour
            oldcolour = strip.getPixelColor(i)
            strip.setPixelColor(i, newcolour)
            strip.show()
            time.sleep(wait_ms/1000.0)
    else:
        for i in range(LED_COUNT-2,-1,-1):
            newcolour = oldcolour
            oldcolour = strip.getPixelColor(i)
            strip.setPixelColor(i, newcolour)
        strip.show()

def wheel(pos):
    """Generate rainbow colors across 0-255 positions."""
    if pos < 85:
        return Color(pos * 3, 255 - pos * 3, 0)
    elif pos < 170:
        pos -= 85
        return Color(255 - pos * 3, 0, pos * 3)
    else:
        pos -= 170
        return Color(0, pos * 3, 255 - pos * 3)

def rainbow(strip, wait_ms=20, iterations=2):
    """Draw rainbow that fades across all pixels at once."""
    for j in range(256*iterations):
        for i in range(strip.numPixels()):
            strip.setPixelColor(i, wheel((i+j) & 255))
        strip.show()
        time.sleep(wait_ms/1000.0)

def rainbowCycle(strip, wait_ms=20, iterations=2):
    """Draw rainbow that uniformly distributes itself across all pixels."""
    for j in range(256*iterations):
        for i in range(strip.numPixels()):
            strip.setPixelColor(i, wheel(((i * 256 / strip.numPixels()) + j) & 255))
        strip.show()
        time.sleep(wait_ms/1000.0)

# Main loop:
if __name__ == ' __main__':
    # Create NeoPixel object with appropriate configuration.
    #strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
    if __version__ == "legacy":
        strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
    else:
        strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_GAMMA)# Intialize the library (must be called once before other functions).

    strip.begin()

    ## Color wipe animations.
    colorWipe(strip, Color(127, 0, 0), WAIT_MS) # Red wipe
    colorWipe(strip, Color(0, 127, 0), WAIT_MS) # Green wipe
    colorWipe(strip, Color(0, 0, 127), WAIT_MS) # Blue wipe
    colorWipe(strip, Color(0, 0, 0), WAIT_MS) # Off wipe

    ## Rainbow animations.
    #rainbow(strip)
    #rainbowCycle(strip)
    #colorWipe(strip, Color(0, 0, 0)) # Off wipe

    while True:
        try:
            data = raw_input()
            bits = data.split(',')
            if len(bits) == 2:
                if bits[0] == "brightness":
                    setBrightness(strip, min(255,max(0,int(bits[1]))), WAIT_MS)
            if len(bits) == 3:
                if MODE == "shiftu":
                    shiftUp(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS)
                elif MODE == "shiftd":
                    shiftDown(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS)
                else:
                    colorWipe(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS)
            if (MODE[0] == 'p' and len(bits) == 4):
                setPixel(strip, int(bits[0]), Color(int(bits[1]), int(bits[2]), int(bits[3]) ))
            if (MODE[0] == 'p' and len(bits) == 5):
                setPixels(strip, int(bits[0]), int(bits[1]), Color(int(bits[2]), int(bits[3]), int(bits[4]) ), WAIT_MS)
        except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program
            sys.exit(0)
        except Exception as ex:
            print("bad data: "+data)
            print(ex)

```

And of course it works this time..... 🙂

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 November 2018 20:38 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/17 "2018-11-25T20:38:02Z")

</div>

> [@TheSauce](#):
>
> Thanks Simon. I'm curious what you'll find out.

@dceejay has updated the node to 0.0.23 - its available on npm or can be upgraded via manage palette in the menu (you need to restart Node-RED after the install)

Try it out and see if its fast enough for you

> **[node-red-node-pi-neopixel](https://flows.nodered.org/node/node-red-node-pi-neopixel)**
>
> A Node-RED node to output to a neopixel (ws2812) string of LEDS from a Raspberry Pi.

---

<div class="post-metadata">

**Author:** ![TheSauce](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@TheSauce](https://discourse.nodered.org/u/TheSauce)\
**Post date:** [26 November 2018 06:59 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/18 "2018-11-26T06:59:24Z")

</div>

Thanks @cymplecy and @dceejay for working on this. I upgraded to 0.0.23 but now the NeoPixel node shows OK for a split second when deploying a flow or restarting Node-RED and then just shows "stopped".

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/ecbe8af45c240ba5466e7ccf036ba786a9d7e1d5.png)

If I revert back to 0.0.22 then it still works fine. Here is the log showing starting with Node-RED starting using 0.0.22 and then upgrading it to 0.0.23 using Palette Manager, stopping and starting Node-RED. I'm assuming the issue has to do with the very last line (session closed for user root) as that doesn't happen when using 0.0.22. I have even restarted my RPi but that didn't change anything. Lastly, I uninstalled it again and tried installing it from command line but still no change. I'm just not sure why it is happening or if there is another log I can look at.

_Started Node-RED graphical event wiring tool._  
_25 Nov 22:36:48 - [info]_  
_Welcome to Node-RED_  
_===================_  
_25 Nov 22:36:48 - [info] Node-RED version: v0.19.4_  
_25 Nov 22:36:48 - [info] Node.js version: v8.12.0_  
_25 Nov 22:36:48 - [info] Linux 4.14.79+ arm LE_  
_25 Nov 22:36:51 - [info] Loading palette nodes_  
_25 Nov 22:37:07 - [info] Dashboard version 2.11.0 started at /ui_  
_25 Nov 22:37:11 - [info] Settings file : /home/pi/.node-red/settings.js_  
_25 Nov 22:37:11 - [info] Context store : 'default' [module=memory]_  
_25 Nov 22:37:11 - [info] User directory : /home/pi/.node-red_  
_25 Nov 22:37:11 - [warn] Projects disabled : editorTheme.projects.enabled=false_  
_25 Nov 22:37:11 - [info] Flows file : /home/pi/.node-red/flows\_RPiZeroW.json_  
_25 Nov 22:37:11 - [info] Server now running at [http://127.0.0.1:1880/](http://127.0.0.1:1880/)_  
_25 Nov 22:37:11 - [warn]_  
_---------------------------------------------------------------------_  
_Your flow credentials file is encrypted using a system-generated key._  
_If the system-generated key is lost for any reason, your credentials_  
_file will not be recoverable, you will have to delete it and re-enter_  
_your credentials._  
_You should set your own key using the 'credentialSecret' option in_  
_your settings file. Node-RED will then re-encrypt your credentials_  
_file using your chosen key the next time you deploy a change._  
_---------------------------------------------------------------------_  
_25 Nov 22:37:11 - [info] [GenericBLE] Start BLE scanning_  
_25 Nov 22:37:12 - [info] Starting flows_  
_25 Nov 22:37:12 - [info] [GenericBLE] Start BLE scanning_  
_25 Nov 22:37:12 - [info] Started flows_  
_pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python -u /home/pi/.node-red/node\_modules/node-red-node-pi-neopixel/neopix.py 60 0 pixels 30 true_  
_pam\_unix(sudo:session): session opened for user root by (uid=0)_  
_25 Nov 22:39:10 - [info] Upgrading module: node-red-node-pi-neopixel to version: 0.0.23_  
_25 Nov 22:39:55 - [info] Upgraded module: node-red-node-pi-neopixel. Restart Node-RED to use the new version_  
_Stopping Node-RED graphical event wiring tool..._  
_25 Nov 22:40:46 - [info] Stopping flows_  
_pam\_unix(sudo:session): session closed for user root_  
_nodered.service: Main process exited, code=exited, status=1/FAILURE_  
_Stopped Node-RED graphical event wiring tool._  
_nodered.service: Unit entered failed state._  
_nodered.service: Failed with result 'exit-code'._  
_Started Node-RED graphical event wiring tool._  
_25 Nov 22:41:11 - [info]_  
_Welcome to Node-RED_  
_===================_  
_25 Nov 22:41:11 - [info] Node-RED version: v0.19.4_  
_25 Nov 22:41:11 - [info] Node.js version: v8.12.0_  
_25 Nov 22:41:11 - [info] Linux 4.14.79+ arm LE_  
_25 Nov 22:41:15 - [info] Loading palette nodes_  
_25 Nov 22:41:30 - [info] Dashboard version 2.11.0 started at /ui_  
_25 Nov 22:41:34 - [info] Settings file : /home/pi/.node-red/settings.js_  
_25 Nov 22:41:34 - [info] Context store : 'default' [module=memory]_  
_25 Nov 22:41:34 - [info] User directory : /home/pi/.node-red_  
_25 Nov 22:41:34 - [warn] Projects disabled : editorTheme.projects.enabled=false_  
_25 Nov 22:41:34 - [info] Flows file : /home/pi/.node-red/flows\_RPiZeroW.json_  
_25 Nov 22:41:34 - [info] Server now running at [http://127.0.0.1:1880/](http://127.0.0.1:1880/)_  
_25 Nov 22:41:34 - [warn]_  
_---------------------------------------------------------------------_  
_Your flow credentials file is encrypted using a system-generated key._  
_If the system-generated key is lost for any reason, your credentials_  
_file will not be recoverable, you will have to delete it and re-enter_  
_your credentials._  
_You should set your own key using the 'credentialSecret' option in_  
_your settings file. Node-RED will then re-encrypt your credentials_  
_file using your chosen key the next time you deploy a change._  
_---------------------------------------------------------------------_  
_25 Nov 22:41:34 - [info] [GenericBLE] Start BLE scanning_  
_25 Nov 22:41:35 - [info] Starting flows_  
_25 Nov 22:41:35 - [info] [GenericBLE] Start BLE scanning_  
_25 Nov 22:41:35 - [info] Started flows_  
_pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python -u /home/pi/.node-red/node\_modules/node-red-node-pi-neopixel/neopix.py 60 0 pixels 30 true_  
_pam\_unix(sudo:session): session opened for user root by (uid=0)_  
_pam\_unix(sudo:session): session closed for user root_

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 November 2018 08:07 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/19 "2018-11-26T08:07:32Z")

</div>

> [@TheSauce](#):
>
> upgraded to 0.0.23 but now the NeoPixel node shows OK for a split second when deploying a flow or restarting Node-RED and then just shows "stopped".

I just checked this morning and I'm getting the same ☹

Just working out how to roll back to previous version

> [@TheSauce](#):
>
> _pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python -u /home/pi/.node-red/node\_modules/node-red-node-pi-neopixel/neopix.py 60 0 pixels 30 true_  
> _pam\_unix(sudo:session): session opened for user root by (uid=0)_  
> _pam\_unix(sudo:session): session closed for user root_

PS - I'm not getting this on my system - do you have extra logging info switched on somewhere?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 November 2018 08:24 UTC](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157/20 "2018-11-26T08:24:13Z")

</div>

> [@cymplecy](#):
>
> Just working out how to roll back to previous version

Found out - just posting for future reference 🙂  
npm install node-red-node-pi-neopixel@0.0.22

[Next page](https://discourse.nodered.org/t/node-red-node-pi-neopixel-control-multiple-leds-at-same-time-not-in-succession/5157.md?page=2)
