I think my code is correct but it doesn't work

Hello, I am trying to make a program to write to 2d array.
I want to print my variables and something like bigger this to my serial port:

ar

But it doesn't work. In two seconds it printed � and it stoped printing.
This is my code:

const byte mapsizeX = 10;
const byte mapsizeY = 10;

byte gmap[mapsizeX][mapsizeY]{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 3, 3, 3, 3, 3, 1, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 1, 3, 3, 1},
{1, 3, 1, 1, 3, 3, 1, 3, 3, 1},
{1, 3, 1, 1, 3, 3, 1, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
}; //mapa zahrady

int pmap[mapsizeX][mapsizeY]{};

byte tarX = 3;
byte tarY = 7;

byte posX = 2;
byte posY = 3;



void pmapcreate(){
  bool fwork = true;
  byte fP = 0;
  int val = mapsizeX * mapsizeY;
  byte out[32][3]{};
  byte in[32][3]{};
  byte outv = 0;
  byte inv = 0;

  while(fwork == true){
    Serial.println(val);
    switch(fP){
      case 0: //clear pmap
        for(byte x = 0; x < mapsizeX; x++){
          for(byte y = 0; y < mapsizeY; y++){
            pmap[x][y] = 0;
          }
        }
        pmap[tarX][tarY] = val;
        out[0][0] = tarX;
        out[0][1] = tarY;
        out[0][2] = 1;
        fP = 2;
        break;
      case 1: //copy in to out
        for(byte c = 0; out[c][2] == 1; c++){
          out[c][0] = 0;
          out[c][1] = 0;
          out[c][2] = 0;
        }
        for(byte copy = 0; in[copy][2] == 1; copy++){
          out[copy][0] = in[copy][0];
          out[copy][1] = in[copy][1];
          out[copy][2] = in[copy][2];
          in[copy][0] = 0;
          in[copy][1] = 0;
          in[copy][2] = 0;
        }
        fP = 2;
        inv = 0;
        outv = 0;
        val--;
        break;
      case 2: //write to pmap
        for(outv = 0; out[outv][2] == 1; outv++){
          if(((gmap[out[outv][0] - 1][out[outv][1]] == 3) || (gmap[out[outv][0] - 1][out[outv][1]] == 2) || (gmap[out[outv][0] - 1][out[outv][1]] == 4)) && (pmap[out[outv][0] - 1][out[outv][1]] < val)){
            pmap[out[outv][0] - 1][out[outv][1]] = val - 1;
            in[inv][0] = out[outv][0] - 1;
            in[inv][1] = out[outv][1];
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] - 1 == posX) && (out[outv][1] == posY)){
              fwork = false;
            }
          }
          if(((gmap[out[outv][0]][out[outv][1] + 1] == 3) || (gmap[out[outv][0]][out[outv][1] + 1] == 2) || (gmap[out[outv][0]][out[outv][1] + 1] == 4)) && (pmap[out[outv][0]][out[outv][1] + 1] < val)){
            pmap[out[outv][0]][out[outv][1] + 1] = val - 1;
            in[inv][0] = out[outv][0];
            in[inv][1] = out[outv][1] + 1;
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] == posX) && (out[outv][1] + 1 == posY)){
              fwork = false;
            }
          }
          if(((gmap[out[outv][0] + 1][out[outv][1]] == 3) || (gmap[out[outv][0] + 1][out[outv][1]] == 2) || (gmap[out[outv][0] + 1][out[outv][1]] == 4)) && (pmap[out[outv][0] + 1][out[outv][1]] < val)){
            pmap[out[outv][0] + 1][out[outv][1]] = val - 1;
            in[inv][0] = out[outv][0] + 1;
            in[inv][1] = out[outv][1];
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] + 1 == posX) && (out[outv][1] == posY)){
              fwork = false;
            }
          }
          if(((gmap[out[outv][0]][out[outv][1] - 1] == 3) || (gmap[out[outv][0]][out[outv][1] - 1] == 2) || (gmap[out[outv][0]][out[outv][1] - 1] == 4)) && (pmap[out[outv][0]][out[outv][1] - 1] < val)){
            pmap[out[outv][0]][out[outv][1] - 1] = val - 1;
            in[inv][0] = out[outv][0];
            in[inv][1] = out[outv][1] - 1;
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] == posX) && (out[outv][1] - 1 == posY)){
              fwork = false;
            }
          }
        }
        fP = 1;
        break;
    }
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("setup");

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("loop");
pmapcreate();
for(byte x = 0; x <= mapsizeX; x++){
  for(byte y = 0; y <= mapsizeY; y++){
    Serial.println(pmap[x][y]);
  }
}
}

