Serial Print issue with multidimensional arrays: Troubleshoot

Hi

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]);


    }
  }
}
static const int   NbrOfCodesThisDevice = 5;
static const int   CodeArrayLength = 31;
static const char  ThingsCodes[NbrOfCodesThisDevice][CodeArrayLength] =
{
  "25Turn on Sconce",
  "26Turn on all night",
  "27Turn off motion sensor",
  "28Turn off automatic functions",
  "29Reset Device"
};

void setup() {
  // put your setup code here, to run once:


  Serial.begin(115200); Serial.println();
  int intCode;
  char descrip[29];
  for (int i = 0; i < NbrOfCodesThisDevice; i++) {
    intCode = ThingsCodes[i][0] - 48;
    intCode *= 10;
    intCode += ThingsCodes[i][1] - 48;
    for (int j = 2; j < CodeArrayLength; j++) {
      if (ThingsCodes[i][j] == '\0') {
        //Serial.println("break");
        descrip[j - 2] = '\0';
        break;
      }
      descrip[j - 2] = ThingsCodes[i][j];
    }
    Serial.print(i + 1); Serial.print(". ");
    Serial.print("intcode="); Serial.print(intCode);
    Serial.print(" descrip="); Serial.println(descrip);
  }
}
void loop() {
   

}

one I worked on yesterday.

Hi sevenoutpinball:

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. :smiley:
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]);
    }
  }
}

This makes every print on a new line

Serial.println(M[r][c]);

You need to change to print 3 times before the next line.

Perhaps increment by 3, and then

Serial.print(M[r][c]); 
Serial.print (" ");
Serial.print(M[r][c+1]);
Serial.print (" ");
Serial.println(M[r][c+2]);

Just noticed this:

void setup() {
  Serial.print(9600);

should be:

void setup() {
  Serial.begin(9600);

That is why you are not getting any serial output.

at first glance I think you reversed row and column. figuring all this out is 90% of the fun.

ToddL1962:
Just noticed this:

void setup() {

Serial.print(9600);




should be:



void setup() {
 Serial.begin(9600);




That is why you are not getting any serial output.

wow;
just wow... thank you, can't believe I missed that!

CrossRoads:
This makes every print on a new line

Serial.println(M[r][c]);

You need to change to print 3 times before the next line.

Perhaps increment by 3, and then

Serial.print(M[r][c]); 

Serial.print (" ");
Serial.print(M[r][c+1]);
Serial.print (" ");
Serial.println(M[r][c+2]);

works like a charm, thank you! :smiley:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.