[Solved] Array not taking expected values from Serial.read()

I have been trying to update and extend my old EEPROM programming program by making it take values from Serial and write them to an EEPROM.

However, when I have input the data into the serial monitor and programmed the Arduino to write it back once it has started processing it, the data is completely different to what I entered.
I assume that there is some problem with the arrays being overwritten but I have no idea why or what is causing it.
I have looked over the forums for similar problems but my code didn't appear to have the same cause.

Here is the part that I think is causing the problem as well as the setup code. It is all in void setup:

  //Initialise all variables
  bool binaryData[8];
  bool binaryAddress[16];
  char hexData[3];
  int byteCount=0;
  int letterCount=0;
  int programSize=0;
  int loopOut=0;

  //Intitialise the IO pins
  pinMode(13, OUTPUT);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(39, OUTPUT);
  pinMode(40, OUTPUT);
  pinMode(41, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(44, OUTPUT);
  pinMode(46, OUTPUT);
  pinMode(48, OUTPUT);
  pinMode(50, OUTPUT);
  pinMode(52, OUTPUT);

  Serial.begin(9600);
  while(!Serial){}
  Serial.setTimeout(100000);
  Serial.println("Enter number of bytes input:");
  programSize = Serial.parseInt();
  Serial.println("Enter data to be written to the EEPROM");


  while (byteCount < programSize){
    if (Serial.available() >= 2){
      Serial.println("Flashing!");
      hexData[0] = 'v';
      hexData[1] = 'v';
      while (hexData[0]!='0' and hexData[0]!='1' and hexData[0]!='2' and hexData[0]!='3' and hexData[0]!='4' and hexData[0]!='5' and hexData[0]!='6' and hexData[0]!='7' and hexData[0]!='8' and hexData[0]!='9' and hexData[0]!='A' and hexData[0]!='B' and hexData[0]!='C' and hexData[0]!='D' and hexData[0]!='E' and hexData[0]!='F'){
        hexData[0] = char(Serial.read());
        Serial.print(hexData[0]);
      }

      while (hexData[1]!='0' and hexData[1]!='1' and hexData[1]!='2' and hexData[1]!='3' and hexData[1]!='4' and hexData[1]!='5' and hexData[1]!='6' and hexData[1]!='7' and hexData[1]!='8' and hexData[1]!='9' and hexData[1]!='A' and hexData[1]!='B' and hexData[1]!='C' and hexData[1]!='D' and hexData[1]!='E' and hexData[1]!='F'){
        hexData[1] = char(Serial.read());
        Serial.print(hexData[1]);
      }

This should check the inputs for the correct characters and then allow it to exit and continue. It does this but all the data I input was valid and far more data has been output by the Serial.print(hexData[0 and 1]) than I input.
The program then goes on to convert the hex into bool arrays which are then used to for the output to the various pins to program the EEPROM.

If anyone knows what could have caused this I would appreciate help.

dont post snippets... where does this go?

you should read Read Serial Input Basics

Why the &"§#@ do you use the type bool for hex data? booleans are meant to be true or false... use uint8_t or byte

when you do

  if (Serial.available() >= 2) {
    Serial.println("Flashing!");
    hexData[0] = 'v';
    hexData[1] = 'v';
    while (hexData[0] != '0' and hexData[0] != '1' and hexData[0] != '2' and hexData[0] != '3' and hexData[0] != '4' and hexData[0] != '5' and hexData[0] != '6' and hexData[0] != '7' and hexData[0] != '8' and hexData[0] != '9' and hexData[0] != 'A' and hexData[0] != 'B' and hexData[0] != 'C' and hexData[0] != 'D' and hexData[0] != 'E' and hexData[0] != 'F') {
      hexData[0] = char(Serial.read());
      Serial.print(hexData[0]);
    }

    while (hexData[1] != '0' and hexData[1] != '1' and hexData[1] != '2' and hexData[1] != '3' and hexData[1] != '4' and hexData[1] != '5' and hexData[1] != '6' and hexData[1] != '7' and hexData[1] != '8' and hexData[1] != '9' and hexData[1] != 'A' and hexData[1] != 'B' and hexData[1] != 'C' and hexData[1] != 'D' and hexData[1] != 'E' and hexData[1] != 'F') {
      hexData[1] = char(Serial.read());
      Serial.print(hexData[1]);
    }

you test if you have more than 2 bytes in the input but if you read something wrong for the first one you will attempt to read plenty more than you have really in the input buffer.

also instead of testing all the possible values, test

if ( (hexData[1] >= '0' && hexData[1]  <= '9') || (hexData[1] >= 'A' && hexData[1]  <= 'F')) {
 // it's an hex digit
}

as those characters are next to one another in the ASCII table. makes it easier to type or use the [url=http://www.cplusplus.com/reference/cctype/isxdigit/]isxdigit()[/url] standard C function

Thank you for your reply.
I don't know what you mean about using a bool and the hexData variable. I am using an integer for the index and it is a char datatype.
Thank you for your second suggestion, I forgot that chars can be manipulated like integers in C using their ASCII codes. I thought there may be a better way to do it but I couldn't remember what it was.
Here is the full program with a the modification you suggested.

void writeEEPROM(bool A15A, bool A14A, bool A13A, bool A12A, bool A11A, bool A10A, bool A9A, bool A8A, bool A7A, bool A6A, bool A5A, bool A4A, bool A3A, bool A2A, bool A1A, bool A0A, bool Q7A, bool Q6A, bool Q5A, bool Q4A, bool Q3A, bool Q2A, bool Q1A, bool Q0A) {
  digitalWrite(41, true);
  delay(150);
  bool Q0S;
  bool Q1S;
  bool Q2S;
  bool Q3S;
  bool Q4S;
  bool Q5S;
  bool Q6S;
  bool Q7S;
  digitalWrite(22, A0A);
  digitalWrite(24, A1A);
  digitalWrite(26, A2A);
  digitalWrite(28, A3A);
  digitalWrite(30, A4A);
  digitalWrite(32, A5A);
  digitalWrite(34, A6A);
  digitalWrite(36, A7A);
  digitalWrite(23, A8A);
  digitalWrite(25, A9A);
  digitalWrite(27, A10A);
  digitalWrite(29, A11A);
  digitalWrite(31, A12A);
  digitalWrite(33, A13A);
  digitalWrite(35, A14A);
  digitalWrite(37, A15A);
  
  digitalWrite(38, Q0A);
  digitalWrite(40, Q1A);
  digitalWrite(42, Q2A);
  digitalWrite(44, Q3A);
  digitalWrite(46, Q4A);
  digitalWrite(48, Q5A);
  digitalWrite(50, Q6A);
  digitalWrite(52, Q7A);
  
  digitalWrite(41, true);
  delay(150);
  digitalWrite(41, false);
  delay(150);
  digitalWrite(41, true);
  delay(150);
}

void setup() {
  //Initialise all variables
  bool binaryData[8];
  bool binaryAddress[16];
  char hexData[2];
  int byteCount=0;
  int letterCount=0;
  int programSize=0;
  int loopOut=0;

  //Intitialise the IO pins
  pinMode(13, OUTPUT);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(39, OUTPUT);
  pinMode(40, OUTPUT);
  pinMode(41, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(44, OUTPUT);
  pinMode(46, OUTPUT);
  pinMode(48, OUTPUT);
  pinMode(50, OUTPUT);
  pinMode(52, OUTPUT);

  Serial.begin(9600);
  while(!Serial){}
  Serial.setTimeout(100000);
  Serial.println("Enter number of bytes input:");
  programSize = Serial.parseInt();
  Serial.println("Enter data to be written to the EEPROM");
  
  for (byteCount = 0;byteCount<programSize;byteCount++){
    Serial.println("Flashing");
    hexData[0] = 'v';
    hexData[1] = 'v';
    while ((hexData[0] < '0' || hexData[0]  > '9') && (hexData[0] < 'A' || hexData[0]  > 'F')){
      if (Serial.available() >= 1){
        hexData[0] = char(Serial.read());
        Serial.print(hexData[0]);
      }
    }
    while ((hexData[1] < '0' || hexData[1]  > '9') && (hexData[1] < 'A' || hexData[1]  > 'F')){
      if (Serial.available() >= 1){
        hexData[1] = char(Serial.read());
        Serial.print(hexData[1]);
      }
    }
    Serial.println(" Data entered");
    Serial.print(hexData[0]);
    Serial.println(hexData[1]);
    //Converts from hex to binary
    for(letterCount=0; letterCount<2; letterCount++){
      switch (hexData[letterCount]){
        case '0':
          binaryData[0+(4*letterCount)] = 0;
          binaryData[1+(4*letterCount)] = 0;
          binaryData[2+(4*letterCount)] = 0;
          binaryData[3+(4*letterCount)] = 0;
          break;
        case '1':
          binaryData[0+(4*letterCount)] = 0;
          binaryData[1+(4*letterCount)] = 0;
          binaryData[2+(4*letterCount)] = 0;
          binaryData[3+(4*letterCount)] = 1;
          break;
        case '2':
          binaryData[0+(4*letterCount)] = 0;
          binaryData[1+(4*letterCount)] = 0;
          binaryData[2+(4*letterCount)] = 1;
          binaryData[3+(4*letterCount)] = 0;
          break;
        case '3':
          binaryData[0+(4*letterCount)] = 0;
          binaryData[1+(4*letterCount)] = 0;
          binaryData[2+(4*letterCount)] = 1;
          binaryData[3+(4*letterCount)] = 1;
          break;
        case '4':
          binaryData[0+(4*letterCount)] = 0;
          binaryData[1+(4*letterCount)] = 1;
          binaryData[2+(4*letterCount)] = 0;
          binaryData[3+(4*letterCount)] = 0;
          break;
        case '5':
          binaryData[0+(4*letterCount)] = 0;
          binaryData[1+(4*letterCount)] = 1;
          binaryData[2+(4*letterCount)] = 0;
          binaryData[3+(4*letterCount)] = 1;
          break;
        case '6':
          binaryData[0+(4*letterCount)] = 0;
          binaryData[1+(4*letterCount)] = 1;
          binaryData[2+(4*letterCount)] = 1;
          binaryData[3+(4*letterCount)] = 0;
          break;
        case '7':
          binaryData[0+(4*letterCount)] = 0;
          binaryData[1+(4*letterCount)] = 1;
          binaryData[2+(4*letterCount)] = 1;
          binaryData[3+(4*letterCount)] = 1;
          break;
        case '8':
          binaryData[0+(4*letterCount)] = 1;
          binaryData[1+(4*letterCount)] = 0;
          binaryData[2+(4*letterCount)] = 0;
          binaryData[3+(4*letterCount)] = 0;
          break;
        case '9':
          binaryData[0+(4*letterCount)] = 1;
          binaryData[1+(4*letterCount)] = 0;
          binaryData[2+(4*letterCount)] = 0;
          binaryData[3+(4*letterCount)] = 1;
          break;
        case 'A':
          binaryData[0+(4*letterCount)] = 1;
          binaryData[1+(4*letterCount)] = 0;
          binaryData[2+(4*letterCount)] = 1;
          binaryData[3+(4*letterCount)] = 0;
          break;
        case 'B':
          binaryData[0+(4*letterCount)] = 1;
          binaryData[1+(4*letterCount)] = 0;
          binaryData[2+(4*letterCount)] = 1;
          binaryData[3+(4*letterCount)] = 1;
          break;
        case 'C':
          binaryData[0+(4*letterCount)] = 1;
          binaryData[1+(4*letterCount)] = 1;
          binaryData[2+(4*letterCount)] = 0;
          binaryData[3+(4*letterCount)] = 0;
          break;
        case 'D':
          binaryData[0+(4*letterCount)] = 1;
          binaryData[1+(4*letterCount)] = 1;
          binaryData[2+(4*letterCount)] = 0;
          binaryData[3+(4*letterCount)] = 1;
          break;
        case 'E':
          binaryData[0+(4*letterCount)] = 1;
          binaryData[1+(4*letterCount)] = 1;
          binaryData[2+(4*letterCount)] = 1;
          binaryData[3+(4*letterCount)] = 0;
          break;
        case 'F':
          binaryData[0+(4*letterCount)] = 1;
          binaryData[1+(4*letterCount)] = 1;
          binaryData[2+(4*letterCount)] = 1;
          binaryData[3+(4*letterCount)] = 1;
          break;
        default:
          Serial.println("Invalid data.");
      }
    }
    for(byteCount=0;byteCount<=programSize;byteCount++){
      binaryAddress[0]=bitRead(byteCount,15);
      binaryAddress[1]=bitRead(byteCount,14);
      binaryAddress[2]=bitRead(byteCount,13);
      binaryAddress[3]=bitRead(byteCount,12);
      binaryAddress[4]=bitRead(byteCount,11);
      binaryAddress[5]=bitRead(byteCount,10);
      binaryAddress[6]=bitRead(byteCount,9);
      binaryAddress[7]=bitRead(byteCount,8);
      binaryAddress[8]=bitRead(byteCount,7);
      binaryAddress[9]=bitRead(byteCount,6);
      binaryAddress[10]=bitRead(byteCount,5);
      binaryAddress[11]=bitRead(byteCount,4);
      binaryAddress[12]=bitRead(byteCount,3);
      binaryAddress[13]=bitRead(byteCount,2);
      binaryAddress[14]=bitRead(byteCount,1);
      binaryAddress[15]=bitRead(byteCount,0);
      writeEEPROM(binaryAddress[0], binaryAddress[1], binaryAddress[2],binaryAddress[3],binaryAddress[4],binaryAddress[5],binaryAddress[6],binaryAddress[7],binaryAddress[8],binaryAddress[9],binaryAddress[10],binaryAddress[11],binaryAddress[12],binaryAddress[13],binaryAddress[14],binaryAddress[15],binaryData[0],binaryData[1],binaryData[2],binaryData[3],binaryData[4],binaryData[5],binaryData[6],binaryData[7]);
    }
  }
  
}


void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("End Of Program");
  delay(5000);
}

Having run a few more tests, I have just got it working.
It turns out that I had used byteCount inside two for loops, one inside of the other. The second loop was causing it to drop out of the first.
Thank you.

It turns out that I had used byteCount inside two for loops, one inside of the other.

Learn from this.
Use local variables as for loop parameters rather than globals
Use different variable names for nested for loop variables