Loops altering Array Contents Unintentionally

Okay, I have a piece of code which should output a 16 by 16 matrix of the value 255. When running the following code, these values are outputted and the calculations run as normal.

However if I try to repeat the last few steps using a whiule loop (see the lower section of the code), the 16 by 16 matrix is altered, seemingly at random without any writes to it.

Ie. If I run the code as below the first serial output is:

Nodes 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255

which is desired, but adding the while loop changes the first serial output to:

Nodes 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 -16384 0 1280 320 1280 1729 2531 1283 257 255 255 255 255 255 255 255 255 255 255 255 255 255 -3073 7 0 0 4098 -16384 0 1280 834 1280 1735 1305 1481 1733 71 159 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255

which then repeats indefinitely

Any ideas?

Any ideas?

Three, actually.

The first is that, in the future, you should post code using the # button on the top row.

The second is that you should modify your first post. Highlight all of your code, and press the # button.

The third is that you post ALL of your code, not just the portion that you think contains the error. Errors are tricky little buggers. While your looking here, they're over there. When you look over there, they hide back over here. Without seeing all of your code, we can't tell. The portion of the code that you think contains the error may not actually be the portion in error.

Here’s the code: in the new format.

// JAPAN00 Maze For testing purposes only ------------------------------------------
int HorizontalWalls[16][17]= { 
  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,
  1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,
  1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,
  1,0,0,0,1,0,1,0,1,0,1,1,0,1,0,0,1,
  1,0,0,0,1,1,0,1,0,1,0,1,0,1,0,0,1,
  1,0,0,0,1,0,0,1,1,0,1,0,0,1,0,0,1,
  1,0,0,0,1,1,1,0,0,0,1,0,0,1,0,0,1,
  1,0,0,0,0,0,1,1,0,1,1,0,0,1,0,0,1,
  1,0,0,1,0,0,1,0,0,0,1,0,1,0,0,0,1,
  1,0,0,1,1,0,1,0,1,1,0,1,1,0,1,0,1,
  1,0,0,1,1,0,1,0,1,1,1,0,1,0,0,0,1,
  1,0,0,1,0,0,1,0,1,0,1,1,0,0,0,1,1,
  1,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,1,
  1,0,0,0,1,0,1,0,0,1,0,0,0,0,0,1,1,
  1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1};

