Odd value keeps showing up

I'm having a problem with a value showing up in an array every time index [0] comes up.

#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAB, 0x64, 0xEF, 0xFA, 0xED };
IPAddress ip(192, 168, 1, 100);
IPAddress subnet(255,255,255,0);
unsigned int localPort = 8888;      // local port to listen on



//////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
//////////////////////////////////////////////////////////////////////////////////
const byte totalAxes = 3;          // Count from zero
const byte deadband = 20;          // Create a deadband in the center of each pot


//////////////////////////////////////////////////////////////////////////////////
// VARIABLES
//////////////////////////////////////////////////////////////////////////////////
byte activeAxis = 0;    // set this to start at zero, or the first axis
int axisZero = A4;
int axisOne = A1;
int axisTwo = A2;
int axisThree = A3;

int test;


//////////////////////////////////////////////////////////////////////////////////
// ARRAYS
//////////////////////////////////////////////////////////////////////////////////
int axesArray[] = {axisZero,axisOne,axisTwo,axisThree};  // Array of analog pins, one for each axis
int axesNewArray[3];    // an array to hold analog values until they can be compared to existing value
int axesOldArray[3];    // an array to hold the final analog raw value
int axesFinalArray[3];    // an array to hold the filtered and centered value


//////////////////////////////////////////////////////////////////////////////////
// SETUP
//////////////////////////////////////////////////////////////////////////////////
void setup(){
  //Ethernet.begin(mac,ip,subnet);
  //Udp.begin(localPort);
  
  int a;
  for (a = 0; a <= 3; a = a + 1) {
  axesOldArray[a] = 0;
}

  for (a = 0; a <= 3; a = a + 1) {
  axesNewArray[a] = 0;
}

  for (a = 0; a <= 3; a = a + 1) {
  axesFinalArray[a] = 0;
  }
  
  Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////////
// MAIN PROGRAM
//////////////////////////////////////////////////////////////////////////////////
void loop(){
  Serial.print("  ++");
  Serial.print(activeAxis);
  Serial.println("++  ");
  
        
  axesNewArray[activeAxis] = analogRead(axesArray[activeAxis]);    // ...read the active pin

for (test = 0; test <= 3; test = test + 1) {
  Serial.println(axesFinalArray[test]);  // <--- why does activeAxis = 0 cause value 511 to show up in axis 0???
  }


  if(axesNewArray[activeAxis] >= axesOldArray[activeAxis] +2 || axesNewArray[activeAxis] <= axesOldArray[activeAxis] -2)
  {
    axesOldArray[activeAxis] = axesNewArray[activeAxis];    // copy new value into old value
    axesFinalArray[activeAxis] = axesOldArray[activeAxis];    // make final value the same
    axesFinalArray[activeAxis] /= 2;    // make the range 0-511
    axesFinalArray[activeAxis] -= 255;    // make the center of the pot zero
        
    // set zero value if in deadband
    if(axesFinalArray[activeAxis] > -deadband && axesFinalArray[activeAxis] < deadband)    // check to see if value is in deadband
    {
      axesFinalArray[activeAxis] = 0;    // make the result equal zero in the center if it is in deadband
    }
      
    // set axis to go from 0 at center to 255 at extreme
    if(axesFinalArray[activeAxis] < 0)
    {
      axesFinalArray[activeAxis] = map(axesFinalArray[activeAxis], -deadband, -255, 0, 255);
      // need direction flag set here
    }
    else if(axesFinalArray[activeAxis] > 0)
    {
      axesFinalArray[activeAxis] = map(axesFinalArray[activeAxis], deadband, 255, 0, 255);
      // need direction flag set here
    }
    else
    {
      axesFinalArray[activeAxis] = 0;    // set value to zero if in deadband
    }
  } 
  
  // index the pointer down to the next axis
  if(activeAxis < totalAxes)    // check if the active axis is the last one or not
  {
    ++activeAxis;    // increment it otherwise
  }
  else
  {
    activeAxis = 0;    // set it back to zero
  }
 
  delay(250);
}

//////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////

The serial monitor shows the following:
++0++ <-- when var(activeAxis) = 0...
511 <-- 511 shows up in axesFinalArray[0] every time
202
0
0
++1++
0 <-- but not here and no other index is affected
202
0
0
++2++
0
202
0
0
++3++
0
202
0
0

What in the world am I missing? Please don't laugh too hard at my code as I'm just learning. Also, it has evolved heavily as I've been tracking this bug for several hours now.

Any help would be appreciated.

Thanks,

Spugnoid

int axesOldArray[3];    // an array to hold the final analog raw value
...

  for (a = 0; a <= 3; a = a + 1) {
    axesOldArray[a] = 0;

You have declared an array of 3 positions. You are writing to 4 of them: 0, 1, 2, 3.

Fixed it right up. :blush:

For some reason I assumed that [3] meant 4 positions counting from zero.

Thanks again,

Spugnoid