Here is another version, that uses an enum instead of a function
pointer to identify the gate. The constants and the setup() are the
same as before, then:
void loop()
{
// The gate we want to test.
static enum { AND, OR, NAND } gate;
// Select the gate through the serial port.
switch (Serial.read()) {
case 'a': gate = AND; Serial.println("AND"); break;
case 'o': gate = OR; Serial.println("OR"); break;
case 'n': gate = NAND; Serial.println("NAND"); break;
}
// Simulate the gate.
bool a = !digitalRead(inputA); // input is in negative logic
bool b = !digitalRead(inputB);
digitalWrite(outputA, a);
digitalWrite(outputB, b);
bool res = false;
switch (gate) {
case AND: res = a && b; break;
case OR: res = a || b; break;
case NAND: res = !(a && b); break;
}
digitalWrite(outputRes, res);
}