Using Magnetic Encoder to count RPM

Hi. I'm making some code to count RPM of motor shaft by using Magnet encoder.
My idea is to use mills() but it doesn't work. So I'm asking some help from you.

  volatile unsigned long time1 = 0;
  volatile unsigned long time2 = 0;

  sensorValue = analogRead(ENCODER_PIN);
  if(sensorValue < 200)
    time1 = (unsigned long)millis();
  if(sensorValue > 4000)
    time2 = (unsigned long)millis();
  rpm = (1 / (time2 - time1)) * 1000 * 60;

*if sensorValue is less than 200, it represents 0 degree. And when sensor value is over 4000, it represents 360 degree. Cuz it's 16bit.

I think I'm making mistakes by using millis but I don't know why.
Pls. help :slight_smile:

First, you should always post a complete program.

What sort of magnetic encoder are you using (please post a link to its datasheet). I find it hard to imagine that it needs to use analogRead() rather than digitalRead()

What is the maximum speed of the shaft?

...R