4 bit Binary adder for Arduino sn74hc595n microprocessor

Hello all, this is my first time posting here so please have patience with me. Anyways the project that I am working on is a 4 bit binary adder. I was hoping I could just say all of the possible "if" statements and I would be fine. Sadly that is not the case for I am having trouble loading it onto the microprocessor I mentioned in the subject. I have only taken one class in my college career that dealt with programming, but it used an outdated program called Visual Basic. I've done a bunch of research, but if someone could reassure me that I am on the right track that would be great. Also it seems to give me this error message "avrdude: stk500_recv(): programmer is not responding avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0xbd Problem uploading to board." This error message has really stumped me, I tried taking out the serial.begin() and serial.end() to see what would happen and no good results. Thanks

-Steven

FIRST_SKETCH.ino (8.59 KB)

The error message means the computer can not communicate with the Arduino. Normally this happens when the wrong com port is chosen or the Arduino is broken.

I can't read a .ino file on an iPad but there is no need to use a bunch of if statements for an adder. The + operation does that and it works in binary. The real problem is gathering individual bits into a variable. Look at direct port addressing for that.

Can't help with the connectivity problem.

Looking at the code, it's far too complicated. You are partially building an exor function using if statements (part of it shown below)

  if (digitalRead(A_1Switch == HIGH) && (digitalRead(B_1Switch) == LOW))
  {
    digitalWrite(ledPinDS, HIGH); //For when A1 read 1 and B1 reads 0
  }

  if (digitalRead(A_1Switch == LOW) && (digitalRead(B_1Switch) == HIGH))
  {
    digitalWrite(ledPinDS, HIGH); //For when A1 read 0 and B1 reads 1
  }

C has built-in functions for that.

The above also contains some errors with braces (e.g. if (digitalRead(A_1Switch == HIGH) ... should be if ((digitalRead(A_1Switch) == HIGH) ...

C code for exor / logical and / logical or

int exor = digitalRead(A_1Switch) ^ digitalRead(B_1Switch);
int and = digitalRead(A_1Switch) & digitalRead(B_1Switch);
int or = digitalRead(A_1Switch) | digitalRead(B_1Switch);

Based on this image of a 4-bit adder (not my picture hence a link) you can do something like

  R1  = value_A1 ^ value_B1 ^ carry;
  carry = ((value_A1 ^ value_B1) && carry) | (value_A1 & value_B1);

where R1 is the output of the output for A1 and B1 and carry is obviously the carry.

Below the full 4-bit adder (learn from it :wink: )

void loop()
{
  // carry 'bit'
  int carry = 0;

  // read and store the values of the buttons
  int value_A1 = digitalRead(A_1Switch);
  int value_A2 = digitalRead(A_2Switch);
  int value_A3 = digitalRead(A_3Switch);
  int value_A4 = digitalRead(A_4Switch);

  int value_B1 = digitalRead(B _1Switch);
  int value_B2 = digitalRead(B _2Switch);
  int value_B3 = digitalRead(B _3Switch);
  int value_B4 = digitalRead(B _4Switch);

  // the result
  int R1 = 0;
  int R2 = 0;
  int R3 = 0;
  int R4 = 0;

  R1  = value_A1 ^ value_B1 ^ carry;
  carry = ((value_A1 ^ value_B1) && carry) | (value_A1 & value_B1);

  Serial.print("A1 = "); Serial.print(value_A1);
  Serial.print(", B1 = "); Serial.print(value_B1);
  Serial.print(", R1 = "); Serial.print(R1);
  Serial.print(", C  = "); Serial.println(carry);

  R2  = value_A2 ^ value_B2 ^ carry;
  carry = ((value_A2 ^ value_B2) && carry) | (value_A2 & value_B2);

  Serial.print("A2 = "); Serial.print(value_A2);
  Serial.print(", B2 = "); Serial.print(value_B2);
  Serial.print(", R2 = "); Serial.print(R2);
  Serial.print(", C  = "); Serial.println(carry);

  R3  = value_A3 ^ value_B3 ^ carry;
  carry = ((value_A3 ^ value_B3) && carry) | (value_A3 & value_B3);

  Serial.print("A3 = "); Serial.print(value_A3);
  Serial.print(", B3 = "); Serial.print(value_B3);
  Serial.print(", R3 = "); Serial.print(R3);
  Serial.print(", C  = "); Serial.println(carry);

  R4  = value_A4 ^ value_B4 ^ carry;
  carry = ((value_A4 ^ value_B4) && carry) | (value_A4 & value_B4);

  Serial.print("A4 = "); Serial.print(value_A4);
  Serial.print(", B4 = "); Serial.print(value_B4);
  Serial.print(", R4 = "); Serial.print(R4);
  Serial.print(", C  = "); Serial.println(carry);


}

I've used serial print statements, you can modify this to shift the bits out.

Note: I've used this so it more closely resembles a hardware implementation (as you were doing). You could simply add 2 numbers (based on your inputs), check if the 5th bit is set to detect the last carry, subtract 16 etc. But that would have been less fun.

Well, the sn74hc595n is not a processor. Perhaps that is your problem. :slight_smile: