Pages: 1 [2] 3 4 ... 34   Go Down
Author Topic: encoder  (Read 16069 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The tutorial sketch in your link uses pins 3 & 4 for phases A and B but you are connecting to 2 and 4,

I think the Z phase is not needed in that code, and anyway it should not be connected to digital pin0, that is used for the serial port.

Try connecting the A sig to pin 3 and the B sig to pin 4. And disconnect the Z sig.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
mingki
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

fantastic!!!. it's working...!  thank you very much for your advise.
it's fantastic! now, i can get the point what interrupts does for.
this is the numbers i get from the serial.
3
2
1
0
65535
65534
65533
65532
when it is rotated in cw, it starts from 0 to 3 etc. when i rotate in ccw(opposite), it starts from starts from 65535 and decreases in values as you can see.
is this mean that if the encoder continues(in cw) to rotate until it reachs to 65535, the next number after 65535 is 0 again and continue increase 1, 2, 3, 4?
also, is it ok if those incoming numbers from encoder keep on piling up in serial? i'm worried about overloading the seiral.
« Last Edit: February 06, 2008, 02:47:49 am by mingki » Logged


Brisbane, Australia
Offline Offline
God Member
*****
Karma: 1
Posts: 593
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
also, is it ok if those incoming numbers from encoder keep on piling up in serial? i'm worried about overloading the seiral.
Dont worry. The serial can think faster than you can. ;D
It can go right up to over a tenth of a megabyte per second.

Worst case scenario is that some lines get dropped.
I've managed to do it only with a really stupid program and that was a computer sending tons of data to the Arduino. smiley
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
mingki
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks. i hope i could get some answer on below question.
Quote
this is the numbers i get from the serial.
3
2
1
0
65535
65534
65533
65532
when it is rotated in cw, it starts from 0 to 3 etc. when i rotate in ccw(opposite), it starts from starts from 65535 and decreases in values as you can see.
is this mean that if the encoder continues(in cw) to rotate until it reachs to 65535, the next number after 65535 is 0 again and continue increase 1, 2, 3, 4?
Logged


0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
mingki
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i just finish testing the numbers for myself whether the numbers are appearing in the order of 0, 1, 2, 3, 4, 5... after 65535 as the encoder is rotating.
i keep on rotating my encoder by using a motor and it turned out that the maximum number is approximately 48700, not 65535.
what is interesting for me is that the numbers are decreasing once it reaches to the maximum number even if i am rotating the encoder in the same direction.
Also, this time, the numbers are irregular.
48001 48000 48001 48000 47999  48000 47999 47998 47999 47998 47999............47992 47993 47992 47993 47992 47991 47992 47991 47992 47993 47992
could anyone please explain what's the reason?
Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try changing
  int encoder0Pos = 0;

To
  long encoder0Pos = 0;
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
mingki
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 06, 2008, 09:35:39 am by mingki » Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

« Last Edit: February 06, 2008, 09:07:43 am by mem » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
« Last Edit: February 06, 2008, 10:57:01 am by mem » Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
mingki
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks. the following is the sketch i used.
Code:
#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);          
                                              
 
}
« Last Edit: February 06, 2008, 05:49:55 pm by mingki » Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One problem is that the position counter is declared as unsigned long, it should just be long (because when the encoder turns ccw it is decremented so it will go negative when the position is less than 0).

But that is not the reason your count is not correct. I suspect the problem may be with the connection of the encoder. I am not familiar with it,  so my suggestion (other than double checking the datasheet and wiring) is to do a test of your sketch using switches instead of the encoder. If you have not run any of the tutorials covering the use of a switch, now is a good time to do this.  Disconnect the encoder and wire up two switches to pins 2 and 4. Then watch what happens to the serial output when the buttons change state.

I expect what should happen is that if the signal on pin 4 is low then transitions (high to low or low to high) on pin 2 will increase the count. If pin 4 is high then transitions of pin 2 will decrease the count.

You may find the count jumps a few counts because of  'switch bounce' (you can google that if you want) but it doesn't matter in this test if you can get the counters moving in the right direction.

This should help you find out if the problem is in the sketch, the wiring, or in the encoder.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
mingki
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your insight, i will test with switchs to find out the problem.
how can i do some phisical action using those serial inputs from encoder?
let's say, i use "if" commend to establish some conditional statement like
Code:
 void loop(){
val = analogRead(0);
"if (val < 20)
{
 digitalWrite(3, HIGH);
 delay(1000);
while arduino keep on reading and monitoring the serials from encoder(one arduino reading both analog input from potentiometer and pulse from encoder).
how can i combine encoder's serial with analog input to do something?
what i hope to achieve is that upon receiving analog input(variables), i want the serial number(which created just in time of analog input receiption) to be related to "if" conditions such as if(val < 20) or if(val < 30) etc. and make one of digital pins get HIGH after a certain designated amount of numbers'(encoder serial) been increased or decreased.
« Last Edit: February 07, 2008, 06:50:07 am by mingki » Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not really clear what you want to do,  but if you want a test for both a count level and an analog level then you could do something like:

Val =  analogRead(0);
if (val < 20)
{
  if(encoder0Pos > 40){  // testing if the encoder count is greater than 40
     digitalWrite(3, HIGH);
      delay(1000);
  }
}

But I think it would help to focus on getting the encoder sketch functioning before adding any more complexity  smiley-wink
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
mingki
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you very much for your help. you teach me a lot, men. i am very greatful!
yes, i will try my best to correct my sketch.
You've mentioned about testing whether encoder counter is greater than 40.
Quote
Val =  analogRead(0);
if (val < 20)
{
  if(encoder0Pos > 40){  // testing if the encoder count is greater than 40
     digitalWrite(3, HIGH);
      delay(1000);
  }
}
how can i change this testing statement into a sketch for "testing if the encoder counter is increased(or decreased) until 40"?
i hope to find out how to make the encoder counter to start to count till 40(ex : from 240 to 280) at the timing point of analog input receiption.
« Last Edit: February 07, 2008, 07:15:21 am by mingki » Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could do something like:

int startCount;

startCount = encoder0Pos;   // put this in setup();

// and this in your loop
if(encoder0Pos > startCount + 40){  
     StartCount = encoder0Pos;  // I assume you want to reset the counter
     digitalWrite(3, HIGH);
     delay(1000);  
}
Logged

Pages: 1 [2] 3 4 ... 34   Go Up
Jump to: