Problem With 500pulse Hanyoung Rotary Encoder

I have Project to Measure Length textille product in Meters and i have Arduino UNO + hanyoung Rotary Encoder 500 Pulse
I have Sketch that use serial communication to show counter in pulse and i put the wheel with rubber with 6.5cm diameter
so 16.5 x 3.14 = 20.41cm -> (20.41/100) * 500Pulse =2449 pulse
It take 2449 Pulse for measure 1 Meter
so i draw 1 m line to measure my rotary.e
the problem Is!!
i have 2 sketch 1 use UDP ethernet and 2 use simple serial communication COM2
for sketch 1 i succes to measure 1m line
but if I use sketch 2 with com2 i measure lower result and not 1 meter
its seems Serial com have a limit to read my encoder or to slow for my encoder
i googling for answer maybe Direct port or using Interrupt maybe its a solve
but i have no experience yet, i learn arduino for 1 month...

Please Help Me...
Code

#include <digitalWriteFast.h>


const int encoderPinA = 2;
const int encoderPinB = 3;
const int ledPin = 9;
const int encoderStepsPerRevolution=500;

float encoderPos = 0, count2[10];

boolean encoderALast = LOW;  // remembers the previous pin state


void setup()
{
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(encoderPinA, HIGH);
  digitalWrite(encoderPinB, HIGH);
  
  
  Serial.begin (115200);
}

void loop()
{
  boolean encoderA = digitalReadFast2(encoderPinA);
  

  if ((encoderALast == HIGH) && (encoderA == LOW))
  {
    if (digitalReadFast2(encoderPinB) == LOW)
    {
      encoderPos++;
    }
    else
    {
      encoderPos--;
       
    }
        

     Serial.print("Counter: ");
     Serial.print(encoderPos);
     Serial.print("      Meter Length: ");
     Serial.print(encoderPos /2449);
     Serial.println();  
     Serial.flush();
  }

  encoderALast = encoderA;
}

You will loose a lot of information with that divide in the print statement.

Also normally rotory encoders normally use an interrupt to gather the pulses.

so maybe attachinterrupt() will be solved?

cnmycrl:
so maybe attachinterrupt() will be solved?

cnmycrl:
so maybe attachinterrupt() will be solved?

Well yes, you have to write the rest of the code as well and do not forget to make the variables volatile.

Thanks & GBU
Grumpy_Mike

i have new Skecth and you right!! its seems my problem solved

#define encoder0Pin               2
#define encoder1Pin               3

volatile int countold;
float count;

void setup()
{
  count = 0;
  countold = 0; 
  
  pinMode(encoder0Pin, INPUT_PULLUP); 
  
  attachInterrupt(0, sensor1, RISING);
  Serial.begin(115200); // initialize serial communication:

}

void loop()
{
  if (countold != count)
  {
    Serial.print("Counter: ");
    Serial.print(count);
    Serial.print("  Meter Total: ");
    Serial.print(count/2449);
    Serial.println();
    Serial.flush();
    countold = count;
  }
  
}

void sensor1()
{
  count++;
}

but pin3 not work , i have no idea how to used for backward counter

but pin3 not work , i have no idea how to used for backward counter

You need to change the ISR sensor1 to be something like what you had when you were polling the pins. Your interrupt is triggered when pin 2 is RISING. If you look at the possible quadrature states of the encoder you will see that if pin3 is high when pin2 rises, you are going one direction, and if it is low when pin2 rises you are going in the other.

if(digitalRead(encoder1Pin))   //is pin 3 high when pin 2 triggered the interrupt?
  {
    count++;
  } 
  else 
  {
    count--;
  }

There are several other problems with your sketch

Count is the variable that changes within the ISR, and it is the one which needs to be a volatile float

At fast measuring speed you may run into a problem with

Serial.flush();

This line tells the Arduino to print out everything in the buffer before it will process another ISR. If you are measuring the cloth fast you may miss counts while the serial print is happening. You want the ISR sensor1 , which increments or decrements count, to operate in between characters of the print out. It will do that because the external interrupt is a higher priority than the interrupt which controls the serial print.

Why is count a float? There is no need for that. Floats are only an approximation.

The problem with count being a multi byte number is that it could change when you are accessing it giving you a false value. You need to either use a byte for count in the interrupt and accumulate the running total in the loop function, or you need to disable the interrupts while you fetch count in your loop function.

Thanks to grumpy_mike and cattledog
I learn from the best
i try your sktech for pin 3 for backward its works!!

the reason i put Serial.flush() in sketch is for serial comm/output not being slow..

sorry for troubling you Both -- GBU