measureing the speed of the encoder with Encoder.h library

hi
I’m using Encoder.h library to read the position of the encoder with this code:

#include <Encoder.h>
Encoder myEncoder(2,3);
double Position;

void setup(){
Serial.begin(9600);
}
long oldPosition = -999;
void loop() {
long newPosition = myEncoder.read();
if (newPosition  != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}

so can I measure the speed of the encoder by adding some codes and change the code to this:

#include <Encoder.h>
Encoder myEncoder(2,3);
double Position;
unsigned long timeO = 0;
unsigned long timeF = 0;
volatile long pulses = 0;
float encoderspeed = 0;

void setup(){
Serial.begin(9600);
}
long oldPosition = -999;
 attachInterrupt(digitalPinToInterrupt(2), detectPulses, RISING);
  timeO = micros();
void loop() {
timeF = micros();
long newPosition = myEncoder.read();
if (newPosition  != oldPosition) {
oldPosition = newPosition;
 encoderspeed = ((float)pulses/1000.0)/((float)(timeF - timeO)/(60000000.0) );
Serial.println(newPosition);
Serial.println(encoderspeed);
}
pulses = 0;
timeO = micros();
}
void detectPulses() {
  if (digitalRead(3) == LOW) {
    pulses++;
  }
  else {
    pulses--;
  }
}

No. The encoder library by default uses interrupts and you have defined your encoder object using pins 2 and 3.
You then call attachInterrupt() on pin 2 which removes the interrupt for the encoder library so it won't work anymore (or work partially on pin 3 or ????)

Use a fixed time interval, then the speed is determined by the position difference during that interval. See BlinkWithoutDelay for the timing.

DrDiettrich:
Use a fixed time interval, then the speed is determined by the position difference during that interval. See BlinkWithoutDelay for the timing.

thanks for your help, I have looked at the https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
but I don't know how to write the interval time and how to subtract the oldPosition and newPosition when I have written ..... oldPosition = newPosition; ...... in void loop

You have to introduce some more variables…
This example averages over 1 second… adjust as needed

#include <Encoder.h>
Encoder myEncoder(2, 3);

const unsigned long timePeriod = 1000;
unsigned long startTime;
long startPosition;

void setup() {
  Serial.begin(9600);
  startPosition = myEncoder.read();
  startTime = millis();
}
long oldPosition = -999;
void loop() {
  unsigned long now = millis();
  long newPosition = myEncoder.read();

  if ( now - startTime >= timePeriod ) {
    // time to calculate average encoder speed
    float speed = (newPosition - startPosition) / (float)timePeriod;
    Serial.print( "Avg speed is ");
    Serial.println( speed, 4 );
    startTime = now;
    startPosition = newPosition;
  }
  if (newPosition  != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

blh64:
You have to introduce some more variables…
This example averages over 1 second… adjust as needed

#include <Encoder.h>

Encoder myEncoder(2, 3);

const unsigned long timePeriod = 1000;
unsigned long startTime;
long startPosition;

void setup() {
  Serial.begin(9600);
  startPosition = myEncoder.read();
  startTime = millis();
}
long oldPosition = -999;
void loop() {
  unsigned long now = millis();
  long newPosition = myEncoder.read();

if ( now - startTime >= timePeriod ) {
    // time to calculate average encoder speed
    float speed = (newPosition - startPosition) / (float)timePeriod;
    Serial.print( "Avg speed is ");
    Serial.println( speed, 4 );
    startTime = now;
    startPosition = newPosition;
  }
  if (newPosition  != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

thanks a lot your code is working , but I have a question about this code , it shows the position when the position is changed but it shows the average speed every 1 second how I could get both of them every 0.001 second?

You can get the position as often and as fast as you like, in the worst case you'll get the same value over and over again. Speed instead depends on time, and with a too short interval you may get alternating zero and full speed values, with no real meaning. Tachometers in all vehicles update so slow that a meaningful mean value is shown.

DrDiettrich:
You can get the position as often and as fast as you like, in the worst case you'll get the same value over and over again. Speed instead depends on time, and with a too short interval you may get alternating zero and full speed values, with no real meaning. Tachometers in all vehicles update so slow that a meaningful mean value is shown.

thanks for your answer, for the last question, if I have several encoders how could I define the speed variable for all of them I mean I could write the position of the encoder #1 as position1 and write the second one as position2 etc. but for speed I could'nt write speed1 or speed2!!

koronus:
thanks for your answer, for the last question, if I have several encoders how could I define the speed variable for all of them I mean I could write the position of the encoder #1 as position1 and write the second one as position2 etc. but for speed I could'nt write speed1 or speed2!!

I mean that the speed color is orange and it seems to be a function and I couldn't define it as a variable isn't it?
and I know that the speed is a keyword in the stepper library so could I define them as speed1, speed2, etc.