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:
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. : )