350Views6Replies

Author Options:

arduino code stalls in mid program? Answered

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);
}}

6 Replies

user
steveastrouk (author)2013-05-11

IIRC Serial output it a blocking process on Arduino, so its screwed up by waiting for something to be transmitted.

Select as Best AnswerUndo Best Answer

user
jduffy54 (author)steveastrouk2013-05-11

I realized that, I just didn't know quite how long it took at 9600

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)jduffy542013-05-11

You CAN use interrupt driven serial and not get the problem.

Select as Best AnswerUndo Best Answer

user
jduffy54 (author)steveastrouk2013-05-11

Its not really a big deal. I just moved it to 115200 and that solved it.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)jduffy542013-05-11

Reckon 1mSec per character, including overhead.

Select as Best AnswerUndo Best Answer

user
jduffy54 (author)2013-05-10

Update: never mind, I just turned up the Serial speed, and that fixed it.

Select as Best AnswerUndo Best Answer