int pin_A[4] = {5,4,3,2};
int input_pins[4] = {4,3,2,1};
int places[4] = {8,4,2,1};
int inputVal_A[4] = {0,0,0,0};
int pin_B[4] = {9,8,7,6};
int inputVal_B[4] = {0,0,0,0};
int pin_C0 = 10;
int inputVal_C0 = 0;
int pinActual;

bool value = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Please input values for A and B pins.");
  Serial.println("(Ex: A1=1, B2=0, etc)");

  for(int i = 0; i <= 3; i++) {
    pinMode(pin_A[i], OUTPUT);
    pinMode(pin_B[i], OUTPUT);
  }

  

  pinMode(pin_C0, OUTPUT);
  
 
}

void loop() {
for(int i = 0; i <= 3; i++) {
    digitalWrite(pin_A[i], inputVal_A[i]);
    digitalWrite(pin_B[i], inputVal_B[i]);
  }
digitalWrite(pin_C0, inputVal_C0);


if(Serial.available() > 0) {
 
  String input = Serial.readStringUntil('\n');
  
  
  if (input.startsWith("A") && isDigit(input.charAt(1))) {
    if(input.charAt(3) != '0') {value = 1;} else {value = 0;}
    int pin = input.charAt(1) - '0';
    setInput('A', pin, value);
  }
  if (input.startsWith("B") && isDigit(input.charAt(1))) {
    if(input.charAt(3) != '0') {value = 1;} else {value = 0;}
    int pin = input.charAt(1) - '0';
    setInput('B', pin, value);
  }

  if (input.startsWith("C=") && isDigit(input.charAt(2))) {
    value = input.charAt(2) - '0';
    setInput('C', 0, value);
  }

  if (input == "A?") {
    chkInput('A');
  }

  if (input == "B?") {
    chkInput('B');
  }

  if (input == "C?") {
    chkInput('C');
  }

  if (input == "S?") {
    chkSum();

  }

  
  


}
}


int chkSum() {

int valA = 0b00;
int valB = 0b00;
int valC = 0b00;
int valS = 0b00;

Serial.println();
for (int i = 0; i < 4; i++) {
      Serial.print(digitalRead(pin_A[i]));
      bitWrite(valA,i,digitalRead(pin_A[i]));
      }
Serial.print(" + ");
for (int i = 0; i < 4; i++) {
      Serial.print(digitalRead(pin_B[i]));
      bitWrite(valB,i,digitalRead(pin_B[i]));
      }

Serial.print(" + ");
Serial.print(digitalRead(pin_C0));
Serial.print(" = ");

if(digitalRead(pin_C0 == 1)) {valC=0b01;}

valS = valA & valB & valC;

Serial.print(valS, BIN);
Serial.print(".");
Serial.println();


}


int chkInput(char input) {
  
  
    if(input == 'A') {
      Serial.print("Input ");
      Serial.print(input);
      Serial.print(": ");
      for (int i = 0; i < 4; i++) {
      Serial.print(digitalRead(pin_A[i]));
      }
    } else if (input == 'B') {
      Serial.print("Input ");
      Serial.print(input);
      Serial.print(": ");
      for (int i = 0; i < 4; i++) {
      Serial.print(digitalRead(pin_B[i]));
      }
    } else if (input == 'C') {
      Serial.print("Carry-In: ");
      Serial.print(digitalRead(pin_C0));
      }
    
  
  Serial.println();
}



int findPin(int array[], int arraySize, int value) {
  for (int i = 0; i < arraySize; i++) {
    if (array[i] == value) {
      return i;  // Return index if value is found
    }
  }
  return -1;  // Return -1 if value is not found
}

void setInput(char input, int num, bool val) {
  switch (input) {
    case 'A':
      pinActual = findPin(input_pins, 4, num);
      inputVal_A[pinActual] = val;
      Serial.print("Pin A");
      Serial.print(num);
      Serial.print(" changed to ");
      Serial.print(val);
      Serial.print(".");
      Serial.println();
      break;
    case 'B':
      pinActual = findPin(input_pins, 4, num);
      inputVal_B[pinActual] = val;
      Serial.print("Pin B");
      Serial.print(num);
      Serial.print(" changed to ");
      Serial.print(val);
      Serial.print(".");
      Serial.println();
      break;
    case 'C':
      inputVal_C0 = val;
      Serial.print("Carry-In changed to ");
      Serial.print(inputVal_C0);
      Serial.print(".");
      Serial.println();
      break;
  }
}

74LS83Breakout