const int sensorPin = 4; // Pin del sensor IR
volatile int contador = 0; // Contador de pulsos
unsigned long tiempoAnterior = 0;
int rpm = 0;
void IRAM_ATTR contarPulso() {
contador++; // Se ejecuta cada vez que detecta un pulso
}
void setup() {
Serial.begin(115200);
pinMode(sensorPin, INPUT);
// Interrupción: detecta cuando el sensor cambia a LOW
attachInterrupt(digitalPinToInterrupt(sensorPin), contarPulso, FALLING); // RISING, FALLING, CHANGE
}
void loop() {
unsigned long tiempoActual = millis();
// Cada 1 segundo calcula RPM
if (tiempoActual - tiempoAnterior >= 1000) {
// RPM = pulsos por segundo * 60
rpm = contador * 60;
Serial.print("RPM: ");
Serial.println(rpm);
contador = 0;
tiempoAnterior = tiempoActual;
}
}