title: Calculate RPM with a quadrature rotary encoder
body: <p>I have written the following code to calculate the RPM of a DC motor using a quadrature rotary encoder and an Arduino Mega:</p>
  
  <pre><code>int N3 = 7; //N3 sur la L298N motor shield
  int N4 = 8; //N4 sur la L298N motor shield
  int ENB = 9; //ENB sur la L298N motor shield
  int potPin = A0; //analog pin 0 sur la carte arduino
  int valeurLu = 0; //valeur lu du potentiomètre
  int valeur_a_ecrire = 0; //valeur à envoyer au moteur
  int pin_A_encodeur = 3;
  int etat_courant_encodeur = 0;
  int etat_precedant_encodeur = 0;
  
  void setup() {
    attachInterrupt(digitalPinToInterrupt(3),updatePosition,CHANGE);
    pinMode(N3, OUTPUT);
    pinMode(N4, OUTPUT);
    pinMode(ENB, OUTPUT);
    pinMode(A0, INPUT);
    pinMode(pin_A_encodeur, INPUT);
    Serial.begin(9600);
  }
  
  void loop() {
    valeurLu = analogRead(potPin); 
    valeur_a_ecrire = (255.0/1023.0)*valeurLu;
    digitalWrite(N4, HIGH);
    digitalWrite(N3, LOW);
    analogWrite(ENB, valeur_a_ecrire);
    etat_courant_encodeur = digitalRead(pin_A_encodeur);
    Serial.print(valeur_a_ecrire);
    Serial.print(" ");
    Serial.println(etat_courant_encodeur);
  }
  </code></pre>
  
  <p>I'm able to read read information that the encoder sends but it's just a series of ones and zeros (11000111... etc). How can I use this information to calculate the RPM of the motor? My encoder has a resolution of 64 counts per revolution. Thanks in advance for any help on how to solve this problem.</p>