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.