Neopixel code help needed

I am just starting to play around with my neopixels and am trying to get my own coding down. Mind you I am not by any means a programmer, so my methods will be pretty basic.

Also ignore my RGB values. For some reason my lights have everything switched around from the standard RGB or GRB. They operate using RBG...???

For my first program I want to have 4 colors that are each 10 pixels long chasing each other. These 4 colors would constantly repeat throughout the whole strand.

This code does not fully work. I get colors overlapping, and lose a bunch of my red segments.

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

#define PIN 6

// 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(100, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

  uint32_t red = strip.Color(255, 0, 0);
  uint32_t blue = strip.Color(0, 255, 0);
  uint32_t lime = strip.Color(0, 0, 255);
  uint32_t magenta = strip.Color(255, 255, 0);
  uint32_t teal = strip.Color(0, 255, 255);
  uint32_t yellow = strip.Color(255, 0, 255);
  uint32_t white = strip.Color(255, 255, 255);
  uint32_t orange = strip.Color(255, 0, 128);
  uint32_t pink = strip.Color(255, 203, 192);
  uint32_t purple = strip.Color(128, 128, 0);
  uint32_t green = strip.Color(0, 0, 128);
  uint32_t brown = strip.Color(165, 42, 42);
  uint32_t maroon = strip.Color(128, 0, 0);
  int r = 0;
  int g = 10;
  int w = 20;
  int b = 30;

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
 strip.setPixelColor(r, red);
 strip.setPixelColor(r+40, red);
 strip.setPixelColor(r+80, red);
 strip.setPixelColor(r-40, red);
 strip.setPixelColor(r-80, red);
 strip.setPixelColor(g, lime);
 strip.setPixelColor(g+40, lime);
 strip.setPixelColor(g+80, lime);
 strip.setPixelColor(g-40, lime);
 strip.setPixelColor(g-80, lime);
 strip.setPixelColor(w, white);
 strip.setPixelColor(w+40, white);
 strip.setPixelColor(w+80, white);
 strip.setPixelColor(w-40, white);
 strip.setPixelColor(w-80, white);
 strip.setPixelColor(b, blue);
 strip.setPixelColor(b+40, blue);
 strip.setPixelColor(b+80, blue);
 strip.setPixelColor(b-40, blue);
 strip.setPixelColor(b-80, blue);
 strip.show();
 r++;
 g++;
 w++;
 b++;
 if (r==strip.numPixels())r=0;
 if (g==strip.numPixels())g=0;
 if (w==strip.numPixels())w=0;
 if (b==strip.numPixels())b=0;
 delay(50);
}

Program has a problem, hope this program can help for you.

#include <Adafruit_NeoPixel.h>
 
#define PIN 6
#define MAX_LED 100

// Parameter 1 = ws2811
// Parameter 2 = arduino PIN
// 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, PIN, NEO_RGB + NEO_KHZ800 );

uint8_t R[10]={0,100,160,255,16,200,78,49,60,43};
uint8_t G[10]={0,0,143,69,85,49,98,200,100,13}; 
uint8_t B[10]={255,100,69,100,200,50,0,0,255,255}; 


void setup()
{
  strip.begin();
  strip.show();
}

void loop()
{
  uint8_t i;

  for(i=0;i<10;i++)
  {
     strip.setPixelColor(i,R[i],G[i],B[i]); 
   }
   strip.show();
}

Hi,

I suspect your problems are to do with this:

strip.setPixelColor(r+40, red);
 strip.setPixelColor(r+80, red);
 strip.setPixelColor(r-40, red);
 strip.setPixelColor(r-80, red);

Pixel number r+80 is ok to begin with, but as soon as r reaches 20, r+80 is 100, which does not exist (99 is the last one). From the beginning, pixel r-40 does not exist (0 is the first).

So have a look at the modulo operator. Instead of r+40 use (r+40)%100 and so on.

However, because in C, the modulo operator is really a remainder operator and reacts differently to negative numbers, if you do (r-40)%100, it won't work as expected, so instead use (r+60)%100.

Paul