const int inputPin = 15; // Define the pin for the input signal
volatile unsigned long pulseCount = 0; // Variable to store pulse count
unsigned long startTime; // Variable to store the start time
unsigned long frequency = 0; // Variable to store the frequency
void IRAM_ATTR countPulse() {
pulseCount++; // Increment pulse count each time an interrupt occurs
}
void setup() {
Serial.begin(115200);
pinMode(inputPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(inputPin), countPulse, RISING); // Count rising edges
startTime = millis(); // Initialize start time
}
void loop() {
unsigned long currentTime = millis();
// Update every second (1000 milliseconds)
if (currentTime - startTime >= 1000) {
// Calculate frequency
frequency = pulseCount;
pulseCount = 0; // Reset pulse count
startTime = currentTime; // Reset start time
// Print frequency to Serial Monitor
Serial.print("Frequency: ");
Serial.print(frequency);
Serial.println(" Hz");
}
}