Arduino calculator using adder and xor

I want to make a calculator using an adder and a xor (LS283),(LS86) with 12 Leds. Eight at a time are used to show me the input in binary, so the first four give me the first part and the second four give me the second part of the equation andhe last four leds show me the result, also in binary. I have no problems with the circuit but with my code. It does not work.

int A1Pin = 5;
int A2Pin = 3;
int A3Pin = 14;
int A4Pin = 12;

int B1Pin = 6;
int B2Pin = 2;
int B3Pin = 15;
int B4Pin = 11;

int LED1Pin = 2;
int LED2Pin = 3;
int LED3Pin = 4;
int LED4Pin = 5;
int LED5Pin = 9;
int LED6Pin = 10;
int LED7Pin = 11;
int LED8Pin = 12;

int LEDs1Pin = 4;
int LEDs2Pin = 1;
int LEDs3Pin = 13;
int LEDs4pin = 10; 

long A; // first equation part
long B; // second equation part
char operation; // The particular operation
long result; // result

void setup()
{
  Serial.begin(9600);
  Serial.println("calculate:");
  pinMode(A1Pin, OUTPUT);
  pinMode(A2Pin, OUTPUT);
  pinMode(A3Pin, OUTPUT);
  pinMode(A4Pin, OUTPUT);
  pinMode(B1Pin, OUTPUT);
  pinMode(B2Pin, OUTPUT);
  pinMode(B3Pin, OUTPUT);
  pinMode(B4Pin, OUTPUT);

  pinMode(LED1Pin, INPUT);
  pinMode(LED2Pin, INPUT);
  pinMode(LED3Pin, INPUT);
  pinMode(LED4Pin, INPUT);
  pinMode(LED5Pin, INPUT);
  pinMode(LED6Pin, INPUT);
  pinMode(LED7Pin, INPUT);
  pinMode(LED8Pin, INPUT);
}

void loop() {
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  
  while(Serial.available() > 0) {
    A = Serial.parseInt();   
    operation = Serial.read();
    B = Serial.parseInt(); 
    calculate(); // Custom function to perform the calculations 
    Serial.print("result = ");
    Serial.println(result); 
    Serial.println("done..."); 
    Serial.println(); 
    }   
  }
void calculate() { 
  if (operation == '+') {
    result = A + B;
  }
  else if (operation == '-')  {
    result = A - B; 
  }
    // input a as four separate bits
    bool a0 = digitalRead(A1Pin);
    bool a1 = digitalRead(A2Pin);
    bool a2 = digitalRead(A3Pin);
    bool a3 = digitalRead(A4Pin);

    //input b as four sepatate bits
    bool b0 = digitalRead(B1Pin);
    bool b1 = digitalRead(B2Pin);
    bool b2 = digitalRead(B3Pin);
    bool b3 = digitalRead(B4Pin);
    
    bool s0 = a0 ^ b0;
    bool c0 = a0 & b0;
    bool c1 = a1 & b1;
    bool c2 = a2 & b2;
    bool c3 = a3 & b3;

    bool s1 = (a0 ^ b0) ^ c0;
    bool s2 = (a1 ^ b1) ^ c1;
    bool s3 = (a2 ^ b2) ^ c2;
    bool s4 = (a3 ^ b3) ^ c3;

    digitalWrite(LEDs1Pin, s0);
    digitalWrite(LEDs2Pin, s1);
    digitalWrite(LEDs3Pin, s3);
    digitalWrite(LEDs4pin, s4);
}

Hi, @showluigi1
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please do not use Fritzy to make your diagram.
Please include component names and pin labels.
What model Arduino are you using?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

So, what does it do ?

When you've been here a little longer, you'll understand why people hate that phrase.

Pin numbers should be "const byte", and if you've gone to the trouble of naming a pin, please use the name throughout

How do you know there's something to read?

Maybe you should try printing your operands and operators to see they're what you expect.

Hi,
You have for example;

int B2Pin = 2;

and

int LED1Pin = 2;

then
deciare the same pins as inputs and outputs.

 pinMode(B2Pin, OUTPUT);
 pinMode(LED1Pin, INPUT);

Which is it, we definitely need a schematic.

Tom... :smiley: :+1: :coffee: :australia:

If you have configured a digital pin as an INPUT you can only digitalRead from it and not digitalWrite to it. Conversely if the pin is configured as an OUTPUT you can only digitalWrite to it.

You seem to have the pins configured backwards. the A and B pins should be inputs and the LEDs pins should be outputs.

Wrong. You can use digitallRead() to read the state of an output pin so you can do

digitalWrite(pinNumber, !digitalRead(pinNumber));

to toggle the state of a pin

You can even write to a pin that's set as INPUT. It will enable the internal pullup resistor.

I am using an Arduino nano . This is the circuit:

...or disable it :wink:

Yep, true. But in this example he's simply got INPUT and OUTPUT backwards. Yes, reading from an output pin does something and writing to an input pin does something, but nothing that the original poster wants to do!

So here is the Kicad version of the circuit

Agreed, but what AWOL and I are pointing out is that when you said

You were wrong

Waggling the pull up resistor on an input pin can cause a connected LED to go from dim to very dim, so yes, something the original poster wanted to do (without the "dim" bit)

What I had in mind:
I want a kind of calculator with an adder and a XOR, with 12 leds, the first 4 leds should display the first value in binary, and the second 4 leds display the second value in binary, with the adder and Xor a sum of both eqations is added up (but just summation and subtraction like A+B=S, A-B=S, A representing the first 4 Leds and B representing the Second 4 Leds and S the sum of both in 4 leds). I hope it is clearer now.

So, what does the code look like now, and how is it behaving?

Unless I'm missing something, the original code doesn't make sense. The operands of the colcualtion are first parsed from serial, and then a binary representation is supposedly read from 8 pins, but there's no way the serial input is transposed to the pins and there are only 4 pins connected to the Nano.

When I load the code onto my Arduino nano, I can calculate on it, but none of the LEDs light up

You haven't shown us the code you're loading after the fixes that have been pointed-out