SOLVED: A bug in my sketch is preventing me from reading back from an array

I am trying to modify a sketch posted here: http://arduinoinfo.mywikis.net/wiki/Arduino_Test - it’s for self testing the pins of an Arduino by connecting them together via resistors and measuring the voltages when they are set to output high/low.

I am having a problem trying to modify the sketch so that an -OK- or FAIL mark is added to the serial output underneath the result. I am trying to copy what was already done earlier on in the sketch where the result is entered into an array in the form of 0 for pass and 1 for fail. It seems like entering this into the array is going fine but for some reason, reading back from the array to print to the serial monitor is going wrong.

I removed all the superfluous tests to produce this sketch:

/* YourDuino Arduino Board TEST
  Tests all Arduino I/O and Analog pins for Source and Sink ability
  Tests Analog Read on A0..A5
  terry@yourduino.com

  This test assumes that pins 0 (RX Data) and 1 (TX Data) are OK
  if programs can be loaded, and that pin 13 is OK if it's attached LED
  can be blinked. Pin 13 is not tested for current sink ability.

  Test progress and pass-fail results are sent to the Serial Monitor.
  At the end of test, Pin 13 LED is blinked very slowly
  for Pass and quickly for Fail.

  This test works with a "Test Shield" that is plugged on top
  of the Arduino under test. This shield has a common isolated
  connection point. The Common Point has 17 220 ohm resistors
  connected to every digital pin from 2 to 12, and to every analog
  pin from 0 to 5. It also has two 100,000 ohm resistors which
  connect it to Ground and +5.0 volts, forming a voltage divider
  that sets the common point to 2.50 volts when all pins are in
  the reset high-impedance input condition. This is read by A0 to
  initially test if any of the pins are "stuck" or "leaking", which
  will pull the common point away from 2.50 volts

  Next, pins 3 thru A5 are tested by setting them High or Low against
  an opposite load presented by pin 2. Each pin must be able to
  source and sink at least 10 ma to bring the common point near 2.50 volts.
  If pin 2 fails, all other pins will fail.

  Next, A0 thru A5 are tested individually with voltages near 1.66, 2.50, 3.33volts
  and possibly others, using the resistors connected to the digital pins to
  make various voltage dividers.

  This design and software is licensed under a Creative Commons
  Attribution-Noncommercial-Share Alike 3.0 License:
  http://creativecommons.org/licenses/by-nc-sa/3
*/

/*-----( Import needed libraries )-----*/

/*-----( Declare Constants )-----*/

const  int    FirstDigPort =  3;  //Pin 2 is used as reference
const  int    LastDigPort  =  12;
const  int    FirstAnaPort =  15; //Pin 14 is used as a reference
const  int    LastAnaOutPort = 19;
const  int    LastAnaPort  =  21;
const  int    A2D_Delay    =  10; //ms delay for A-D to settle

const  float  LowStuckResultOK     =  2.40 ;
const  float  HighStuckResultOK    =  2.60 ;
const  float  LowVoltResultOK      =  2.30 ;
const  float  HighVoltResultOK     =  2.70 ;
const  float  LowVoltResult2OK      =  3.13 ;
const  float  HighVoltResult2OK     =  3.53 ;
const  float  LowVoltResult3OK      =  1.46 ;
const  float  HighVoltResult3OK     =  1.86 ;

/*-----( Declare objects? )-----*/

/*-----( Declare Variables )-----*/

