#include <Arduino.h>
const int sensorPin = 2;
volatile unsigned long count = 0;
unsigned long previousMillis = 0;
unsigned long interval = 1000;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(sensorPin), countRevolutions, RISING);
}
//----------------------------------------------------------------------
void countRevolutions() {
count++; // Increment count on each interrupt
}
//----------------------------------------------------------------------
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
float rpm = (count / 1.0) * (60000.0 / interval); // Assuming 1 pulses per revolution
Serial.print("RPM: ");
Serial.println(rpm,0);
// Reset count
count = 0;
// Update previous time
previousMillis = currentMillis;
}
}