problem with encoder

hi guys !
so I have a problem with my program. this program aims to print the number of ticks of my encoder while turning. we've written this code:

int codeurPinA = 2;
int codeurPinB = 3;
int codeurInterruptionA=0;
int codeurInterruptionB=1;
volatile unsigned int ticksCodeur = 0;

void setup() {
 
 pinMode(codeurPinA, INPUT); 
 pinMode(codeurPinB, INPUT);

 digitalWrite(codeurPinA, HIGH); //turn pullup resistor on
 digitalWrite(codeurPinB, HIGH);
 //Setup Channel A
 pinMode(12, OUTPUT); //Initiates Motor Channel A pin
 pinMode(9, OUTPUT); //Initiates Brake Channel A pin
 attachInterrupt(codeurInterruptionA, GestionInterruptionCodeurPinA,CHANGE);
 attachInterrupt(codeurInterruptionB, GestionInterruptionCodeurPinB,CHANGE);
 Serial.begin (9600);
}


void GestionInterruptionCodeurPinA()
{
 if (digitalRead(codeurPinA) == HIGH) {

               // check channel B to see which way encoder is turning
               if (digitalRead(codeurPinB) == LOW) {
                       ticksCodeur = ticksCodeur + 1;         
               }
               else {
                       ticksCodeur = ticksCodeur - 1;         
               }
       }

       else   // must be a high-to-low edge on channel A
       {
               // check channel B to see which way encoder is turning
               if (digitalRead(codeurPinB) == HIGH) {
                       ticksCodeur = ticksCodeur + 1;          
               }
               else {
                       ticksCodeur = ticksCodeur - 1;          
               }
       }
       Serial.println (ticksCodeur, DEC);
       Serial.println(ticksCodeur);
}
void GestionInterruptionCodeurPinB()
{
if (digitalRead(codeurPinB) == HIGH) {

               // check channel A to see which way encoder is turning
               if (digitalRead(codeurPinA) == HIGH) {
                       ticksCodeur = ticksCodeur + 1;         
               else {
                       ticksCodeur = ticksCodeur - 1;         
               }
       }

       // Look for a high-to-low on channel B

       else {
               // check channel B to see which way encoder is turning
               if (digitalRead(codeurPinA) == LOW) {
                       ticksCodeur = ticksCodeur + 1;         
               }
               else {
                       ticksCodeur = ticksCodeur - 1;         
               }
       }
       Serial.println(ticksCodeur);
 }

void loop() {
  
 
 
 digitalWrite(9, HIGH); 
   
}

in the monitor, it printed us only 1,2,0, or -1,-2
where is the problem ?

sorry for our bad english, we are french :wink:

thank you

hi !

can you please give us a hint on how everything is wired ?

have a nice day

Please read the "How to Use Forum" sticky to learn how to use code tags and edit your post so it's easier to read.

I'm pretty sure I've seen examples of using two encoders with the Arduino. I suggest finding some example code to start with. Hopefully someone else will provide a link.

Do you have any filtering on the encoder pins as shown on the bottom right of page 2 here?

hi a little picture to see how it is wired :

and the encoder:

Reply and Attach it please, I can't open that link.

done! (i've modified my previous post)

I get really tired of losing posts because it hadn't been 10 minutes since my last one. I wish the post would at least get saved so didn't have to type it again.

Here's the short version of what I wrote before.

Try commenting out one ISR for now. See if the counter increments.

These Pololu encoders are really nice. They should work great without any additional circuitry.

Hi,
How are you powering the motor?
250mA may be a bit to high for the arduino if you are using its 5V.

Tom.... :slight_smile: