Failing to understand a seemingly inconsistent error with for loops

I’ve set up a matrix of neopixels and with the push of a button I want a random pixel to light up along with a user defined number of neighboring pixels in a cross. What I’ve got so far is this:

#include <Adafruit_NeoPixel.h>

#define pixelPin 6
#define buttonPin 7

#define numPixels 80
#define stripLength 20

int vNeighbors = 2; // neighboring vertical pixels in each direction stretching from active pixel
int hNeighbors = 2; // neighboring horizontal pixels in each direction stretching from active pixel

int addVNeighbor[] = {}; int y;
int addHNeighbor[] = {}; int z;

int buttonState;
int prevState;

int currentPixel;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixels, pixelPin, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show();
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  for (int v = -vNeighbors; v < vNeighbors; v++) {
    if (v < numPixels) addVNeighbor[y] = v;
    Serial.print("v: "); Serial.print(addVNeighbor[y]); Serial.print(", ");
    y++;
  }
  Serial.println();
  
  for (int h = -hNeighbors; h < (hNeighbors + 1); h++) {
    int neighbor = stripLength * h;
    if (neighbor < numPixels) addHNeighbor[z] = neighbor;
    Serial.print("h: "); Serial.print(addHNeighbor[z]); Serial.print(", ");
    z++;
  }
  Serial.println();
}

I’ve left out the void loop because the problem appears in the void setup where the above code shows

v: -2, v: -1, v: 0, v: 1,
h: -40, h: -20, h: 0, h: 20, h: 40,

in the monitor, which makes sense, but when I add (vNeighbors + 1) in the first for loop, like so:

for (int v = -vNeighbors; v < (vNeighbors + 1); v++) {
  if (v < numPixels) addVNeighbor[y] = v;
  Serial.print("v: "); Serial.print(addVNeighbor[y]); Serial.print(", ");
  y++;
}
Serial.println();

I get an endless stream of “v:”'with no line break in the monitor and nothing else…

When I switch places on the two loops, I get

h: -40, h: -20, h: 0, h: 20, h: 40,
v: -18248, v: -18248, v: -18248, v: 184, v: 2,

in the monitor, which to me makes absolutely no sense whatsoever.

I just don’t get what’s missing, and would appreciate any help at all.

Hello and welcome,

This is not valid:

int addVNeighbor[] = {};
int addHNeighbor[] = {};

You must give a size to those arrays to reserve memory space. The only case you don't need to give a size to an array is when you fill the array at declaration, in which case the compiler will count the elements to set the size of the array.

Try change to:

int addVNeighbor[4];
int addHNeighbor[4];

See this array tutorial maybe :wink:

flaggermann:
What I've got so far is this:

You are declaring two arrays, but each of them contains zero elements:

int addVNeighbor[] = {}; int y;
int addHNeighbor[] = {}; int z;

Then you do some array buffer overflows by assigning values to non-existing elements within the array, which actually means you are overwriting random variables in RAM.

Perhaps learn some basics about array, number of elements within an array, indexing array elements and buffer overflows (= programming mistakes) first.

In an array which actually has zero elements declared, it is not even possible to use the first element like 'addVNeighbor[0]'.

Hey again and thanks for the feedback!

The problem was that the size of the array would vary, but I changed it to sizeof(addVNeighbor)/sizeof(int), and that worked. What threw me off was that it worked some of the times but not consistently. That being said, there were a couple of other things I changed, and now it works more or less.

I have, however run into a couple of other problems if anybody would like to have a look here

Again, thank you

flaggermann:
What threw me off was that it worked some of the times but not consistently.

That's the beauty of buffer overflow problems; they are consistently inconsistent.