1

I have to code in arduino the logic gates based on the truth table with two inputs. I searched for some examples, however all that I found is using && to compared them using only onf if else condition. What I need is to do it using all the possible cases. This is what I have, however only the last condition is working. I ask you please clarify to me what concept I am not understanding well, or what is wrong in my logic. Thank you.

  const int p2  = 2;  //Ground G
  const int p13 = 13; //Input A
  const int p15 = 15; //Input B

  void setup() {
    // put your setup code here, to run once:
    pinMode(p2,   OUTPUT);
    pinMode(p13,  INPUT_PULLUP);
    pinMode(p15,  INPUT_PULLUP);
  }
void loop() {
  // put your main code here, to run repeatedly:
  if ( digitalRead(p13) == 0 && digitalRead(p15) == 0 ){
    digitalWrite(p2, HIGH);
  }

  if( digitalRead(p13) == 0 && digitalRead(p15) == 1 ) {
    digitalWrite(p2, LOW);
  }

  if( digitalRead(p13) == 1 && digitalRead(p15) == 0 ) {
    digitalWrite(p2, LOW);
  }

  if( digitalRead(p13)== 1 && digitalRead(p15) == 1) {
    digitalWrite(p2, LOW);
  }

}
2
  • 1
    "What I need is to do it using all the possible cases." Uh... there are only two possible cases. Either they are both HIGH, or the output is LOW. – Ignacio Vazquez-Abrams Oct 12 '16 at 8:22
  • Well, I am thinking in terms of input and output, no matter if the output is high or low, so there are 4 cases. I know what you mean, it is just this is an assignment. So I would like to check with the pin in my board, I do not know if I explain well, I doing my best. – Sora Oct 12 '16 at 8:29
2

Let me give some feedback on your code, kind of a small code review:

const int p2  = 2;  //Ground G
const int p13 = 13; //Input A
const int p15 = 15; //Input B

This is useless. The purpose of creating these constants is to give them meaningful names, and p2 is no more meaningful than just 2. You can remove these definitions and replace in the following p2 with 2, etc. Or you could find better names:

const int inputA_pin = 13;
const int inputB_pin = 15;
const int output_pin = 2;

Notice that the comments that you put next to the constants are not needed anymore, as the names are now explicit enough.

void setup() {
    // put your setup code here, to run once:

This comment is useless, better remove it. It is in BareMinimum.ino so that you know what to put there, but now that you have done it, no need to keep the comment.

if (digitalRead(p13) == 0 && digitalRead(p15) == 0) {
    digitalWrite(p2, HIGH);
}

I assume you are using negative logic: the user presses a button, and makes the input LOW, to mean TRUE. If this is the case, you are not being clear enough about it. You should at least have a comment to make this explicit, e.g.:

int inputA = !digitalRead(inputA_pin);  // negative logic

where ! is the NOT logic operator.

Otherwise your program seems fine. If it does not work, check your wirings. John Burger's suggestions on how to make it better are very sound, and you should probably follow them. But I would like to show you another way of writing this that could be relevant to your course: you can implement the truth table as an actual table, i.e. a C++ array. Here is my version:

const int inputA_pin = 13;
const int inputB_pin = 15;
const int output_pin = 2;

const bool truth_table[2][2] = {
            /* false  true  */
/* false */  { false, false },
/* true  */  { false, true  }
};

void setup() {
    pinMode(inputA_pin, INPUT_PULLUP);
    pinMode(inputB_pin, INPUT_PULLUP);
    pinMode(output_pin, OUTPUT);
}

void loop() {
    int inputA = !digitalRead(inputA_pin);  // negative logic
    int inputB = !digitalRead(inputB_pin);  // ditto
    int output = truth_table[inputA][inputB];
    digitalWrite(output_pin, output);
}

Notice that this uses the equivalence between false/true and 0/1.

In the case of the AND gate, this looks just like a silly classroom exercise. However, for a complex gate with four inputs or more, and a non-trivial truth-table, this technique may be the most straightforward implementation.


Addendum – You wrote in a comment:

Not clear how your truth table works.

You are probably confused by the fact that I laid out the table in compact form, with the first input in the row headings (comments on the left edge) and the second input in the column headings (comments at the top), just like a traditional multiplication table. Incidentally, it happens to be a 2×2 multiplication table.

I could have used the more common “flat” layout, where each combination of inputs is represented as one line of the table:

const bool truth_table[] = {
    /*   A     B     output  */
    /*-----------------------*/
    /* false false */ false,
    /* false true  */ false,
    /* true  false */ false,
    /* true  true  */ true
};

But then, since it's now a 1D table, the table index has to be computed from both inputs, as each input provides one bit of the index:

int output = truth_table[inputA<<1 | inputB];

This is actually how I would do it if I had to simulate a gate with more than two inputs. I've used this technique to make a 2-bit Gray code adder (4 inputs, 2 outputs) which, with a little bit of optimization, boils down to

for (;;) PORTB = truth_table[PINB & 0x0f];
3
  • Your explanation is very clear, I will try it. Negative logic is Because the board I am using has the LED weld to it. Not clear how your truth table works. – Sora Oct 13 '16 at 2:10
  • @Sora: You may find the truth table easier to understand if it has the more common flat layout. See expanded answer. – Edgar Bonet Oct 13 '16 at 10:11
  • I've got it, again your explanation has many details. Do not why the led is on if I have for A = false and B = true, in this case the led must to be turn off, the other cases are ok, should be something wrong with the pin of my board? – Sora Oct 16 '16 at 14:37
3

Your whole code can be replaced with one line:

digitalWrite(output_pin, digitalRead(inputA_pin) && digitalRead(inputB_pin));

That is, write out the results of a boolean AND on the state of the two inputs.

0

Your question title says "and", which means that if input1 is HIGH AND input2 is HIGH, then output should be HIGH. Your first line has

(...==0 && ...=0) digitalWrite(..., HIGH);

Surely these should be ==1?

Another thing is that every line of code in your program is re-reading the inputs. You should read them once, then decode the meaning behind them. So:

int input1 = digitalRead(p13);
int input2 = digitalRead(p15);

if (input1==1 && input2==1) {
    digitalWrite(p2, HIGH);
} // if
else if (input1==0 && input2==0) {
    digitalWrite(p2, LOW);
} // else if
else if ...

But note that the last three options are all the same: the code simply writes LOW. So you can combine all the last three cases into one step.

In other words, there's a single special case (both inputs ==1), and an everything-else case. In this code, I only read the inputs once, since I'm only looking for the special case. If it doesn't exist, then I simply do the alternative:

if (digitalRead(p13)==1 && digitalRead(p15)==1) {
    digitalWrite(p2, HIGH);
} // if
else {
    digitalWrite(p2, LOW);
} // else
2
  • "Your question title says "and", which means that if input1 is HIGH AND input2 is HIGH, then output should be HIGH. " Yes, you are right.It has to be like that in the natural behavior of the and truth table, however here I just followed an example my teacher gave to me. I do not get it. I am using this to turn on and turn off a led. I think he does it like that because the led is already turn it on before the push the pin. is it really a big difference? Got it with the combination. Teacher ask us to write the code as it is in the truth table, that is why I did not combine it. – Sora Oct 12 '16 at 9:07
  • The official designation for what you supplied is called the NOT OR, or NOR. There are two ways to look at it: either it's "Neither input1 NOR input2 is HIGH", or that it's exactly like AND but in the opposite direction. Just to be even more confusing, some systems say that ON is 0 - but in those systems, the result of 0 AND 0 would be 0, not 1. – John Burger Oct 12 '16 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.