Strange outputs when programming Neopixels using "for loop" and arrays

hi guys.
ive created this sketch which generates generates a random RGB value for the first neopixel in the strip then the code makes the RGB value (stored in an array) shunt along to the next pixel in the strip, this then repeats creating scrolling effect.

however while the first neopixel is working fine i.e. flash random colours.
The scrolling pixels start off only displaying blue, then when the blue reach the end of the strip, the green is added and when the green & blue reach the end of the strip the red is added so it is finely doing what i was expecting in the first place.

can someone pleas explain what is going on that causes this?

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>

int number_Pixels = 30; //set number ofLEDs on strip
int RedBri[30] = {}; 
int GreenBri[30]={};
int BlueBri[30]= {};
int i = number_Pixels; //
int r; //couter value 0-2 for random generator.
long prox; //holds random value
int ran[3]={0,0,0};
int pr;
int d = 0;
int alt = 1;
const int PIN = 6;  // Which pin on the Arduino is connected to the NeoPixels?

// 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(number_Pixels, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
pixels.begin(); // This initializes the NeoPixel library.
pixels.show(); // Initialize all pixels to 'off'  
Serial.begin(9600); 
randomSeed(analogRead(0)); // generater random value using noise from the analog read pin
}


void loop() {
for(r=0; r<=3; r++){ //loop to generater 3 different random numbers and store in array
prox = random(0,50);
ran[r]= prox;
}

RedBri[0]    = ran[0];  // assign random value for brightness of red the LED of Neopixel zero
GreenBri[0]  = ran[1];  // assign random value for brightness of green the LED of Neopixel zero
BlueBri[0]   = ran[2];  // assign random value for brightness of blue the LED of Neopixel zero

pixels.setPixelColor(0, pixels.Color(RedBri[0], GreenBri[0], BlueBri[0]));//set brightness value for RGB of Neopixel zero

for(i=number_Pixels; i>0; i--){ // coutdown from last led too led zero

RedBri[i]  =RedBri[i-1];    //set red LED brightness with value from the led in front from the preveus cycal 
GreenBri[i]=GreenBri[i-1];  //set green LED brightness with value from the led in front from the preveus cycal 
BlueBri[i] =BlueBri[i-1];   //set blue LED brightness with value from the led in front from the preveus cycal 

pixels.setPixelColor(i, pixels.Color(RedBri[i], GreenBri[i], BlueBri[i])); //set brightness value for RGB of Neopixel one too (number of pixels)
  
}

pixels.show(); // This sends the updated pixel color to the hardware.
/*
Serial.println(RedBri[0]);   //********TEST*********
Serial.println(GreenBri[0]); //********TEST*********
Serial.println(BlueBri[0]);  //********TEST*********
Serial.println(" ");         //********TEST********
*/

delay(50);
}

Try increasing the delay in loop() and see what's happening.

I already can see what is happening both on the neopixel strip and the serial monitor but dont understand why it is happening.

(edit) i did put a delay in the loop as u suggested, but as i said it didn't really reveal anything new.

Can you post a video? It's not clear what you want to do.

this loop writes four values:

for(r=0; r<=3; r++){ //loop to generater 3 different random numbers and store in array
prox = random(0,50);
ran[r]= prox;
}

you need to fix that

this loop writes beyond the last member of the array as well:

for(i=number_Pixels; i>0; i--){ // coutdown from last led too led zero

RedBri[i]  =RedBri[i-1];    //set red LED brightness with value from the led in front from the preveus cycal 
GreenBri[i]=GreenBri[i-1];  //set green LED brightness with value from the led in front from the preveus cycal 
BlueBri[i] =BlueBri[i-1];   //set blue LED brightness with value from the led in front from the preveus cycal 

pixels.setPixelColor(i, pixels.Color(RedBri[i], GreenBri[i], BlueBri[i])); //set brightness value for RGB of Neopixel one too (number of pixels)

arrays of size n have n elements numbered zero through n - 1

Not sure it's related to your problem, but this is wrong.

for (r = 0; r <= 3; r++) {

It's placing a value in a memory location that's not part of the array. Probably clobbering another variable. Should be:

for (r = 0; r<3; r++) {

Also, I don’t understand why your 'for' loop is counting DOWN from the end of the strip rather than UP from the beginning. The way it is, you're using elements of the array that have not yet been assigned a value. Why don't you try:

for (i =1; i <(number_Pixels-1); i++) {

gfvalvo:
Also, I don’t understand why your 'for' loop is counting DOWN from the end of the strip rather than UP from the beginning. The way it is, you're using elements of the array that have not yet been assigned a value. Why don't you try:for (i =1; i <( number_Pixels-1); i++) {

oopsie

Thanks guys.

i should have spotted both those mistakes myself really
:blush:
You were right BulldogLowell it was " i=number_Pixels " that was causing the strange outputs. I have now corrected it so it reads " i=(number_Pixels-1) " and it works as it was supposed to.

You don't happen to know why that error would cause it to behave the way it did do you? I was more interested in understanding the error than fixing it anyway.

@ gfvalvo
I assumed, and it seems to be the case, that the unassigned values in an array are zero by default. so that's not a problem.
The reason the loop is counting down is because the code has to retrieve a value from the array then it assign a new value to that location.

SavageRodent:
You don't happen to know why that error would cause it to behave the way it did do you? I was more interested in understanding the error than fixing it anyway.

when you write beyond the end of an array, you may be writing on some other data. that causes "undefined" behavior!

SavageRodent:
You don't happen to know why that error would cause it to behave the way it did do you? I was more interested in understanding the error than fixing it anyway.

It's more of a case of understanding 'your' error. The error that you made.

As someone mentioned already.....writing beyond the end of an array can mean overwriting data that might be reserved for other variables....so could end up undesirably modifying other data. Using an array index larger than the pre-defined maximum index can lead to software problems too. Things like that.

Southpark:
It's more of a case of understanding 'your' error. The error that you made.

I already know what my error was and understand about overwriting data. It was just a silly mistake.

But i don't think that's a reason not to take an interest in trying to understand what specifically is going on inside the micro-controller to result in such odd outputs. Maybe it is impossible to extrapolate what's going on, but there's no harm in asking :slight_smile: