From 4-Bit-Dualnumber reader + LED binary counter to using an 4-Bit adder

Hi,

I am not new and know a couple of things. Also I know basics of electrical engineering. On top of that I tried various approaches and tests and am stuck in an actually not that hard task.

I had to do this because I had to check what’s wrong with the Nano. 2 digital PINS are broke. Number 6 and 7. Proofed this with another Arduino (Mega2560 R3)

I connected each 4-Row-LEDs with the MSB in annother color and with one 220 OHM for each of the 4 LEDs using the red LEDs so my 5V’s are enough to get some light. The cathodes are at the (-), the anodes at Pins.

The loop problem is solved.

I am not lazy I sat infront of this for an unacctable amount of time and am getting grumpy. I keep working at it but please comment me device and/or solution.

#include "Arduino.h"

byte binwert;                      // defines variable as byte
int ledPinsA[] = {2, 3, 4, 5};     //array with Pinnumber of first 4 LEDs (Row A)
int ledPinsB[] = {8, 9, 10, 11};   //2. array for row B
int pinCountA = 4;
int pinCountB = 4;

void setup () {
  Serial.begin(9600);              //Serielle monitor for debugging
for(int PINa = 0; PINa < pinCountA; PINa++){
  pinMode(ledPinsA[PINa], OUTPUT);}
for(int PINb = 0; PINb < pinCountB; PINb++){
  pinMode(ledPinsB[PINb], OUTPUT); }}

void loop() {
  delay(2000);

for (int PINa = 0; PINa < pinCountA; PINa++){
  digitalWrite(ledPinsA[PINa], LOW);}                   //Row A lights out
for(int PINb = 0; PINb < pinCountB; PINb++){
  digitalWrite(ledPinsB[PINb], LOW);}                   //Row B Lights out
 
int A0;                                    //1. variable definieren for saving serial.parseInt 
long A1;                                  //2. long to safe Int as byte         
bool var = Serial.available();    //3. long define to check what's given from serial
  
  if (var) {    
    A0 = Serial.parseInt();
    A1 = (byte) A0;
    Serial.print(A1, BIN); 
    for(int n; n < 9; n++){
    if (bitRead(A1, n) == true){  
      digitalWrite(ledPinsA[n], HIGH); // Lights on array A   
      delay(1000); } 
      else {
      digitalWrite(ledPinsA[n], LOW);}   // else Lights out array A
              
    if (bitRead(A1,n) == 1) {
      digitalWrite(ledPinsB[n], HIGH); 
      delay(1000);}
    else {digitalWrite(ledPinsB[n], LOW);} 
}
}
}

Aufgabe_2a.ino (1.78 KB)

Hello and welcome To-bias.

I am not lazy I sat in front of this for an unacceptable amount of time and am getting grumpy.

We get grumpy when people ask questions without providing anything like enough information. Please can you read General guidance and
How to use this forum

My immediate thoughts on reading your question is that I need to see a schematic, hand drawn and photographed is fine, and I need to know what each item of hardware is that you are using. I don’t know what a 4-Bit-Dualnumber-LED-Counter is, although I might guess, but even if I guess I might get the wrong one. Please provide the part number of the device.

Please post your code in code tags </> as per the instructions.

Thank you.

There was an issue with uploading the jpg and it uploaded the post anyway.

The Dual-number-led-counter works now. Just only once. Part number is mentioned or what do you mean apart from Arduino Mega2560 and Arduino Nano?

The adder is going to be 74LS283 Motorola 4-BIT binary full adder with fast carry.

How to post an image

Code in code tags?
Schematic?
Part numbers?

PerryBebbington:
How to post an image

Code in code tags?
Schematic?
Part numbers?

Added and commented the code. Added the version of Arduino Mega and postet the adder number. Red LEDs 1.6V, yellow 2.1V, OHM as mentioned in the first thing.

I want to add a third row of LEDs for the results of the adder and connect the first two rows to the adder now.

Your array ledPins has 4 elements

int ledPinsA[] = {2, 3, 4, 5};     //array with Pinnumber of first 4 LEDs (Row A)

Here you are trying to access 9 of them…

