-#define PATTERNPANIC 0
+/* Start of error codes */
+#define NUMERRORCODES 1
+#define CRCFAIL 0
 
-#define MESSAGESIZE 3
+#define MESSAGESIZE 3 // the number of bytes in a message
 
 #include <SoftwareSerial.h>
 
 SoftwareSerial rpiSerial(3, 2); // RX, TX
-
-#define NUMANALOGINPUTPINS 6
-int analogInputPins[NUMANALOGINPUTPINS] = {0, 1, 2, 3, 4, 5};
-
-#define NUMDIGITALINPUTPINS 1
-int digitalInputPins[NUMDIGITALINPUTPINS] = {4};
-
-#define NUMINPUTPINS NUMANALOGINPUTPINS + NUMDIGITALINPUTPINS
-int inputPins[NUMINPUTPINS]; // will hold analogInputPins + digitalInputPins
-
-int (*readPtr[NUMINPUTPINS])(uint8_t); // will hold the read (digitalRead or analogRead or something else) function for a given pin
 
 union message {
     byte rawBytes[MESSAGESIZE]; // holds the two data bytes and the CRC

  byte bytes[2];
};

void setup() {

  Serial.begin(9600);
  rpiSerial.begin(9600);

  // combine the two arrays of pins
  memcpy(inputPins, analogInputPins, NUMANALOGINPUTPINS * sizeof(int));
  memcpy(inputPins + NUMANALOGINPUTPINS, digitalInputPins, NUMDIGITALINPUTPINS * sizeof(int));

  for (int index = 0; index < NUMANALOGINPUTPINS; index++)
  {
    readPtr[index] = analogRead;
  }

  for (int index = 0; index < NUMDIGITALINPUTPINS; index++)
  {
    pinMode(digitalInputPins[index], INPUT);
    readPtr[index + NUMANALOGINPUTPINS] = digitalRead;
  }
}

void loop() {
    
  if (rpiSerial.available() >= MESSAGESIZE) { // wait for a full MESSAGE to arrive
    
    message masterMessage = ReadMessage(rpiSerial);
    
    byte incomingMasterCRC = CalcCRC(masterMessage.dataBytes, MESSAGESIZE-1);

    if (incomingMasterCRC == masterMessage.CRC) { // if the CRC from the master passes
      


      message slaveMessage;
      memcpy(slaveMessage.dataBytes, reading.bytes, MESSAGESIZE-1);
      slaveMessage.CRC = calcCRC(slaveMessage.dataBytes, MESSAGESIZE-1); 
  
      writeMsg(rpiSerial, slaveMessage);
            
    } else { // Bad master CRC
      Serial.println("Sending CRC!");
      writeMsg(rpiSerial, errorMessages[CRCFAIL]);
    }
  }
}

message readMsg(SoftwareSerial &port) {
  message incomingMessage;
  port.readBytes(incomingMessage.rawBytes, MESSAGESIZE); // read the raw bytes from the serial port into a message struct
  return incomingMessage;
}

void writeMsg(SoftwareSerial &port, message m) {
  for (int index = 0; index < MESSAGESIZE; index++) {
    port.write(m.rawBytes[index]);
  }
}

void printMsg(message m) {
  Serial.print("Message [");
  Serial.print("M0: ");
  Serial.print(m.rawBytes[0], HEX);
  Serial.print(" M1: ");
  Serial.print(m.rawBytes[1], HEX);
  Serial.print(" CRC: ");
  Serial.print(m.rawBytes[2], HEX);
  Serial.println("]");
}

byte calcCRC(byte byteArray[], int numBytes) {
  byte b = 0;
  for (int index = 0; index < numBytes; index++)
  {
