I/O control over serial

Hi Paul,
Thanks for the comment.

This was only in simulation, but I do use resistors in practice. The rest of the simulation I have drawn up has the outputs driving relays through an ULN2003A and the inputs isolated through 4N25 opto couplers.
This is the circuit i have so far.

I did wonder this myself. I agree it is a strange order to start with D1 instead of D0. I presume it has something todo with the onboard 328P pinouts, avoiding un-nessessary via's or something to do with FTDI/PL2303 chipset, I would like to hear if someone knows the actual reason for this.

Would adding the following after the read sequence help in removing any unwanted jibberish from the receive buffer that may have gotten stuck? I assume that running the MCU at 16Mhz and the baud of the serial port only at 9600, data couldn't possibly come in that fast that it would get stuck before the buffer is empty.

void ReceiveData() {
  if (Serial.available() == 8 && Serial.read() == 'S') {   // If there is 8 bytes of data in the receive buffer AND the buffer starts with S
    Serial.readBytes(ReceivedData, 7);           // read the received buffer into the ReceivedData array.
    for (int r = 0; r <= 5; r++) {
      switch (ReceivedData[r]) {
        case '1':
          digitalWrite(OutputPins[r], HIGH);               // If '1' is received, turn off output.
          break;
        case '2':                                          // If a 2 is received, turn on the output.
          digitalWrite(OutputPins[r], LOW);
          break;
      }
    }
  }
  while (Serial.available() > 8){
    Serial.read();                        //  Read until the serial buffer is empty.
  }
}

Kind regards,
Josh