const int sensorPin = 2; // Digital pin connected to the sensor
volatile int counter = 0; // Counter for the number of pulses
unsigned long startTime; // Start time of each measurement
void setup() {
pinMode(sensorPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(sensorPin), countPulse, FALLING);
Serial.begin(9600);
}
void loop() {
// Reset the counter and start time for each measurement
counter = 0;
startTime = millis();
// Wait for one second to count pulses
delay(1000);
// Calculate RPM
unsigned long endTime = millis();
unsigned long elapsedTime = endTime - startTime;
float rpm = (float)counter / (float)elapsedTime * 60000.0; // RPM = (Pulses / Time) * 60000 (60 seconds * 1000 milliseconds)
// Display RPM in the serial monitor
Serial.print("RPM: ");
Serial.print(rpm);
Serial.print(" Count: ");
Serial.print(counter);
Serial.print(" Time: ");
Serial.println(elapsedTime);
}
void countPulse() {
counter++;
}