Using stepper motor as encoder

Stepper motor example

      
        #include <Encoder.h>
#define dir 12   // Stepping
#define stp 13   // Direction
Encoder myEnc(3, 2);

void setup() {
  pinMode(dir, OUTPUT);
  pinMode(stp, OUTPUT);

}

int newPosition=0;
int oldPosition = 0;
int diff=0;
void loop() {
  oldPosition=newPosition;
  newPosition = myEnc.read();
  diff=newPosition-oldPosition;
  if(diff!=0){
    if(diff> 0){
      digitalWrite(dir, HIGH);
    }else{
      digitalWrite(dir, LOW);
    }
    diff=abs(diff);
    for(int i=0;i< diff;i++){
      digitalWrite(stp,HIGH);
      delay(50);
      digitalWrite(stp,LOW);
    }
  }
}