user27344: 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);
  }

}


user7508: 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];


user4143: 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.

27344: <p>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 &amp;&amp; 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.</p>

<pre><code>  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 &amp;&amp; digitalRead(p15) == 0 ){
    digitalWrite(p2, HIGH);
  }

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

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

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

}
</code></pre>
 7508: <p>Let me give some feedback on your code, kind of a small code review:</p>

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

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

<pre><code>const int inputA_pin = 13;
const int inputB_pin = 15;
const int output_pin = 2;
</code></pre>

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

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

<p>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.</p>

<pre><code>if (digitalRead(p13) == 0 &amp;&amp; digitalRead(p15) == 0) {
    digitalWrite(p2, HIGH);
}
</code></pre>

<p>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.:</p>

<pre><code>int inputA = !digitalRead(inputA_pin);  // negative logic
</code></pre>

<p>where <code>!</code> is the NOT logic operator.</p>

<p>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
<em>another</em> 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:</p>

<pre><code>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);
}
</code></pre>

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

<p>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.</p>

<hr>

<p><strong>Addendum</strong> – You wrote in a comment:</p>

<blockquote>
  <p>Not clear how your truth table works.</p>
</blockquote>

<p>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 <a href="https://commons.wikimedia.org/wiki/File:Tpitagoras.gif" rel="nofollow">multiplication table</a>. Incidentally, it
happens to be a 2×2 multiplication table.</p>

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

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

<p>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:</p>

<pre><code>int output = truth_table[inputA&lt;&lt;1 | inputB];
</code></pre>

<p>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 <a href="https://www.logre.eu/wiki/Codeur_rotatif_incr%C3%A9mental#Solution_avec_un_microcontr.C3.B4leur" rel="nofollow">2-bit Gray code
adder</a> (4 inputs, 2 outputs) which,
with <a href="https://www.arduino.cc/en/Reference/PortManipulation" rel="nofollow">a little bit of optimization</a>, boils down to</p>

<pre><code>for (;;) PORTB = truth_table[PINB &amp; 0x0f];
</code></pre>
 4143: <p>Your whole code can be replaced with one line:</p>

<pre><code>digitalWrite(output_pin, digitalRead(inputA_pin) &amp;&amp; digitalRead(inputB_pin));
</code></pre>

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