Binary adder/subtracter

Hi, I'm very new to the forum (about 5 min actually) so just correct me if I am saying anything stupid.

I have this schoolproject and my group is making a 4 bit adder/subtractor with a whole lot of breadboards and the arduino. We use serial from the PC to arduino and are able to give the arduino input A and B to the adder. This works fine! Communication between the adder and the arduino goes via a data-pin and a clock-pin. Same with the data back to the arduino. The whole setup is PC--> arduino-->(data+clock-pin)SIPO-element that converts the 4 bit signals to four individual inputs in te adder with help from the clock (shift register)--> adder-->4bit +carry out to PISO-element where the opposite is done. WIth help from a 555-timer, the 5 bits are put into a single pin and brought back to the arduino. The clock is there to tell the arduino that a signal is actually incomming, just in case the signal start with a 0.

The problem lies in the programming when the signal reaches the arduino again. How do I make the arduino understand these 5 bits and convert them into decimal number that show up in the serial monitor..?

Help for this would give you at least 5gazilion of these famous internet-cookies and a warm thought from myself!!!

Hope someone can answer me, quickly hopefully :stuck_out_tongue:

(I am a noob at arduino and general coding)

Schematic?
Existing code?

I wonder why you are doing it in this very obscure way. If you have a five bit binary address why not just feed it from ten arduino pins?

The code is posted further down. My mistake trying to copy the whole code into the text bow without putting it in these fancy code-boxes

Ops, that code didn't really look too good in the forum... :astonished:

as a reply to Grumpy_Mike, do you mean having a clock and data-pin for all the five outputs?
Well, ofcourse that would indeed make it a lot easier with the && statements that I can't remember the name of, but we wanted there to be as few cables as possible, also it is a schoolproject, so doing it the "hard" way is probably good :wink: (but I do see your point)

Sorry again, the int 100 was a mistake, just ignore it XD

Please read the how to use this forum sticky and post the code correctly.
Postcode that at least compiles.
Code is useless without a schematic of what you are trying to drive.

Sorry! Let's make another try
Like the last one I cut out the middle part with the statements for 2-8 due to lack of space

void setup()
{

 pinMode(13, OUTPUT);   //DATA IN  
 pinMode(12, OUTPUT);  //CLOCK IN
 pinMode(11, INPUT);   //DATA OUT
 pinMode(10, INPUT);   //CLOCK OUT
digitalWrite(13, LOW);
digitalWrite(12, LOW);

Serial.begin(9600);
}

void loop()
{
  if(Serial.available() >= 0)
  {
 char letter = Serial.read();
 
                                                 if(letter =='0')
               {
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                

               }

                                                                if(letter =='9')
               {
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                

               }
                                                                if(letter =='A') //100
               {
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                

               }
                                                                if(letter =='B') //11
               {
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                

               }
                                                                if(letter =='C') //12
               {
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                

               }
                                                                if(letter =='D') //13
               {
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                

               }
                                                                if(letter =='E') //14
               {
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                

               }
                                                                if(letter =='F') //15
               {
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                
                digitalWrite(13, HIGH);
                digitalWrite(12, HIGH);
                delay(100);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                delay(100);                

         }
               
    }
               
}

The schematics, do you mean like a "map" of all the electronic components?
I'm afraid I don't have one, and the adder itself is really complicatet but I have a yenka-file if that helps :confused:

You really need a function to shift out those values.
Do you really need to toggle the data line every time, even if the value doesn't change?

I would suggest something like this:

#define DATA_IN    13
#define CLOCK_IN   12
#define DATA_OUT   11
#define CLOCK_OUT  10

#define DELAY      100

void setup()
{
    pinMode(DATA_IN, OUTPUT);  
    pinMode(CLOCK_IN, OUTPUT);
    pinMode(DATA_OUT, INPUT);
    pinMode(CLOCK_OUT, INPUT);

    digitalWrite(DATA_IN, LOW);
    digitalWrite(CLOCK_IN, LOW);

    Serial.begin(9600);
}

