Ok guys, first of all, this is my first post, so please be patient with me
I am also a newbie in programming...
What I want to do :
- read 2 optical encoders (built from a mouse)
- send the values as often as possible through serial port to the pc (50 times per second is fine).
The speed of the encoders is not extremely high, aprox. 500 - 800 pulses per sec. at maximum speed.
Currently, my program is just for one encoder.
The serial.print is called once every 50 ms using mills method, sends the value , and reset's the counter of the encoder. (so, sending just 20 times per second now)
The problem I encounter : - when the speed of the encoder is high, I get more pulses per rotation, as when the speed is lower. I thing this is due to the fact that on every serial.print, my arduino is missing some pulses. So in low speed mode, more serial.print's are called in one revolution of the encoder, and that's why I get less pulses counted.
The big question is :
Can I send a constant stream of data while counting encoder pulses without loosing any pulse ???
This is my code so far (it doesn't use interrupts, but I tried with the interrupts and the problem is still there).
#define ENC_A 54
#define ENC_B 55
#define ENC_PORT PINF
int counter = 0;
int tmpdata = 0;
long previousMillis = 0;
long interval = 50;Â
void setup() {
 pinMode(ENC_A, INPUT);
 pinMode(ENC_B, INPUT);
 Serial.begin(9600);
}
void loop() {
 tmpdata = read_encoder();
unsigned long currentMillis = millis();
 if (tmpdata) {
 counter = counter + tmpdata;
Â
Â
 if(currentMillis - previousMillis > interval) {
  previousMillis = currentMillis;Â
  Serial.println(counter);
  counter = 0;
 }
 }
}
int8_t read_encoder()
{
 int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
 static uint8_t old_AB = 0;
 old_AB <<= 2;        Â
 old_AB |= ( ENC_PORT & 0x03 );
 return ( enc_states[( old_AB & 0x0f )]);
}
As you can see, serial.print is called only when there is movement in the encoder.
If anyone can come with a suggestion, I will be grateful.
Thank you in advance.