I think, I have no error in code, but I'm new here, so I thanks you so much for any answers. : )

1 Like

Good place to start would be read details at the top of the forum page.

1 Like

These lines address array elements that do not exist. So no, the code is not correct.

1 Like

Hello, I didn't read the whole code, but I believe that the mistake could be in loop(), more specifically in this part:

for(byte x = 0; x <= mapsizeX; x++){
  for(byte y = 0; y <= mapsizeY; y++){
    Serial.println(pmap[x][y]);
  }
}

I believe it should look like this:

for(byte x = 0; x < mapsizeX; x++){
  for(byte y = 0; y < mapsizeY; y++){
    Serial.println(pmap[x][y]);
  }
}

(the difference is <= and < sign in for loops; it basically tried to access a nonexistent element of the array, hence it printed �, because it accessed a random memory location that was used by another part of the program or another program entirely)
Please try it and tell me if it worked.
All best, V

1 Like

Of course not. It's likely the compiler....

And using no comments makes Your coding much faster, but harder, or impossible, to read for stupid helpers.

1 Like

Thank you all so much for fast help.
Thank you for notice error.
I tried to correct my code, but it is same.
Here is screenshot of my serial monitor when arduino stopped printing:

ar2

I think there is another and maybie bigger error then you noticed.
I am using arduino uno but it's clon, so I think it can be error in compiling. But I am beginner and I don't know much.

Thank you all for answers. :slight_smile:

Which IDE are you using? Maybe it is software

I am using Arduino IDE 2.0.0

Please post your revised sketch

This is my edited code:



const byte mapsizeX = 10;
const byte mapsizeY = 10;

byte gmap[mapsizeX][mapsizeY]{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 3, 3, 3, 3, 3, 1, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 1, 3, 3, 1},
{1, 3, 1, 1, 3, 3, 1, 3, 3, 1},
{1, 3, 1, 1, 3, 3, 1, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
}; 

int pmap[mapsizeX][mapsizeY]{};

byte tarX = 3;
byte tarY = 7;

byte posX = 2;
byte posY = 3;



void pmapcreate(){
  bool fwork = true;
  byte fP = 0;
  int val = mapsizeX * mapsizeY;
  byte out[32][3]{};
  byte in[32][3]{};
  byte outv = 0;
  byte inv = 0;

  while(fwork == true){
    Serial.println(val);
    switch(fP){
      case 0: //clear pmap
        for(byte x = 0; x < mapsizeX; x++){
          for(byte y = 0; y < mapsizeY; y++){
            pmap[x][y] = 0;
          }
        }
        pmap[tarX][tarY] = val;
        out[0][0] = tarX;
        out[0][1] = tarY;
        out[0][2] = 1;
        fP = 2;
        break;
      case 1: //copy in to out
        for(byte c = 0; out[c][2] == 1; c++){
          out[c][0] = 0;
          out[c][1] = 0;
          out[c][2] = 0;
        }
        for(byte copy = 0; in[copy][2] == 1; copy++){
          out[copy][0] = in[copy][0];
          out[copy][1] = in[copy][1];
          out[copy][2] = in[copy][2];
          in[copy][0] = 0;
          in[copy][1] = 0;
          in[copy][2] = 0;
        }
        fP = 2;
        inv = 0;
        outv = 0;
        val--;
        break;
      case 2: //write into pmap
        for(outv = 0; out[outv][2] == 1; outv++){
          if(((gmap[out[outv][0] - 1][out[outv][1]] == 3) || (gmap[out[outv][0] - 1][out[outv][1]] == 2) || (gmap[out[outv][0] - 1][out[outv][1]] == 4)) && (pmap[out[outv][0] - 1][out[outv][1]] < val)){
            pmap[out[outv][0] - 1][out[outv][1]] = val - 1;
            in[inv][0] = out[outv][0] - 1;
            in[inv][1] = out[outv][1];
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] - 1 == posX) && (out[outv][1] == posY)){
              fwork = false;
            }
          }
          if(((gmap[out[outv][0]][out[outv][1] + 1] == 3) || (gmap[out[outv][0]][out[outv][1] + 1] == 2) || (gmap[out[outv][0]][out[outv][1] + 1] == 4)) && (pmap[out[outv][0]][out[outv][1] + 1] < val)){
            pmap[out[outv][0]][out[outv][1] + 1] = val - 1;
            in[inv][0] = out[outv][0];
            in[inv][1] = out[outv][1] + 1;
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] == posX) && (out[outv][1] + 1 == posY)){
              fwork = false;
            }
          }
          if(((gmap[out[outv][0] + 1][out[outv][1]] == 3) || (gmap[out[outv][0] + 1][out[outv][1]] == 2) || (gmap[out[outv][0] + 1][out[outv][1]] == 4)) && (pmap[out[outv][0] + 1][out[outv][1]] < val)){
            pmap[out[outv][0] + 1][out[outv][1]] = val - 1;
            in[inv][0] = out[outv][0] + 1;
            in[inv][1] = out[outv][1];
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] + 1 == posX) && (out[outv][1] == posY)){
              fwork = false;
            }
          }
          if(((gmap[out[outv][0]][out[outv][1] - 1] == 3) || (gmap[out[outv][0]][out[outv][1] - 1] == 2) || (gmap[out[outv][0]][out[outv][1] - 1] == 4)) && (pmap[out[outv][0]][out[outv][1] - 1] < val)){
            pmap[out[outv][0]][out[outv][1] - 1] = val - 1;
            in[inv][0] = out[outv][0];
            in[inv][1] = out[outv][1] - 1;
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] == posX) && (out[outv][1] - 1 == posY)){
              fwork = false;
            }
          }
        }
        fP = 1;
        break;
    }
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("setup");

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("loop");
pmapcreate();
for(byte x = 0; x < mapsizeX; x++){
  for(byte y = 0; y < mapsizeY; y++){
    Serial.println(pmap[x][y]);
  }
}
}