void shiftOut4Bit(const uint8_t value)
{
    // MSB first
    for(uint8_t i = 0x8; i; i >>= 1)
    {
        digitalWrite(DATA_IN, value & i ? HIGH : LOW);
        digitalWrite(CLOCK_IN, HIGH);
        delay(DELAY);
        digitalWrite(CLOCK_IN, LOW);
        delay(DELAY);
    }
}

void loop()
{
    if(Serial.available() >= 0)
    {
        char letter = Serial.read();
        if (letter >= '0' && letter <= '9')
            shiftOut4Bit( letter - '0' );
        else if (letter >= 'A' && letter <= 'F')
            shiftOut4Bit( letter - 'A' + 10 );
    }
}

That of course doesn't answer your questions.
But yes, for that we'll need to know how things are wired up.

The schematics, do you mean like a "map" of all the electronic components?

I mean a diagram showing how all the components are wired together.

How on earth have you managed to make it without a schematic? How do you know it is wired correctly?

Thanks a lot for the code, I will try it tomorrow at school but I am unfortunately not able to use it, since I am so new at this I am not able to explain the mechanics behind your code... Therefore it is probably better to use a piece of code that might not be as elegant and short but at least simple enough :wink:

Attachements include the yenka file for the project. Yenka is a payed program and unless you have it pirated I don't know if you're able to read it. For some reason the file refuses to open here, it's saved in a cloud and looks like it didn't really like that :frowning:

4-bit Full Adder - Jacob.yka (7.58 KB)

Grumpy_Mike

The group consists of three people and I was not in charge for the building of the hardware. The calculator is built from what we have learned from datasheets and the teacher. Took quite a while to troubleshoot it but it works, we have checked :wink:

Attachements include the yenka file for the project

That is totally useless. If you want help then you are going to have to provide information in a form anyone can read.

I'm really sorry but it seems yenka stopped working. I could provide a picture but not untill tomorrow morning I think. I'll post it from another persons PC, is that ok, or what? because a schematic like you see on the arduino tutorials on this site is impossible to provide

HenrikAT:
Thanks a lot for the code, I will try it tomorrow at school but I am unfortunately not able to use it, since I am so new at this I am not able to explain the mechanics behind your code...

Which part of the code do you not understand?
If it's the use of functions or the for loop, I suggest you look into these concepts before proceeding any further in your project.

The only thing that I could imagine is a little harder to understand are the bit operations. To solve the problem you're having regarding reading in the results, you'll need to read up on the bitwise operators, since you're going to need them.

Here is some reading material to get you started:

like you see on the arduino tutorials on this site

If you mean the Fritzing diagrams that show the physical components as a sort of picture then forget it, that is not a schematic.
Attached is an example of a schematic:-

I2C LCD interface.pdf (24.2 KB)

Ok, I understand what it is now, but I still can't provide it to you.

I just wondered, all I want is code that translates two pin input to serial. The first pin is a clock saying when a signal will be available in the other pin. It is going to be 5 bits every time and the delay is constant. Why do you actually need to knwo the schematics of our project?

HenrikAT:
I'm really sorry but it seems yenka stopped working.

I'm not surprised. a: the name and b: it is proprietary. That never bodes well.

Some open source tools such as Fritzing tend to be associated with the Arduino project - it would be as well to swap over to them before you get into more trouble.

Here is a picture of that Yenka file:

Here is a picture of that Yenka file:

Rather too fuzzy to actually see anything.

Why do you actually need to knwo the schematics of our project?

Because without it it is impossible to tell what you actually need, and therefore it it impossible to help you.
What is wrong with a pencil and paper and posting a photograph of the result?

saying

all I want is code that translates two pin input to serial.

Is not helpful because you do not know how to describe what you want. Those words do not make any sense.