All submissions

Logic Gate Implementation

Solving Approach

How do you plan to solve it?

 
 

 

 

 

Code

// Define the pins for inputs and outputs
const int switchA = 2;
const int switchB = 7;

const int orLED = 6;
const int andLED = 3;
const int norLED = 4;
const int nandLED = 5;

void setup() {
  // Set input pins with internal pull-up resistors
  pinMode(switchA, INPUT_PULLUP);
  pinMode(switchB, INPUT_PULLUP);
  
  // Set output pin modes
  pinMode(andLED, OUTPUT);
  pinMode(orLED, OUTPUT);
  pinMode(nandLED, OUTPUT);
  pinMode(norLED, OUTPUT);
  
  // Initialize Serial communication
  Serial.begin(9600);
}

void loop() {
  // Read the state of the switches and invert the logic
  int inputA = !digitalRead(switchA);
  int inputB = !digitalRead(switchB);
  
  // AND Gate Logic
  if (inputA == HIGH && inputB == HIGH) {
    digitalWrite(andLED, HIGH);
  } else {
    digitalWrite(andLED, LOW);
  }

  // OR Gate Logic
  if (inputA == HIGH || inputB == HIGH) {
    digitalWrite(orLED, HIGH);
  } else {
    digitalWrite(orLED, LOW);
  }
  
  // NAND Gate Logic
  if (!(inputA == HIGH && inputB == HIGH)) {
    digitalWrite(nandLED, HIGH);
  } else {
    digitalWrite(nandLED, LOW);
  }

  // NOR Gate Logic
  if (!(inputA == HIGH || inputB == HIGH)) {
    digitalWrite(norLED, HIGH);
  } else {
    digitalWrite(norLED, LOW);
  }
  
  // Print current state to Serial Monitor
  Serial.print("Input A: ");
  Serial.print(inputA);
  Serial.print(" | Input B: ");
  Serial.print(inputB);
  Serial.print(" | AND: ");
  Serial.print(digitalRead(andLED));
  Serial.print(" | OR: ");
  Serial.print(digitalRead(orLED));
  Serial.print(" | NAND: ");
  Serial.print(digitalRead(nandLED));
  Serial.print(" | NOR: ");
  Serial.println(digitalRead(norLED));
  
  delay(0);
}


 

 

 

Output

Video

Add a video of the output (know more)

 

 

 

 

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!