Arduino 4 input and 4 output programm

 To implement a program where the output is low if any one of the four inputs is low, you can use the following code in the loop() function:#include <Adafruit_SSD1306.h> // Include the OLED library

#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(OLED_RESET); // Create an instance of the OLED display


const int inputPin1 = 2; // Input pin 1

const int inputPin2 = 3; // Input pin 2

const int inputPin3 = 4; // Input pin 3

const int inputPin4 = 5; // Input pin 4


const int outputPin1 = 6; // Output pin 1

const int outputPin2 = 7; // Output pin 2

const int outputPin3 = 8; // Output pin 3

const int outputPin4 = 9; // Output pin 4


volatile int count1 = 0; // Count variable for output pin 1

volatile int count2 = 0; // Count variable for output pin 2

volatile int count3 = 0; // Count variable for output pin 3

volatile int count4 = 0; // Count variable for output pin 4


void setup() {

  Serial.begin(9600); // Initialize serial communication

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize the OLED display

  display.clearDisplay(); // Clear the display

  display.setTextSize(2); // Set the text size

  display.setTextColor(SSD1306_WHITE); // Set the text color

  display.setCursor(0, 0); // Set the cursor position


  // Set input pins as inputs

  pinMode(inputPin1, INPUT);

  pinMode(inputPin2, INPUT);

  pinMode(inputPin3, INPUT);

  pinMode(inputPin4, INPUT);


  // Set output pins as outputs

  pinMode(outputPin1, OUTPUT);

  pinMode(outputPin2, OUTPUT);

  pinMode(outputPin3, OUTPUT);

  pinMode(outputPin4, OUTPUT);


  // Enable interrupts on the input pins using rising edge trigger

  attachInterrupt(digitalPinToInterrupt(inputPin1), incrementCount1, RISING);

  attachInterrupt(digitalPinToInterrupt(inputPin2), incrementCount2, RISING);

  attachInterrupt(digitalPinToInterrupt(inputPin3), incrementCount3, RISING);

  attachInterrupt(digitalPinToInterrupt(inputPin4), incrementCount4, RISING);

}


void loop() {

  // Display the count for each output pin on the serial monitor

  Serial.println("Count:");

  Serial.print("Output 1: ");

  Serial.println(count1);

  Serial.print("Output 2: ");

  Serial.println(count2);

  Serial.print("Output 3: ");

  Serial.println(count3);

  Serial.print("Output 4: ");

  Serial.println(count4);


  // Display the count for each output pin on the OLED display

  display.clearDisplay();

  display.setCursor(0, 0);

  display.println("Count:");

  display.print("Output 1: ");

  display.println(count1);

  display.print("Output 2: ");

  display.println(count2);

  display.print("Output 3: ");

  display.println(count3);

  display.print("Output 4: ");

  display.println(count4);

  display.display();


  delay(1000); // Wait for 1 second before updating the count

}


void incrementCount1() {

  count1++; // Increment the count for output pin 1

  digitalWrite



const int inputPin1 = 2;  // Input pin 1

const int inputPin2 = 3;  // Input pin 2

const int inputPin3 = 4;  // Input pin 3

const int inputPin4 = 5;  // Input pin 4

const int outputPin = 6;  // Output pin


void setup() {

  pinMode(inputPin1, INPUT);

  pinMode(inputPin2, INPUT);

  pinMode(inputPin3, INPUT);

  pinMode(inputPin4, INPUT);

  pinMode(outputPin, OUTPUT);

}


void loop() {

  if (digitalRead(inputPin1) == LOW || digitalRead(inputPin2) == LOW ||

      digitalRead(inputPin3) == LOW || digitalRead(inputPin4) == LOW) {

    digitalWrite(outputPin, LOW);  // Set output pin LOW

  } else {

    digitalWrite(outputPin, HIGH);  // Set output pin HIGH

  }

}

In this code, four input pins (inputPin1, inputPin2, inputPin3, inputPin4) and one output pin (outputPin) are defined. In the setup() function, the input pins are set as inputs, and the output pin is set as an output.


In the loop() function, the code checks the state of each input pin using digitalRead(). If any one of the input pins reads LOW, the output pin is set to LOW using digitalWrite(). Otherwise, if all input pins are HIGH, the output pin is set to HIGH.


You can connect your input devices (e.g., switches or sensors) to the defined input pins and connect an output device (e.g., an LED or relay) to the output pin. The output device will be driven LOW if any one of the input pins is LOW.







Comments

Popular Posts