for(int n; n < 9; n++){
if (bitRead(A1, n) == true){  
      digitalWrite(ledPinsA[n], HIGH); // Lights on array A

There’s at least 12 seconds delay in loop(), maybe more I missed. How long were you wating for loop() to go round once?

When it comes to reading other people’s code I am not the best, there are probably other mistakes I have missed.

Thanks for your help, it was this very line. I just had to type a

“for(n=0; n<4; n++)”

Even though it’s working with a 9 instead of a 4. At first it worked as continous counter and the array was worked through because of the delays. Without them it’s just the sudden binary blink of both rows simultaneously. I thought the n’s just to read the byte and has nothing to do with the number of LEDs.
The first problem was to start n from 0.
The second to understand n’s total task.

I followed your pic- and code instructions already. I am not going to post a schematic as the thing is described pretty detailed in the first post and this is not going anywhere.

2 Rows of LEDs as rows in DC circuit. One 220 Ω between the GND and the - of each Row as it can be seen within the picture.

Works with both Arduinos now. Next stop is to connect the adder so maybe don’t close the thread yet.

Thanks for your help.

You're welcome :slight_smile:

I tried various approaches and tests and am stuck in an actually not that hard task.

When that happens to me I turn the computer off and go to bed. Usually, after some sleep, the problem becomes obvious.

If you go outside the bounds of an array sometimes you get away with it, sometimes you get weird behaviour with no obvious cause.

delay() is my pet hate, it's fine for testing but don't use it in any serious programming, it just clogs things up and stops anything from happening. In answer to the question "my code is not very responsive and I don't know why", which you will be asking in a month or 3; "It's because of all the delays".

I wrote some more Code to divide to dual numbers and let them be seen by LED's.
Also there is more code now to support addition and subtraction (basically .csv) in the Serial. It gives out the results in LED and I want the adder 74LS283 Motorola 4-BIT binary full adder with fast carry to show the results within a third row of LED's connected to the SUM-PINs of the adder.

Now the algebra works fine and I can type in addition after subtraction and so on. Only the LED's just don't show the separated values even though I separated them in the Code.

Actually the Programm doesn't save MY number within A1 and B1 (Variables to show in LED-Row(as in DC Row)). I connected all inputs of the adder to the anodes of the LED's.
I checked the adders logic gatter and the first Sum LED is glowing for no input so that works. Only it doesn't add anything + I don't know where to connect the C0 and C1 of the adder to.

This is my Code:

#include "Arduino.h"

int ledPinsA[] = {2, 3, 4, 5};     //array with pinnumbers
int ledPinsB[] = {8, 9, 10, 11};   //2. array for B LEDs
int pinCountA = 4;
int pinCountB = 4;


void setup () {
  Serial.begin(9600);              //Serial for debugging
for(int PINa = 0; PINa < pinCountA; PINa++){
  pinMode(ledPinsA[PINa], OUTPUT);}
for(int PINb = 0; PINb < pinCountB; PINb++){
  pinMode(ledPinsB[PINb], OUTPUT); }}

void loop() {
  delay(1000);

for (int PINa = 0; PINa < pinCountA; PINa++){
  digitalWrite(ledPinsA[PINa], LOW);}
for(int PINb = 0; PINb < pinCountB; PINb++){
  digitalWrite(ledPinsB[PINb], LOW);}

                                  
long D=0;                             
while (Serial.available()>0){        //stays in this loop until somethings available in the Serial
    int A = Serial.parseInt();       //int A definition as first value 
    int B = Serial.parseInt();       //int B definition 
  if (Serial.read() == '\n') {
    int constrainedA = constrain(A, -8, 7);   //saves A as constrained int
    int constrainedB = constrain(B, -8, 7);
    int C=0;
    C = A + B;
    Serial.print(C, DEC);  //Gives C as Dec-number in Serial  
   
    A = (byte) A1;                        //PROBABLY THE PROBLEM: Saves int A in a Byte A1
    B = (byte) B1;
    }  
    
    for(int n=0; n < 4; n++){                  //n for Bitinfo 
     if (bitRead(A1, n) == true){  
      digitalWrite(ledPinsA[n], HIGH);}        // array ledPinsA to 1           
     else {digitalWrite(ledPinsA[n], LOW);}    
                 
     if (bitRead(B1, n) == true) {
      digitalWrite(ledPinsB[n], HIGH);}        
     else {digitalWrite(ledPinsB[n], LOW);}    
      
    }
  }
}

Adder.PNG
Addierer - 74LS283.pdf (50.3 KB)

I am supposed to use this XOR 74LS86 to build an LED-adder with an adder and the XOR:

XOR.PNG

Adder.PNG

Addierer - 74LS283.pdf (50.3 KB)

XOR.PNG

I am not going to post a schematic as the thing is described pretty detailed in the first post and this is not going anywhere.

You're almost right. Let me rephrase that. This is not going anywhere without a schematic.

Is this schematic and description enough? I don’t have anything else right now. (See last edited post)

No, it is not enough. A schematic shows how things are connected together. Nothing you posted shows that. It's likely that not having one for yourself to use during construction, is contributing greatly to your difficulties.

Edit:

I had a wrong circuit and illogical bindings. This is the circuit done with Eagle

UPDATE:

Using the schematic and wiring everything new the adder is working now. I also have a resistor behind every LED now. The adding works perfectly and my serial is adding and substracting.

The XOR ist wired as the schematic says and I can control the PIN VCC trough PIN 8. Now we have to figure out how to manipulate it to get the subtractor working.

These 4 lines didn’t work:

 if (A < 0 || B < 0){
    digitalWrite(8, HIGH);
   }
   else {
    digitalWrite(8,LOW);
   }

I put them behind "Serial.print(C, DEC); "

Without the XOR-Hardware-Element every MSB is wrong when subtracting. XOR inverts every Bit of variable B.

Difficulties finding der logical on-off-status of der XOR. It’s me having problems understanding the binaries.