RPM Tacho Reader

This is my humble contribution to the automotive community:

#define RPM_PIN 2
#define MAX_RPM_LIMIT 10000
#define PULSE_PER_REV 2
#define BUFFER_LENGTH 5

int bufferIndex = 0;
int buffer[BUFFER_LENGTH];

void setup() {
  
  Serial.begin(9600);

  pinMode(RPM_PIN, INPUT);
  digitalWrite(RPM_PIN, HIGH);

}


void loop() {

  float pwRaw = pulseIn(RPM_PIN, LOW);

  buffer[bufferIndex] = pwRaw;
  bufferIndex++;

  if (bufferIndex > BUFFER_LENGTH) { 

    float pw = 0;
    int countRpm = 0, int maxCountRpm = 0;
    
    for (int j = 0; j < BUFFER_LENGTH; j++) {
      for (int i = 0; i < BUFFER_LENGTH; i++) {
        if (buffer[j] == buffer[i]) countRpm++;
      }
      if (countRpm > maxCountRpm) { 
            pw = buffer[j]; 
            maxCountRpm = countRpm; 
      }
      countRpm = 0;
    }
    
    float period = pw * 2;                        
    int rpm = 60000000 / period / PULSE_PER_REV;     
    if (rpm < 0 || rpm > MAX_RPM_LIMIT) rpm = 0;
    
    Serial.print("RPM: ");
    Serial.print(rpm);
      
    bufferIndex = 0; 

  }

}

Let me know if something seems wrong.