Advice for programming NEOPixel strips

Trying to code a custom damaged lightsaber, the leds lighting up in sequence, before getting dimmer (roughly every 10sec). once fully dimmed to 1/8th brightness, i'm hoping to have the code randomly select LEDs on both strips independently and make it flicker/stutter, then upon activation of the accelerometer, reset to full brightness.

So far i have the startup and dimming code working, but having problems finding code examples/tutorials for selecting and making LEDs flicker/stutter on the strip.
any help would be greatly appreciated.

APIXELS controls both strips via pin 4
BPIXELS controls one of the strips via pin 6
CPIXELS controls the other strip via pin 7
Accelerometer connected via pin 8 (tilt sensor on diagram as tinkercad doesn't have accelerometers)

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

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            4
#define PIN2           6
#define Pin3           7

// Color Segments
#define APIXELS      19 // number of blue pixels 
#define BPIXELS      19 // number of pixels left
#define CPIXELS      19 // number of pixels right

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(19, PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel pixels1 = Adafruit_NeoPixel(19, PIN2, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel pixels2 = Adafruit_NeoPixel(19, PIN3, NEO_GRBW + NEO_KHZ800);
int delayval = 120; // delay for 1.2 seconds

void setup() {

  pixels.begin(); // This initializes the NeoPixel library.
for(int i=0;i<APIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,100,200)); // Set Pixels to Blue Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
}

void loop() {
   // Fill up 19 pixels with blue, on both sides starting from pixel number 0.
  delay (1000);
  for(int i=0;i<APIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,50,100)); // Set Pixels 1/2 brightness
    pixels.show(); // This sends the updated pixel color to the hardware.
  } 
delay (1000);
  for(int i=0;i<APIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,25,50)); // Set Pixels 1/4 brightness
    pixels.show(); // This sends the updated pixel color to the hardware.
  }
  delay (1000);
  for(int i=0;i<APIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,12,25)); // Set Pixels 1/8 brighness
    pixels.show(); // This sends the updated pixel color to the hardware.
  }
}

it is not good to shorting 3 pins together
light saber flickers not, only fluorescent lamp can do this

I redrew your drawing to remove pins 6 and 7, connect the DO of one "string" to the DI of the other, and connected power from the 5V power plug to the 5V NeoPixel. (do not use your Arduino as a power supply - NeoPixels use a lot of energy).

With this new drawing, you use only one data line (Pin 4) to control the cascaded Neopixel string (DO to DI). You can "control both" or "control one" or "control the other" just by using the Adafruit_NeoPixel library. The new length of your string is:

ALLPIXELS = APIXELS + BPIXELS + CPIXELS

Your new loop will be

for (int i = 0; i < (APIXELS + BPIXELS + CPIXELS); I++)

You will test for each "string" and light them according to pixel number.

if (0 < i < 19)
   { pixels.setPixelColor(i, pixels.Color(0,50,100)); // APIXELS }
if (19 < i < 38)
   { pixels.setPixelColor(i, pixels.Color(0,25,50)); // BPIXELS }
if (38 < i < 57)
   { pixels.setPixelColor(i, pixels.Color(0,12,25)); // CPIXELS }
pixels.show();

are you there?
check this out

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

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            4

// Color Segments
#define Side_A      19 // number of  pixels on the right
#define Side_B      19 // number of pixels on the left

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(Side_A + Side_B, PIN, NEO_GRBW + NEO_KHZ800);
int delayval = 120; // delay for 1.2 seconds

void setup() {
  pixels.begin(); // This initializes the NeoPixel library.
  for (int i = 0; i < Side_A; i++) {
    pixels.setPixelColor(i, pixels.Color(0, 100, 200)); // Set Pixels to Blue Color
    pixels.setPixelColor(i + Side_A, pixels.Color(0, 100, 200)); // Set Pixels to Blue Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
}

void loop() {
  // Fill up 19 pixels with blue, on both sides starting from pixel number 0.
  delay (1000);
  for (int i = 0; i < Side_A; i++) {
    pixels.setPixelColor(i, pixels.Color(0, 50, 100)); // Set Pixels 1/2 brightness
    pixels.setPixelColor(i + Side_A, pixels.Color(0, 50, 100)); // Set Pixels 1/2 brightness
    pixels.show(); // This sends the updated pixel color to the hardware.
  }
  for (int i = 0; i < 100; i++) {
    uint16_t Pos = random(Side_A + Side_B);
    uint32_t color = pixels.getPixelColor(Pos);
    pixels.setPixelColor(Pos, pixels.Color(255, 255, 255));
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(10);
    pixels.setPixelColor(Pos, color);
    pixels.show(); // This sends the updated pixel color to the hardware.
  }
  delay (1000);
  for (int i = 0; i < Side_A + Side_B; i++) {
    pixels.setPixelColor(i, pixels.Color(0, 25, 50)); // Set Pixels 1/4 brightness
    pixels.show(); // This sends the updated pixel color to the hardware.
  }
  delay (1000);
  for (int i = 0; i < Side_B; i++) {
    pixels.setPixelColor(Side_A + i, pixels.Color(0, 12, 25)); // Set Pixels 1/8 brighness
    pixels.show(); // This sends the updated pixel color to the hardware.
  }
}

I did the "pin 4 only" drawing... OP had a different wiring. See post #1 with pins 4, 6, 7 on three separate strings... my drawing intended to show how to (1) power all the neopixels, (2) have remote sections connecting DO, + and -, and (3) control all with one data pin.

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