Rotative Encoder, measurement of time and speed.

Hi everyone ,

I am using a script from the library encoder.h (cf below ) with arduino méga 2560, python 3.5 and HEDS-5500 A06 encoder. My first step was to extract the encoder position with arduino and display it with Pyton. This step is working. The second step of my project, is to extract the time at the position, and then use those to calcul speed of the movement. I have search and found function as delays, but that is not what I need. I also read about millis() and macros (), but I am not sure that corresponds to what I need. I exactly need the time at the position location extracted by my encoder/arduino. I tried also using time.time() in my python script , but the time did vary only every three position mesure.
Do you have an idea to get the time at the position? and then , the speed?
Thanks

Arduino code

#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>

// Change these pin numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
// interrup pin in Arduino Uno : digital pin 2 and 3
// interrup pin in Arduino Mega 2560 : digital pin 2, 3, 18, 19, 20, 21

Encoder knobLeft(2,3); 
Encoder knobRight(18,19);


void setup() {
  // put your setup code here, to run once:
  //Serial.println("TwoKnobs Encoder Test:");
  Serial.begin(9600);
}

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  long newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
  if (newLeft != positionLeft || newRight != positionRight) {
    //Serial.print("Left = ");
    Serial.print(newLeft);
    //Serial.print(", Right = ");
    Serial.print(',');
    Serial.println(newRight);
    //Serial.println();
    positionLeft = newLeft;
    positionRight = newRight;
  }
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knobLeft.write(0);
    knobRight.write(0);
  }
}

Python code is attach

pyqt_first.txt (6.74 KB)

Use millis(). Send its value as the first item in the string you send to python - followed by a comma of course.