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
thank you