I am trying to create a multidimensional array and print it line by line; I am expecting 3 columns with 21 rows - was hoping to get any pointers on why I am not seeing that using the below code - my purpose for this code is (scaled up) to use in an RGB LED project to contain 0-255 values. Any pointers appreciated!
*I checked the baud rate of the serial monitor; they match
int c;
int r;
int M[21][3];
int val;
void setup() {
Serial.print(9600);
for (c = 1; c <= 3; c=c+1) {
val = 1;
for (r = 0; r <= 256; r=r+1) {
M[r][c] = val;
val=val+1;
}
}
}
void loop() {
for (c = 1; c <= 3; c=c+1) {
for (r = 0; r <= 20; r=r+1) {
Serial.println(M[r][c]);
}
}
}
I am having a tough time figuring out what's wrong with my basic code - it will take me far longer to even interpret yours.
does the issue in my code stand out?
You have significant issues with your indices. c should go from 0 to 2 and r should go from 0 to 20. You are exceeding the boundaries of the array which can cause all kinds of weird issues since you are overwriting memory that you have not allocated.
for (c = 1; c <= 3; c=c+1) {
val = 1;
for (r = 0; r <= 256; r=r+1) {
M[r][c] = val;
val=val+1;
}
}
Here is how the code should probably look:
int c;
int r;
int M[21][3];
int val;
void setup() {
Serial.print(9600);
for (c = 0; c < 3; c=c+1) {
val = 1;
for (r = 0; r < 21; r=r+1) {
M[r][c] = val;
val=val+1;
}
}
}
void loop() {
for (c = 0; c < 3; c=c+1) {
for (r = 0; r < 21; r=r+1) {
Serial.println(M[r][c]);
}
}
}