arduino code stalls in mid program?

I'm using a rotary encoder, and to test it, I wrote a program with an interrupt on pin two that counts (with a state machine for the direction and two parts per cycle), but whenever I spin the encoder too fast, the program simple stops sending values.  Its running on an uno with a FTDI friend (becaust the serial chip on the board itself is fried), with a 32 cycle per revolution encoder on pins two and seven.  The encoder is standard quadrature, with an optical pickup and slotted disk. 
It works fine normally, the problem only occours when it's spun too fast. 
I will need it to spin quickly for my application.  
I tried closing and reopening serial communications, and it didn't help, I have to reset it to make it work again.

here's the code:

#define ENCA 2
#define ENCB 7
int r = 0;
float pos = 0;

void setup(){
  Serial.begin(9600);
   pinMode(ENCA, INPUT);
   pinMode(ENCB, INPUT);
   digitalWrite(ENCA, HIGH);
   digitalWrite(ENCB, HIGH);
 
   attachInterrupt(0, epos, CHANGE);

}

void loop(){
delay(1);
}


void epos(void){
  r = 1;
  if(digitalRead(2) == HIGH){
   if(digitalRead(7) == LOW){
     while(r == 1){
       delay(0.1);
       if(digitalRead(2) == LOW){
         r = 0;
       } else {
         if(digitalRead(7) == HIGH){
           pos = pos + 0.015625;
           r = 0;
         }}}
          
          
          
   }
   else {
     while(r == 1){
       delay(0.1);
       if(digitalRead(2) == LOW){
         r = 0;
       } else {
         if(digitalRead(7) == LOW){
           pos = pos - 0.015625;
           r = 0;
         }}}
   }
   Serial.println(pos);
}else{
  r = 1;
   if(digitalRead(7) == HIGH){
     while(r == 1){
       delay(0.1);
       if(digitalRead(2) == HIGH){
         r = 0;
       } else {
         if(digitalRead(7) == LOW){
           pos = pos + 0.015625;
           r = 0;
         }}}
          
          
          
   }
   else {
     while(r == 1){
       delay(0.1);
       if(digitalRead(2) == HIGH){
         r = 0;
       } else {
         if(digitalRead(7) == HIGH){
           pos = pos - 0.015625;
           r = 0;
         }}}
   }
   Serial.println(pos);
}}

sort by: active | newest | oldest
IIRC Serial output it a blocking process on Arduino, so its screwed up by waiting for something to be transmitted.
jduffy54 (author)  steveastrouk4 years ago
I realized that, I just didn't know quite how long it took at 9600
You CAN use interrupt driven serial and not get the problem.
jduffy54 (author)  steveastrouk4 years ago
Its not really a big deal. I just moved it to 115200 and that solved it.
Reckon 1mSec per character, including overhead.
jduffy54 (author) 4 years ago
Update: never mind, I just turned up the Serial speed, and that fixed it.