Go Down

Topic: problem with encoder (Read 819 times) previous topic - next topic

jujuoik9

Oct 27, 2015, 04:38 pm Last Edit: Oct 27, 2015, 07:10 pm by jujuoik9
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:

Code: [Select]
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 ;)

thank you

druid442

hi !

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

have a nice day

DuaneDegn

#2
Oct 27, 2015, 06:03 pm Last Edit: Oct 27, 2015, 06:06 pm by DuaneDegn
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.


CrossRoads

Do you have any filtering on the encoder pins as shown on the bottom right of page 2 here?
http://www.bourns.com/docs/Product-Datasheets/PEC12R.pdf
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

jujuoik9

#4
Oct 27, 2015, 06:54 pm Last Edit: Oct 27, 2015, 07:22 pm by jujuoik9
hi a little picture to see how it is wired :


and the encoder:
https://www.pololu.com/product/1447

CrossRoads

Reply and Attach it please, I can't open that link.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

jujuoik9

done! (i've modified my previous post)

DuaneDegn

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.

TomGeorge

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

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up