struggling with encoders

hello,

I'm struggling a little bit with encoders

I have 2 encoders attached to my arduino on pin 2, 3 and 10, 11

I use the code from carnevaledaniele on the arduino playground site : rotary encoders, because
his code seems to be the quickest, since I use encoders on motors.

http://playground.arduino.cc/Main/RotaryEncoders

my code :

#include "MonsterMoto.h"

MonsterMoto drive ;

int val ; 
int encoder1PinA = 2 ;
int encoder1PinB = 3 ;
volatile unsigned int encoder1Pos = 0 ;
volatile boolean encoder1PinBLast = LOW ;

int encoder2PinA = 10 ;
int encoder2PinB = 11 ;
volatile unsigned int encoder2Pos = 0 ;
volatile boolean encoder2PinBLast = LOW ;

void changeEncoder1A() ;
void changeEncoder1B() ;

void changeEncoder2A() ;
void changeEncoder2B() ;

void setup()
{
  Serial.begin(9600) ;

  pinMode(encoder1PinA, INPUT) ;
  digitalWrite(encoder1PinA, HIGH);       // turn on pull-up resistor

  pinMode(encoder1PinB, INPUT) ;
  digitalWrite(encoder1PinB, HIGH);       // turn on pull-up resistor
  
  pinMode(encoder2PinA, INPUT) ;
  digitalWrite(encoder2PinA, HIGH);       // turn on pull-up resistor
  
  pinMode(encoder2PinB, INPUT) ;
  digitalWrite(encoder2PinB, HIGH);       // turn on pull-up resistor
   
  drive.motorOff(0) ;
  drive.motorOff(1) ;

  attachInterrupt(digitalPinToInterrupt(encoder1PinA), changeEncoder1A, RISING);  
  attachInterrupt(digitalPinToInterrupt(encoder1PinB), changeEncoder1B, CHANGE);  
  
  attachInterrupt(digitalPinToInterrupt(encoder2PinA), changeEncoder2A, RISING);  
  attachInterrupt(digitalPinToInterrupt(encoder2PinB), changeEncoder2B, CHANGE);  
    
  drive.motorGo(0, CCW, 125) ;
  drive.motorGo(1, CCW, 125) ;
}

void loop()
{   
  Serial.print("Motor 1 : ") ;
  Serial.print(encoder1Pos) ;
  Serial.print("\n\n") ;
  Serial.print("Motor 2 : ") ;
  Serial.print(encoder2Pos) ;
  Serial.print("\n") ;
}

void changeEncoder1A()
{
  encoder1PinBLast ? encoder1Pos-- : encoder1Pos++ ;  
}

void changeEncoder1B()
{
  encoder1PinBLast = !encoder1PinBLast ;  
}

void changeEncoder2A()
{
  encoder2PinBLast ? encoder2Pos-- : encoder2Pos++ ;  
}

void changeEncoder2B()
{
  encoder2PinBLast = !encoder2PinBLast ;  
}

encoder 1 reads, but only counts backwards (turning left or right doesn't matter, counter goes down).
encoder 2 does nothing

I suspect it has to do with how I attach the interrupts

Can some1 point me in the right direction, please?

thanks in advance :smiley:

Which Arduino do you have? If UNO pins 10 & 11 are not external interrupt pins, maybe Encoder1 Channel A on pin 2, channel B on pin 10, encoder2 channel A on pin 3, channel B on pin 11.

You appear to be unaware of the pinMode (pin, INPUT_PULLUP) option, which is much clearer.

If you are on the Uno you may need to look at pin change interrupts, which are available on all pins
(of the Uno, not Mega), but which are a little harder to use.