use interrupt pin in arduino
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
volatile int count1 = 0; // Count variable for input pin 1
volatile int count2 = 0; // Count variable for input pin 2
volatile int count3 = 0; // Count variable for input pin 3
volatile int count4 = 0; // Count variable for input pin 4
void setup() {
Serial.begin(9600); // Initialize serial communication
// Set input pins as inputs
pinMode(inputPin1, INPUT);
pinMode(inputPin2, INPUT);
pinMode(inputPin3, INPUT);
pinMode(inputPin4, INPUT);
// 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 input pin on the serial monitor
Serial.println("Count:");
Serial.print("Input 1: ");
Serial.println(count1);
Serial.print("Input 2: ");
Serial.println(count2);
Serial.print("Input 3: ");
Serial.println(count3);
Serial.print("Input 4: ");
Serial.println(count4);
delay(1000); // Wait for 1 second before updating the count
}
void incrementCount1() {
count1++; // Increment the count for input pin 1
}
void incrementCount2() {
count2++; // Increment the count for input pin 2
}
void incrementCount3() {
count3++; // Increment the count for input pin 3
}
void incrementCount4() {
count4++; // Increment the count for input pin 4
}
Comments
Post a Comment