int    DigPort2Test;
int    AnaPort2Test;
int    IntVoltsRead;      // Result of AnalogRead 0..1023
float  FloatVoltsRead;    // Result of AnalogRead 0.0 .. 5.0
int    Failure;          // Count failures that occur
byte   FailArray[20];    // Save Pass-Fail during a test
int    FailArrayPtr;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  delay(500);
  Serial.begin(9600);
  delay(500);
  Serial.println("Arduino Board Test V2");
  Serial.println("YourDuino.com terry@yourduino.com");
  delay(1000);

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Failure = 0;
  Serial.println();
  Serial.println("--- TEST START ---");

  /*---------( TEST 6 )----------------------*/
  Serial.println();
  Serial.println("TEST 006 - Analog Pins A0 to A5: A to D (About 2.50 V)");
  pinMode(2, OUTPUT);      // Set Load pin to Output mode
  digitalWrite(2, HIGH);              // Connect HIGH Load
  pinMode(3, OUTPUT);      // Set Load pin to Output mode
  digitalWrite(3, LOW);              // Connect LOW Load
  FailArrayPtr    =  0 ;  // Reset fail array position pointer

  WriteAnalogVoltLabels();

  for (  AnaPort2Test = FirstAnaPort - 1;
         AnaPort2Test <= LastAnaPort;
         AnaPort2Test ++)
  { // Loop thru Analog Tests
    delay(A2D_Delay);
    IntVoltsRead = analogRead(AnaPort2Test);
    FloatVoltsRead = a2d2float (IntVoltsRead);
    Serial.print(FloatVoltsRead, 2);
    Serial.print("   ");

    if (FloatVoltsRead > LowVoltResultOK
        &&  FloatVoltsRead < HighVoltResultOK)
    {
      FailArray[FailArrayPtr]  = 0;
    }
    else
    {
      Failure ++;
      FailArray[FailArrayPtr]  = 1;
    }
 Serial.println(FailArray[FailArrayPtr]);
  }// END Loop thru Analog Tests
  Serial.println();
  WriteAnalogVoltPassFail();

  //----------( End Test Digital and Analog Ports: Reset everything)-------
  pinMode(2, INPUT);      // Reset Load pin to Input mode
  pinMode(3, INPUT);      // Reset Load pin to Input mode
  pinMode(4, INPUT);      // Reset Load pin to Input mode
  MCUCR |= ( 1 << PUD ); //Disable all Pullups

  /*------------( TEST COMPLETE )----------------*/
  Serial.println();
  Serial.println("TEST COMPLETE");
  delay(10000000);
  //--(end main loop )---
}
//----------------( Declare User-written Functions )--------------------------

/*---( fmap: Converts int Analog readings of 0..1023 to float)---*/
float fmap(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

float a2d2float (int raw)
{
  return fmap(raw, 0, 1023, 0.0, 5.0);
}

void WriteAnalogVoltLabels()
{
  //---------( Write the Analog Pin Labels )---------------------
  for (  DigPort2Test = FirstAnaPort - 1;
         DigPort2Test <= LastAnaPort;
         DigPort2Test ++)
  { //Write the label strings
    Serial.print("PIN-");
    Serial.print(DigPort2Test - 14 , DEC);
    Serial.print("  ");
  }// Write Labels
  Serial.println();
}


//--------( Write the Analog Pin PASS-FAIL Strings )----------------------
void WriteAnalogVoltPassFail()
{
  FailArrayPtr   = 0;
  for (  DigPort2Test = FirstAnaPort - 1;
         DigPort2Test <= LastAnaPort;
         DigPort2Test ++)
  { //Write the PASS-FAIL strings
    FailArrayPtr ++;
    if   (FailArray[FailArrayPtr]  == 1)  // Was a Fail
    {
      Serial.print("FAIL   ");
    }
    else
    {
      Serial.print("-OK-   ");
    }
Serial.println(FailArray[FailArrayPtr]);
  }// Write PASS-FAIL
  Serial.println();
}//End  WriteDigitalPassFail




//*********( THE END )***********

The output from this sketch looks like this:

--- TEST START ---

TEST 006 - Analog Pins A0 to A5: A to D (About 2.50 V)
PIN-0  PIN-1  PIN-2  PIN-3  PIN-4  PIN-5  PIN-6  PIN-7  
2.48   0
2.48   0
2.48   0
2.48   0
2.48   0
1.88   1
2.48   0
1.80   1

-OK-   0
-OK-   0
-OK-   0
-OK-   0
-OK-   0
-OK-   0
-OK-   0
-OK-   0


TEST COMPLETE

As you can see, the fails are stored correctly into the array when the measurements are taken but in the later attempt to read back from the array, all I get are 0s? Thanks for any advice.

The loop which does the testing and writes to FailArray[FailArrayPtr] never updates FailArrayPtr. Therefore all entries are written to index [​0] of the array.

In loop(), you fail to increment FailArrayPtr, and also fail to check it against the maximum possible value.

Thanks so much! I really appreciate it. I spent an hour going over it and couldn't see that. I guess/hope you get better/faster at seeing something like that with practice.