1 Like

The problem could be here. And there could be more similar or identical cases. Basically, if out[outv][0] is equal to 0, then out[outv][0] - 1 is equal to -1, and the address you are trying to access would look like this:

gmap [-1] [out[outv][1]]

As you can see, this is not ok.
(just so it's easier for you, this is from line 89 of the last code that you posted)
Also, you might consider using while loops instead of for loops in some places. For loop is used when we know the number of iterations up front, and while loop is used when we don't know that; in some places in this code while loop is more suitable (I would use while loop instead of both for loops in case 1 and instead of for loop in case 2 ).

1 Like

Running on Wokwi does not exhibit the serial output corruption.

Adding some debugging output gives

setup
loop
val=100	fP=0	inv=0	outv=0
val=100	fP=2	inv=0	outv=0
val=100	fP=1	inv=3	outv=1
val=99	fP=2	inv=0	outv=0
val=99	fP=1	inv=6	outv=3
val=98	fP=2	inv=0	outv=0
val=98	fP=1	inv=8	outv=6
val=97	fP=2	inv=0	outv=0
val=97	fP=1	inv=8	outv=8
val=96	fP=2	inv=0	outv=0
val=96	fP=1	inv=13	outv=8
val=95	fP=2	inv=0	outv=0
val=95	fP=1	inv=14	outv=13
val=94	fP=2	inv=0	outv=0
val=94	fP=1	inv=20	outv=14
val=93	fP=2	inv=0	outv=0
val=93	fP=1	inv=37	outv=20     ◀◀◀ note value of inv
val=92	fP=2	inv=0	outv=0
val=92	fP=1	inv=0	outv=0
val=91	fP=2	inv=0	outv=0
val=91	fP=1	inv=0	outv=0
val=90	fP=2	inv=0	outv=0
val=90	fP=1	inv=0	outv=0
.
.

with ever decreasing values for val which will eventually get negative.

1 Like

@oqibidipo Yes, this is correct :+1:, but the goal is to print pmap in void loop() using

for(byte x = 0; x < mapsizeX; x++){
  for(byte y = 0; y < mapsizeY; y++){
    Serial.println(pmap[x][y]);
  }
}

However, this is somehow stuck in a never-ending loop that will never print it, it can only generate these values that you have shown. In other words, fwork will never become false, so it is stuck in a forever while loop that is located in pmapcreate(). I concluded this after running it for more than 2 minutes using your described method. I still think that part of the problem might be what I described in my previous post.

1 Like

Hello all, thank you all so much. I tried edit my code and it finally works. Thank you all for notice me for all errors. The pmap variable is navigation map for my homemade robotic mower. It's beautiful there are guys like you, who helps another less experienced guys with theirs problems. Thank you all. :slight_smile:

This is my final code:

const byte mapsizeX = 10;
const byte mapsizeY = 10;

byte gmap[mapsizeX][mapsizeY]{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 3, 3, 3, 3, 3, 1, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 1, 3, 3, 1},
{1, 3, 1, 1, 3, 3, 1, 3, 3, 1},
{1, 3, 1, 1, 3, 3, 1, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 3, 3, 3, 3, 1},
{1, 3, 3, 3, 3, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
}; //garden map

int pmap[mapsizeX][mapsizeY]{};

byte tarX = 3;
byte tarY = 7;

byte posX = 2;
byte posY = 3;



void pmapcreate(){
  bool fwork = true;
  byte fP = 0;
  int val = mapsizeX * mapsizeY;
  byte out[125][3]{};
  byte in[125][3]{};
  byte outv = 0;
  byte inv = 0;

  while(fwork == true){
    Serial.println(val);
    switch(fP){
      case 0: //clear pmap
        for(byte x = 0; x < mapsizeX; x++){
          for(byte y = 0; y < mapsizeY; y++){
            pmap[x][y] = 0;
          }
        }
        pmap[tarX][tarY] = val;
        out[0][0] = tarX;
        out[0][1] = tarY;
        out[0][2] = 1;
        fP = 2;
        break;
      case 1: //copy in to out
        for(byte c = 0; out[c][2] == 1; c++){
          out[c][0] = 0;
          out[c][1] = 0;
          out[c][2] = 0;
        }
        for(byte copy = 0; in[copy][2] == 1; copy++){
          out[copy][0] = in[copy][0];
          out[copy][1] = in[copy][1];
          out[copy][2] = in[copy][2];
          in[copy][0] = 0;
          in[copy][1] = 0;
          in[copy][2] = 0;
        }
        fP = 2;
        inv = 0;
        outv = 0;
        val--;
        break;
      case 2: //write to pmap
        for(outv = 0; out[outv][2] == 1; outv++){
          if(((gmap[out[outv][0] - 1][out[outv][1]] == 3) || (gmap[out[outv][0] - 1][out[outv][1]] == 2) || (gmap[out[outv][0] - 1][out[outv][1]] == 4)) && (pmap[out[outv][0] - 1][out[outv][1]] < val)){
            pmap[out[outv][0] - 1][out[outv][1]] = val - 1;
            in[inv][0] = out[outv][0] - 1;
            in[inv][1] = out[outv][1];
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] - 1 == posX) && (out[outv][1] == posY)){
              fwork = false;
              break;
            }
          }
          if(((gmap[out[outv][0]][out[outv][1] + 1] == 3) || (gmap[out[outv][0]][out[outv][1] + 1] == 2) || (gmap[out[outv][0]][out[outv][1] + 1] == 4)) && (pmap[out[outv][0]][out[outv][1] + 1] < val)){
            pmap[out[outv][0]][out[outv][1] + 1] = val - 1;
            in[inv][0] = out[outv][0];
            in[inv][1] = out[outv][1] + 1;
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] == posX) && (out[outv][1] + 1 == posY)){
              fwork = false;
              break;
            }
          }
          if(((gmap[out[outv][0] + 1][out[outv][1]] == 3) || (gmap[out[outv][0] + 1][out[outv][1]] == 2) || (gmap[out[outv][0] + 1][out[outv][1]] == 4)) && (pmap[out[outv][0] + 1][out[outv][1]] < val)){
            pmap[out[outv][0] + 1][out[outv][1]] = val - 1;
            in[inv][0] = out[outv][0] + 1;
            in[inv][1] = out[outv][1];
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] + 1 == posX) && (out[outv][1] == posY)){
              fwork = false;
              break;
            }
          }
          if(((gmap[out[outv][0]][out[outv][1] - 1] == 3) || (gmap[out[outv][0]][out[outv][1] - 1] == 2) || (gmap[out[outv][0]][out[outv][1] - 1] == 4)) && (pmap[out[outv][0]][out[outv][1] - 1] < val)){
            pmap[out[outv][0]][out[outv][1] - 1] = val - 1;
            in[inv][0] = out[outv][0];
            in[inv][1] = out[outv][1] - 1;
            in[inv][2] = 1;
            inv++;
            if((out[outv][0] == posX) && (out[outv][1] - 1 == posY)){
              fwork = false;
              break;
            }
          }
        }
        fP = 1;
        break;
    }
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("setup");

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("loop");
pmapcreate();
for(byte x = 0; x < mapsizeX; x++){
  Serial.println();
  for(byte y = 0; y < mapsizeY; y++){
    if(pmap[x][y] < 10){
      Serial.print(" ");}
    Serial.print(pmap[x][y]);
    Serial.print("|");
  }
}
delay(10000);
}

1 Like

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