Go Down

Topic: encoder (Read 16 times) previous topic - next topic

mem

Try changing
 int encoder0Pos = 0;

To
 long encoder0Pos = 0;

mingki

#21
Feb 06, 2008, 02:55 pm Last Edit: Feb 06, 2008, 03:35 pm by mingki Reason: 1
hi, i changed int encoder0Pos = 0 to longencoder0Pos = 0 and i get following numbers.
21094
21095
21096
21097
21096
21097
21098
st1
2
1
2
1
it, ended after 21098.
and after 1, i continued rotating the encoder and the following is the numbers i get.
45053
45054
45055
45056
st1
art
2
3
4
5
6
2
1
it ended after 45056. i think st1 and art means start.
i hope i could know why i get such irregular numbering patterns.

mem

#22
Feb 06, 2008, 03:06 pm Last Edit: Feb 06, 2008, 03:07 pm by mem Reason: 1
did you change:
  int encoder0Pos = 0;
to
 int longencoder0Pos = 0;
or to
 long encoder0Pos = 0;  // this is what you want

the last one is correct. If that doesn't fix it could you post your sketch here.


mem

#23
Feb 06, 2008, 04:56 pm Last Edit: Feb 06, 2008, 04:57 pm by mem Reason: 1
Could you post the sketch here so I can scroll through it. you can past it into a post by pressing the pound icon '#' and pasting the sketch between the start and end code symbols:
i.e ...code]past your code here [/code...

mingki

#24
Feb 06, 2008, 11:49 pm Last Edit: Feb 06, 2008, 11:49 pm by mingki Reason: 1
thanks. the following is the sketch i used.
Code: [Select]

#define encoder0PinA  2
#define encoder0PinB  4

volatile unsigned long encoder0Pos = 0;

void setup() {


 pinMode(encoder0PinA, INPUT);
 digitalWrite(encoder0PinA, HIGH);      
 pinMode(encoder0PinB, INPUT);
 digitalWrite(encoder0PinB, HIGH);      

 attachInterrupt(0, doEncoder, CHANGE);  
 Serial.begin (9600);
 Serial.println("start");                

}

void loop(){

}


void doEncoder(){
 if (digitalRead(encoder0PinA) == HIGH) {  
   if (digitalRead(encoder0PinB) == LOW) {  
                                           
     encoder0Pos = encoder0Pos - 1;         // CCW
   }
   else {
     encoder0Pos = encoder0Pos + 1;         // CW
   }
 }
 else                                        
 {
   if (digitalRead(encoder0PinB) == LOW) {  
                                             // encoder is turning  
     encoder0Pos = encoder0Pos + 1;          // CW
   }
   else {
     encoder0Pos = encoder0Pos - 1;          // CCW
   }

 }
 Serial.println (encoder0Pos, DEC);          
                                             

}


Go Up