3 LED strips 3 Different Pins Programming help

I am making a prop weapon that splits into three parts because it's rather big so I needed three separate LED strips. Pin 2, 6, and 12. Pins 2 and 12 have 94 LEDs and pin 6 has 15 because it's a much smaller section.

Now I have a code that will light up each section individually but I can't figure out out to get them to all light up at once. My friend has tried to offer me some alternatives but his only light up 1 LED on 1 of the strips. Below is my code which does light up each section like it should. I will post his code in the following comment.


#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 2

#define NUMPIXELS   93

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN NEO_GRB + NEO_KHZ800);

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  //#if defined (__AVR_ATtiny85__)
  //  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
 // #endif // defined __AVR_ATtiny85__

  // Initialize the library
  strip.begin();

  // Send updates to the strip
  strip.show();
  
  // Set color to teal
}

void loop() {
  // whatever number is here, divide by 2
  // that's about how long it should take for the loop to complete
  pulse(16);
}

// @param wait  the time to wait between the brightness updates (in milliseconds)
// @param color the color to pulse
void pulse(uint8_t wait) {
  fadeIn(wait);

  // uncomment this if you want it to stay bright for some time
  delay(5000);
  
  fadeOut(wait);

  // uncomment this if you want it to stay dim for some time
  delay(5000);
}

// When brightness is updated, not all pixels get the update sometimes
// This makes sure it does
void rerenderStrip(uint16_t accumulator) {
  for (uint16_t i = 0; i < strip.numPixels(); ++i) {
    strip.setPixelColor(i, strip.Color(179,247,0));
  }
  strip.show();
}


// @param wait  the time to wait between the brightness updates (in milliseconds)
void fadeIn(uint8_t wait) {
  for (uint16_t i = 0; i < 256; ++i) {
    // Rerender the full strip so to ensure all pixels are updated
    rerenderStrip(i);
    delay(wait);
  }
}


// @param wait  the time to wait between the brightness updates (in milliseconds)
void fadeOut(uint8_t wait) {
  for (uint16_t i = 255; i >= 0; --i) {
    // Rerender the full strip so to ensure all pixels are updated
    rerenderStrip(i);
    delay(wait);
  }
}

This is my friend's code which only lights up a single LED on the whole prop.


#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define NEO_PIN1         2
#define NEO_PIN2         6
#define NEO_PIN3         12

#define NPINS       3
  
#define NUMPIXELS_2_12   93
#define NUMPIXELS_6     15

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strips[3] = {
    Adafruit_NeoPixel(NUMPIXELS_2_12, NEO_PIN1, NEO_GRB + NEO_KHZ800),
    Adafruit_NeoPixel(NUMPIXELS_6,    NEO_PIN2, NEO_GRB + NEO_KHZ800),
    Adafruit_NeoPixel(NUMPIXELS_2_12, NEO_PIN3, NEO_GRB + NEO_KHZ800)
};

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  //#if defined (__AVR_ATtiny85__)
  //  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
 // #endif // defined __AVR_ATtiny85__

  for(int i = 0; i < NPINS; ++i){
    Adafruit_NeoPixel strip = strips[i];

    strip.begin();
    strip.show();
    strip.setBrightness(50);
  }
}

void loop() {
  // whatever number is here, divide by 2
  // that's about how long it should take for the loop to complete
  pulse(16);
}

// @param wait  the time to wait between the brightness updates (in milliseconds)
// @param color the color to pulse
void pulse(uint8_t wait) {
  fadeIn(wait);

  // uncomment this if you want it to stay bright for some time
  delay(5000);
  
  fadeOut(wait);

  // uncomment this if you want it to stay dim for some time
  delay(5000);
}

// When brightness is updated, not all pixels get the update sometimes
// This makes sure it does
void rerenderStrip(uint8_t accumulator) {
  for(int i = 0; i < NPINS; ++i){
    Adafruit_NeoPixel strip = strips[i];
    uint32_t color = strip.ColorHSV(21626, 255, accumulator);
    strip.fill(color);
    strip.show();
    //strip.show();
  }
}


// @param wait  the time to wait between the brightness updates (in milliseconds)
void fadeIn(uint8_t wait) {
  for (uint8_t i = 0; i <= 100; ++i) {
    // Rerender the full strip so to ensure all pixels are updated
    rerenderStrip(i);
    delay(wait);
  }
}


// @param wait  the time to wait between the brightness updates (in milliseconds)
void fadeOut(uint8_t wait) {
  for (uint8_t i = 100; i >= 0; --i) {
    // Rerender the full strip so to ensure all pixels are updated
    rerenderStrip(i);
    delay(wait);
  }
}
Collapse
Darken_blade.ino
4 KB

Easy. Drop all delays and for loops.

Make the loop function the only loop you need.
Base all timings on millis, don't block.

all strips are connected to one microcontroller. So there are wires from each strip to the µC.

In summary this means you have 94 + 94 + 15 = 203 neopixels

These 203 neopixels can be driven all from one IO-pin.
you simply connect the dataout of the last neopixel of your first strip with the datain of the second
and
connect the dataout of the last neopixel of your second strip with the datain of the third

The data-transmission is always the same regardless of a pixel being in a strip or in mutliple strips
data-out---connects to next data-in

To make just your second strip light up you simply only change values from 95th to 188th pixel
and keep all others the same value.

This requires to modify your code to use offsets (start with pixel 95 start with pixel 189
to modify them

It is a different thing for the power-supply. You should feed in power at multiple places at least at each end of the strips

the other approach requires consequent non-blocking coding.
This means zero use of function delay() and replacing each and every delay() by non-blocking timing based on function millis()

If you insist on using three IO-pins it adds complexity in managing three IO-pins with non-blocking timing
best regards Stefan

looks like your functions sequence across LEDs, indexed from 0-255 in a "for loop" and with a delay() in each iteration, so nothing else can eb done until the for loop completes.

instead of using a for loop and delay, millis() can be used to only do the action within your for loops after the delay period has passed allowing other operations in between

the code will need separate index variables (the "i" in the for loops) for each sequence and it looks like the timer code needs to be turned off once the sequence is complete.

based on @StefanL38 's comment, it's not clear if separate pins are needed or simply separate indices for sub-sets of LEDs in the LED strings.

one difficulty is knowing what state the string is: all on or all off, and whether functions similar to your fadeIn() or fadeOut() should be called.

Adafruit has a Tutorial that might be helpful for the "multi-tasking".

And here is all the blocking pixel demo code fully unblocked, so you can see how the theory is applied:

Non-blocking effects code

HTH

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.