Mapping robot travel path to txt file

Hi I am trying to implement a code where I take an x and y array and outputs into a simple x and y graph text file. Problem is, the indices I try to call do not match the numbers in the coord array. I have tried several iterations, which are listed below, but have not been able to find a solution. Should I just give up and use python at this point?

Here is one attempt:

#include <SPI.h>
#include <SD.h>
File myFile;
char myArray[10][4]; //output array

int coord[2][10] = {
{0, 1, 2, 3, 3, 4, 5, 5, 5, 5}, //x axis 
{0, 0, 1, 1, 2, 2, 2, 3, 4, 5} //y axis 
};

void setup() {
  // SD card check
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  if (!SD.begin(10))
  {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

 //Outputs an array with the blank places as . and occupied places as #

for(int x = 0; x < 10; x++){     //x axis
  for(int y = 0; y < 10; y++){   //y axis
    myArray[x][y]='.';
    if (x=y)
    {
    //Serial.print(coord[x][y]); //For debugging purposes
    myArray[(coord[0][x])][(coord[1][y])] ='#';
    }
    
    Serial.print(myArray[x][y]);
   }
Serial.println();

 }

 }


/*myFile = SD.open("map.txt", FILE_WRITE);
if (myFile)
{
  myFile.print(
  for (int i=10; i>0; i--)    //i equals whatever the length of the array is
  {
   for(int i = 0; i < coord[1][10]; i--){
    for(int j = 0; j < coord[2][10]; j--){
      myFile.print(i)
    }}
myFile.close();
}}
else 
{
  Serial.println("Error opening map.txt");
}
}
*/
void loop() {
  // nothing happens after setup
}

Here is another attempt:

#include <SPI.h>
#include <SD.h>
File myFile;
char myArray[10][4]; //output array

int coord[2][10] = {
{0, 1, 2, 3, 3, 4, 5, 5, 5, 5}, //x axis 
{0, 0, 1, 1, 2, 2, 2, 3, 4,5} //y axis 
};

void setup() {
  // SD card check
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  if (!SD.begin(10))
  {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

 //Outputs an array with the blank places as . and occupied places as #

for(int x = 0; x < 10; x++){     //x axis
  for(int y = 0; y < 10; y++){   //y axis
    myArray[x][y]='.';
    if ((coord[0][x]==x)&&(coord[1][y]==y))
    {
    //Serial.print(coord[x][y]); //For debugging purposes
    myArray[x][y] ='#';
    }
    
    Serial.print(myArray[x][y]);
   }
Serial.println();

 }

 }

/*myFile = SD.open("map.txt", FILE_WRITE);
if (myFile)
{
  myFile.print(
  for (int i=10; i>0; i--)    //i equals whatever the length of the array is
  {
   for(int i = 0; i < coord[1][10]; i--){
    for(int j = 0; j < coord[2][10]; j--){
      myFile.print(i)
    }}
myFile.close();
}}
else 
{
  Serial.println("Error opening map.txt");
}
}
*/
void loop() {
  // nothing happens after setup
}

Finally, I also tried three for loops, but that did not work so well.

for(int x = 0; x < 10; x++){     //x axis
  for(int y = 0; y < 10; y++){   //y axis
    myArray[x][y]='.';

If you are used to interpreted languages, one of the hardest things to grasp about C is that it won’t complain about writing to non-existent array locations
You have to remember not to do that.

      if (x = y)

BONG !

If you break it down a bit is this the result your looking for?

#.........
#.........
.#........
.##.......
..#.......
..####....
..........
..........
..........
..........
char myArray[10][10]; //output array

int coord[2][10] = 
{
  {0, 1, 2, 3, 3, 4, 5, 5, 5, 5}, //x axis
  {0, 0, 1, 1, 2, 2, 2, 3, 4,5} //y axis
};

void setup() 
{
  Serial.begin(115200);
  //Outputs an array with the blank places as . and occupied places as #
  
  for(int x = 0; x < 10; x++){     //x axis
    for(int y = 0; y < 10; y++){   //y axis
      myArray[x][y]='.';
    }
  }
  
  for(int z = 0; z < 10; z++){     //array axis
    myArray[coord[0][z]][coord[1][z]] ='#';
  }
  
  
  for(int x = 0; x < 10; x++){     //x axis
    for(int y = 0; y < 10; y++){   //y axis
      Serial.print(myArray[x][y]);
    }
    Serial.println();
  }
  
  
}

void loop() 
{
  // nothing happens after setup
}

That is exactly what I am looking for. Thanks for all your help! Riva, would you happen to know how to invert the matrix so that 5 goes to the front and 1 goes to back and so on. I just want to invert the # and . map.

Riva:
If you break it down a bit is this the result your looking for?

#.........

#…
.#…
.##…
…#…
…####…











char myArray[10][10]; //output array

int coord[2][10] =
{
  {0, 1, 2, 3, 3, 4, 5, 5, 5, 5}, //x axis
  {0, 0, 1, 1, 2, 2, 2, 3, 4,5} //y axis
};

void setup()
{
  Serial.begin(115200);
  //Outputs an array with the blank places as . and occupied places as #
 
  for(int x = 0; x < 10; x++){    //x axis
    for(int y = 0; y < 10; y++){  //y axis
      myArray[y]=’.’;
    }
  }
 
  for(int z = 0; z < 10; z++){    //array axis
    myArray[coord[0][z]][coord[1][z]] =’#’;
  }
 
 
  for(int x = 0; x < 10; x++){    //x axis
    for(int y = 0; y < 10; y++){  //y axis
      Serial.print(myArray[y]);
    }
    Serial.println();
  }
 
 
}

void loop()
{
  // nothing happens after setup
}

My next problem is that I am trying to invert and flip the map so that (0,0) is on the bottom left side. Here is what I’ve tried, and it makes logical sense in my mind, but I can’t get it to work on the serial monitor.

char myArray[10][10]; //output array

int coord[2][10] = //sample array, it will be a fixed array in the actual code!
{
  {0, 1, 2, 3, 3, 4, 5, 5, 5, 5}, //x axis
  {0, 0, 1, 1, 2, 2, 2, 3, 4, 5} //y axis
};

void setup()
{
  Serial.begin(115200);
  //Outputs an array with the blank places as . and occupied places as #

  for (int y = 0; y < 10; y++) {   //y axis
    for (int x = 0; x < 10; x++) { //x axis
      myArray[x][y] = '.';
    }
  }

  for (int z = 0; z < 10; z++) {   //array axis
    myArray[(5-coord[0][z])][(5-coord[1][z])] = '#'; //invert matrix, 5=largest number in coord array
  }


  for (int y = 0; y < 10; y++) {   //y axis
    for (int x = 10; x > 0; x--) { //x axis (returns funny map?)
      Serial.print(myArray[x][y]);
    }
    Serial.println();
  }
}

void loop()
{
  // nothing happens after setup
}

I’m not able to test this but maybe the below code? I have only flipped the x axis for now but doing the y axis instead/as well is just the same.

  for(int x = 9; x >= 0; x--){     //x axis
    for(int y = 0; y < 10; y++){   //y axis
      Serial.print(myArray[x][y]);
    }
    Serial.println();
  }

From reply 5for (int x = 10;
Neither axis has an element with the subscript 10.