Optical encoder with interrupt


hello everyone,

i'm trying read singals optical encoder mega 2560. optical encoder has 4 pins: gnd, 5v+, cha, chb. (http://fabacademy.org/archives/2014/students/moreira.guilherme/site_en/agilent_optical_encoders.pdf)

i connect wires of cha , chb 1 of interrupt pins of mega 2560.

i'm using round disk 1800 lines. code message every degree disk turned. working fine.

but work pins 18, 19, 20 , 21. other interrupt pins a2 , a3 aren't working.
they used work before other pins. is possible damage interrupt function?

all other funtions (that know) of these pins working:
high: checked voltmeter , gnd. i've measured 5v.
low: 0v
input , output working.


here code. maybe did wrong. selecting other pins, switch a2 , a3 18,19,20 or 21.

code: [select]

#define encoderpina a2
#define encoderpinb a3

volatile  int encoderpos = 0;
volatile  int angle = 0;
 int lastreportedpos = 1;

boolean a_set = false;
boolean b_set = false;


void setup() {

  pinmode(encoderpina, input);
  pinmode(encoderpinb, input);




// encoder pin on interrupt 0 (pin 2)
  attachinterrupt(digitalpintointerrupt(encoderpina), doencodera, change);
// encoder pin on interrupt 1 (pin 3)
  attachinterrupt(digitalpintointerrupt(encoderpinb), doencoderb, change);

  serial.begin(9600);
}


void loop(){
  if (lastreportedpos != angle) {
    serial.print("index:");
    serial.print(angle, dec);
    serial.println();
    lastreportedpos = angle;
  }}



// interrupt on changing state
void doencodera(){
  // test transition
  a_set = digitalread(encoderpina) == high;
  // , adjust counter + if leads b
  encoderpos += (a_set != b_set) ? +1 : -1;
if (0==encoderpos-20){
encoderpos=0;
angle++;}
if (0==encoderpos+20){
encoderpos=0;
angle--;}

}

// interrupt on b changing state
void doencoderb(){
  // test transition
  b_set = digitalread(encoderpinb) == high;
  // , adjust counter + if b follows a
  encoderpos += (a_set == b_set) ? +1 : -1;
if (0==encoderpos-20){
encoderpos=0;
angle++;}
if (0==encoderpos+20){
encoderpos=0;
angle--;}
}


maybe has idea. thank you.

quote
the other interrupt pins a2 , a3 aren't working.
those pins cannot used normal interrupts on mega

see attachinterrupt()


Arduino Forum > Using Arduino > Programming Questions > Optical encoder with interrupt


arduino

Comments