int VerticalWalls[17][16]= { 
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
  1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,
  1,0,1,1,0,1,1,0,1,1,1,1,1,1,1,0,
  1,0,1,0,1,0,1,1,1,1,0,0,1,1,0,1,
  1,0,1,1,0,1,0,0,0,0,0,1,0,0,1,0,
  1,0,1,0,0,0,1,0,0,0,1,0,1,1,0,1,
  1,0,1,1,0,0,1,1,1,1,0,1,1,0,1,0,
  0,1,0,1,1,1,0,0,0,0,1,1,0,1,0,1,
  0,1,0,0,1,0,0,1,1,0,0,1,1,1,0,1,
  1,0,1,0,0,1,0,1,0,1,1,0,0,1,0,1,
  0,1,0,1,1,0,1,0,0,0,0,0,1,0,1,0,
  1,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0,
  0,1,0,0,1,0,1,0,1,1,0,0,1,0,1,1,
  1,0,0,1,1,1,0,1,0,0,1,1,1,1,0,0,
  0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,0,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
// END OF TESTING SECTION ------------------------------------------


//-----------------------------------------------------------------------------  
//Initialising Vairables
//Entry Coordinates
const int EntryCoordinatesX =0;
const int EntryCoordinatesY =0;

////Weightings
//const int WeightDiagonal=5;
//const int WeightStraight=4;

//Distance Node Graph
int Nodes[16][16]={
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};

// Print Nodes
void setup(){ 
  Serial.begin(9600); 

  Serial.println("");
  Serial.println("Nodes"); 
  for (int Rows=0; Rows<=15; Rows++){
    for (int Columns=0; Columns<=15; Columns++){  
      Serial.print(Nodes[Rows][Columns]);
      Serial.print("\t");
    }
    Serial.println("");
  }
  Serial.println("");




  //Change Variables (to see if the route is being updated)
  int ChangesOld=0;
  int ChangesNew=1;
  int LastLocationX=EntryCoordinatesX;
  int LastLocationY=EntryCoordinatesY;

  //Cardinal Points
  int NodeNorth=0;
  int NodeEast=0;
  int NodeSouth=0;
  int NodeWest=0;


  //Weightings

  int WeightStraight=1;


  //-----------------------------------------------------------------------------  
  // Start Weighting Grid
  Nodes[EntryCoordinatesX][EntryCoordinatesY]=0;

  for (int counter=1; counter<255; counter++){
    ChangesOld=ChangesNew;
    for (int NodeRows=0; NodeRows<=15; NodeRows++){
      for (int NodeColumns=0; NodeColumns<=15; NodeColumns++){  
        LastLocationX=NodeRows;
        LastLocationY=NodeColumns;

        //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   
        // 90 Degree Values
        if (NodeColumns==15){
          NodeNorth=750;
        }
        else{
          NodeNorth=Nodes[LastLocationX][LastLocationY+1];
        }

        if (NodeRows==15){
          NodeEast=750;
        }
        else{
          NodeEast=Nodes[LastLocationX+1][LastLocationY];
        }

        if (NodeColumns==0){
          NodeSouth=750;
        }
        else{
          NodeSouth=Nodes[LastLocationX][LastLocationY-1];
        }

        if (NodeRows==0){
          NodeWest=750;
        }
        else{
          NodeWest=Nodes[LastLocationX-1][LastLocationY];
        }

        // Weightings  
        if ((NodeNorth<=(Nodes[NodeRows][NodeColumns]-WeightStraight)
          &&(HorizontalWalls[NodeRows][NodeColumns+1]==0))){
          Nodes[NodeRows][NodeColumns]=NodeNorth+WeightStraight;
          ChangesNew=ChangesNew+1;
        }  

        if ((NodeEast<=(Nodes[NodeRows][NodeColumns]-WeightStraight)
          &&(VerticalWalls[NodeRows+1][NodeColumns]==0))){
          Nodes[NodeRows][NodeColumns]=NodeEast+WeightStraight;
          ChangesNew=ChangesNew+1;
        }  

        if ((NodeSouth<=(Nodes[NodeRows][NodeColumns]-WeightStraight)
          &&(HorizontalWalls[NodeRows][NodeColumns]==0))){
          Nodes[NodeRows][NodeColumns]=NodeSouth+WeightStraight;
          ChangesNew=ChangesNew+1;
        }  

        if ((NodeWest<=(Nodes[NodeRows][NodeColumns]-WeightStraight)
          &&(VerticalWalls[NodeRows][NodeColumns]==0))){
          Nodes[NodeRows][NodeColumns]=NodeWest+WeightStraight;
          ChangesNew=ChangesNew+1;
        }   


      }
    }
  }  



  //-----------------------------------------------------------------------------  
  // Print Solved Maze
  Serial.println("");
  Serial.println("Nodes"); 
  for (int Rows=0; Rows<=15; Rows++){
    for (int Columns=0; Columns<=15; Columns++){  
      Serial.print(Nodes[Rows][Columns]);
      Serial.print("\t");
    }
    Serial.println("");
  }
  Serial.println("");


  //-----------------------------------------------------------------------------  
  // Get Directions

  //Plot Route Vectors
  int RouteX[200]={
    0            };
  int RouteY[200]={
    0            };

  //Find Min Destination  
  RouteX[0]=7;
  RouteY[0]=7;
  int MinNodes=Nodes[7][7];
  if (Nodes[7][8]<MinNodes){
    RouteX[0]=7;
    RouteY[0]=8;
    MinNodes=Nodes[7][8];
  };
  if (Nodes[8][7]<MinNodes){
    RouteX[0]=8;
    RouteY[0]=7;
    MinNodes=Nodes[8][7];
  };
  if (Nodes[8][8]<MinNodes){
    RouteX[0]=8;
    RouteY[0]=8;
    MinNodes=Nodes[8][8];
  };

  //-----------------------------------
  //Find Route

  int CellCounter=0;
       int LowestCell=500;
//while ((RouteX[CellCounter]!=EntryCoordinatesX) && (RouteY[CellCounter]!=EntryCoordinatesY)){ //Could be || according to MATLab?

      //Find Neighbouring Cells
      if (RouteY[CellCounter]==15){
        NodeNorth=750;
      }
      else{
        NodeNorth=Nodes[RouteX[CellCounter]][RouteY[CellCounter]+1];
      }
      if (RouteX[CellCounter]==15){
        NodeEast=750;
      }
      else{
        NodeEast=Nodes[RouteX[CellCounter]+1][RouteY[CellCounter]];
      }
      if (RouteY[CellCounter]==0){
        NodeSouth=750;
      }
      else{
        NodeSouth=Nodes[RouteX[CellCounter]][RouteY[CellCounter]-1];
      }
      if (RouteX[CellCounter]==0){
        NodeWest=750;
      }
      else{
        NodeWest=Nodes[RouteX[CellCounter]-1][RouteY[CellCounter]];
      }
  
      //Find LowestCell
 LowestCell=500;
      if (NodeNorth<LowestCell && HorizontalWalls[RouteX[CellCounter]][RouteY[CellCounter]+1]==0){
       LowestCell=NodeNorth;
      }
      if (NodeEast<LowestCell && VerticalWalls[RouteX[CellCounter]+1][RouteY[CellCounter]]==0){
        LowestCell=NodeEast;
      }
      if (NodeSouth<LowestCell && HorizontalWalls[RouteX[CellCounter]][RouteY[CellCounter]]==0){
        LowestCell=NodeSouth;
      }
      if (NodeWest<LowestCell && VerticalWalls[RouteX[CellCounter]][RouteY[CellCounter]]==0){
        LowestCell=NodeWest;
      }
  
      //Find Path
  
      if (NodeNorth==LowestCell && HorizontalWalls[RouteX[CellCounter]][RouteY[CellCounter]+1]==0){
        RouteX[CellCounter+1]=RouteX[CellCounter];
        RouteY[CellCounter+1]=RouteY[CellCounter]+1;
      }
      else
        if (NodeEast==LowestCell && VerticalWalls[RouteX[CellCounter]+1][RouteY[CellCounter]]==0){
          RouteX[CellCounter+1]=RouteX[CellCounter]+1;
          RouteY[CellCounter+1]= RouteY[CellCounter];
        }
        else
          if (NodeSouth==LowestCell && HorizontalWalls[RouteX[CellCounter]][RouteY[CellCounter]]==0){
            RouteX[CellCounter+1]=RouteX[CellCounter];
            RouteY[CellCounter+1]=RouteY[CellCounter]-1;
          }
          else
            if (NodeWest==LowestCell && VerticalWalls[RouteX[CellCounter]][RouteY[CellCounter]]==0){
              RouteX[CellCounter+1]=RouteX[CellCounter]-1;
              RouteY[CellCounter+1]=RouteY[CellCounter];
            }



  //Increase Counter Value
   CellCounter+=1;

//}





  Serial.println("");
  Serial.println("X Vector"); 
  Serial.print(RouteX[0]);
  Serial.print("\t");
  Serial.print(RouteX[1]);
  Serial.print("\t");
  Serial.print(RouteX[2]);
  Serial.print("\t");
  Serial.print(RouteX[3]);
  Serial.print("\t");
  Serial.print(RouteX[4]);
  Serial.print("\t");
  Serial.println("");
} 


void loop(){
}

Well, now I'm confused. You show two blocks of output. One is produced before you do any manipulation to the array, and is correct. The other is after manipulating the array, and you think that the problem is in the code that outputs the array.

Or, did I miss something?

Why is everything happening in the setup?

If you only need to store a binary value, you should not use ints.
16172 = 544 bytes

You could represent the same data using 34 bytes, that’s 6,25% RAM!
:slight_smile:

What Arduino board do you have ?

Depending on your board you might be using more RAM than available.

@ PaulS

The two outputs are from the same part of the program ie the first Serial.print block before and after commenting the while loop.

Without the loop the Nodes array is nice and full With the loop it seems to corrup before the rest of the program is even run.

The outputs of the other Serial.prints I havnt uploaded as they are either the expected (when there is no while loop), or garbabe (with while loop).

/---------------------

@Digimike

Everything is ion the setup as I opnt want it to run once, I am converting MATLab code into C++ code and this is one small function. As I am changing it to a function later, I do not want to use the looping nature of loop.

/-----------------------

@AlphaBeta

Gahhh forgot about doing that, you have a good point. I've grown soft on MATLab. Will d that toute suite, but the loop problem still remains :(

/-------------------------- @MikMo I was thinking RAM, but there should still be plenty left over...

Arduino Duealmove (sp)

Is your Arduino board with the 168 or the 328 processor.

If im not much wrong the 168 only has 1kB of RAM and you are over that.

The 328 has 2KB RAM. But Arduinos internal structures use some so you might even be over that.

The 3 arrays you declare use 1600 Bytes together, that leaves only 400 + a bit if you have the 328 processor, and When Arduino has taken what it needs you might be using more than 2KB.

Wow is it really a RAM thing after all? its a 328 version

Thanks for your help. How Do I work out the ram capacity as I thought as long as the loader said xxx Kb free, everything is fine.

Is there a command or do I just add it up by hand?

How do I rectify, as the program needs to be much longer. Just be reducing the arrays to binary where possible?

Cheers for your help

How Do I work out the ram capacity as I thought as long as the loader said xxx Kb free, everything is fine

The compiler/loader tells you how much program memory is free.
The AVR is a Harvard architecture, so the program and data memories are separate.

As to memory saving, yes, if it’s a binary, use a single bit if possible.
Always try to use the smallest datatype that will do the job.

Makes sense, I'll keep that in mind for the future. Thanks for the help.

is there a way to have a binary array, or a long string of binary data? I'm looking at the supported data types and only see 'byte'. If i use this, do I group the data into batches of 8 or is there a way of making a long bit sting then use:

byte Variable[8]

to access the 9th binary value.

Apologies for the 'n00b' question. I am not used to this variable assignment yet.

I'd probably do it myself with arrays of "byte" - here integer division and modulo operators come in handy. Also, the bit manipulation functions provided come in handy, e.g. http://www.arduino.cc/en/Reference/BitRead

Checked on a Mega- turns out it was a RAM issue.

Reworte with bye instead of int, and now works on a Duemilanove

Thanks for all the help!