1

hello guys is there someone who can help me with my first project. its basically about logic circuits.

by using tact switch and pull-up resistor to set the values to logical 1 or 0. the output can be verified by using a LED as an indicator. so the code goes like this:

//AND GATE          
int pin2=1;         //set pin2=1
int pin3=1;         //set pin3=1

void setup()
{
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,OUTPUT);
pinMode(11,OUTPUT);    
pinMode(12,OUTPUT);
}
void loop()
{
  pin2=digitalRead(2);
  pin3=digitalRead(3);

 if(pin2==1)            //turn led on pin11 when pin2 =1
 {
 digitalWrite(11,HIGH);
 }
 else
 {
 digitalWrite(11,LOW);
 }

 if(pin3==1)          ////turn led on pin12 when pin3 =1
 {
   digitalWrite(12,HIGH);
 }
 else
 {
   digitalWrite(12,LOW);
 }

//TRUTH TABLE OF AND 

  if(pin2 && pin3)       
  {
    digitalWrite(4,HIGH);
  }
  else
  {
  digitalWrite(4,LOW);
  }
}

//OR GATE          
int pin2=1;
int pin3=1;

void setup()
{
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop()
{
  pin2=digitalRead(2);
  pin3=digitalRead(3);

 if(pin2==1)            
 {
 digitalWrite(11,HIGH);
 }
 else
 {
 digitalWrite(11,LOW);
 }

 if(pin3==1)          
 {
   digitalWrite(12,HIGH);
 }
 else
 {
   digitalWrite(12,LOW);
 }

//TRUTH TABLE OF OR 

  if(pin2 || pin3)       
  {
    digitalWrite(4,HIGH);
  }
  else
  {
  digitalWrite(4,LOW);
  }
}

//NAND GATE          
int pin2=1;
int pin3=1;

void setup()
{
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop()
{
  pin2=digitalRead(2);
  pin3=digitalRead(3);

 if(pin2==1)            
 {
 digitalWrite(11,HIGH);
 }
 else
 {
 digitalWrite(11,LOW);
 }

 if(pin3==1)          
 {
   digitalWrite(12,HIGH);
 }
 else
 {
   digitalWrite(12,LOW);
 }

//TRUTH TABLE OF NAND 

  if(!(pin2 && pin3))       
  {
    digitalWrite(4,HIGH);
  }
  else
  {
  digitalWrite(4,LOW);
  }
}

can you guys help me to run this in one sketch by using tact switch to select the logic gate to be tested or run? thank you in advance

  • 1
    This cannot work: pin2, pin3, setup() and loop() are all defined three times. You cannot paste three programs one after the other and expect the whole to be a valid program. Just run one program at a time. If you have no push button, use a wire that you manually connect and disconnect to/from GND (if using a pullup resistor) or 5V (if using a pulldown). – Edgar Bonet Aug 6 '16 at 11:35
  • @EdgarBonet actually combining both programs in one seems to be the actual question here :-) – jfpoilpret Aug 7 '16 at 8:48
2

For simulating different gates with the same program, an option is to use the serial port to tell it what gate you want it to simulate. Here I am also using a function pointer (called gate) to represent the selected gate:

/* Wiring. */
const int inputA    = 2;
const int inputB    = 3;
const int outputA   = 11;
const int outputB   = 12;
const int outputRes = 4;

void setup()
{
    pinMode(inputA,    INPUT_PULLUP);
    pinMode(inputB,    INPUT_PULLUP);
    pinMode(outputA,   OUTPUT);
    pinMode(outputB,   OUTPUT);    
    pinMode(outputRes, OUTPUT);
    Serial.begin(9600);
    Serial.println(F("Type 'a'(for AND), 'o' (OR) or 'n' (NAND)."));
}

/* Available gates. */
bool AND(bool a, bool b)  { return a && b; }
bool OR(bool a, bool b)   { return a || b; }
bool NAND(bool a, bool b) { return !(a && b); }

/* The gate we want to test. */
bool (*gate)(bool a, bool b) = AND;

void loop()
{
    // 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);
    digitalWrite(outputRes, gate(a, b));
}

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);
}
1

What about something like this:

const uint8_t    inA =  2;
const uint8_t    inB =  3;
const uint8_t   outA =  4;
const uint8_t   outB = 11;
const uint8_t   outY = 12;
const uint8_t button =  5;

void setup() {
    pinMode(inA, INPUT_PULLUP);
    pinMode(inB, INPUT_PULLUP);
    pinMode(outA, OUTPUT);
    pinMode(outB, OUTPUT);
    pinMode(outY, OUTPUT);

    pinMode(button, INPUT_PULLUP);
}

uint32_t check_time = 0;

uint8_t     counter = 0;
uint8_t       funct = 0b0110; // start with XOR  

void loop() {
    // active low: (for usage with buttons connected to ground with internal pullup)
    // uint8_t state = (digitalRead(inA)==LOW) | (digitalRead(inB)==LOW)<<1;

    // active high:
    uint8_t state = (digitalRead(inA)==HIGH) | (digitalRead(inB)==HIGH)<<1;

    digitalWrite(outA, (state & 1) > 0);
    digitalWrite(outB, (state & 2) > 0);

    digitalWrite(outY, ((funct >> state) & 1) > 0);


    // button handling
    uint32_t actual = millis();
    if (actual > check_time) {
        check_time = actual + 4; // check every 4ms

        if (digitalRead(button) == LOW) { // expects pull-up and active low
            if (++counter == 4) { // debounce - confirmed after 16ms (4*4) and every 256*4 ms again
                funct = (funct + 1) & 0xF;
            }
        } else {
            counter = 0; // reset counter
        }
    }
}

It uses push button to cycle through all 16 possible functions of two inputs. Default is XOR. Assumption is Active HIGH inputs (you can use INPUT instead of INPUT_PULLUP).

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.