Hi!!
I have to run a DC geared motor with an encoder and get the feedback of the postion/ number of rotations.
I am using a L298N Dual H-Bridge to run the motor and found some pieces of codes on the net to get something out the encoder but the problem is that I get some false results, for example : after measuring the rotations I can easily see that the motor have turned one time and less than a half and the monitor shows 2250 pulses and for my case i have 810 pulses per main shaft revolution wich gives me 2.77 rotations...
Links :
Attached :
- schematic
- Arduino code i am using
Any help will be appreciated!
Thanks
Cheikhna

sketch_apr30a.ino (1.74 KB)
Your encoder outputs are to pins 2 and 3. Why do you read the state o pins 9 and 10 when they fire.
Please post your code inline with code tags like this. More peoiple will look at it.
const int interuptG = 0;
const int interuptD = 1;
const int canalBG = 9;
const int canalBD = 10;
int set1 = 6;
int set2 = 7;
int enable = 5;
void Reagir();
void ReagirBis();
long comptD = 0;
long comptG = 0;
//double tourGauche = comptG/1620.0;
//double tourDroite = comptD/1620.0;
void setup()
{
// Permet un affichage du résultat sur la consol Série
Serial.begin(9600);
pinMode(set1, OUTPUT);
pinMode(set2, OUTPUT);
pinMode(enable, OUTPUT);
pinMode(canalBG, INPUT);
pinMode(canalBD, INPUT);
// Déclaration des intéruptions
// Ici les fonctions d'intéruptions s'éxecutent
// sur fronts montants : RISING
attachInterrupt(interuptG, Reagir, RISING);
attachInterrupt(interuptD, ReagirBis, RISING);
}
void loop()
{
delay(4000);
avancer();
Serial.print("Tour Gauche");
Serial.print(comptG);
arreter();
reculer();
Serial.print(" Tour droite");
Serial.println(comptD);
arreter();
comptD=0;
comptG=0;
}
void Reagir()
{
if(digitalRead(canalBG) == HIGH)
{
comptG--;
}
else
{
comptG++;
}
}
void avancer()
{
digitalWrite(set1, LOW);
digitalWrite(set2, HIGH);
analogWrite(enable, 255);
delay(5000);
digitalWrite(set1, LOW);
digitalWrite(set2, LOW);
}
void reculer()
{
digitalWrite(set1, HIGH);
digitalWrite(set2, LOW);
analogWrite(enable, 255);
delay(5000);
digitalWrite(set1, LOW);
digitalWrite(set2, LOW);
}
void arreter()
{
digitalWrite(set1, LOW);
digitalWrite(set2, LOW);
delay(5000);
}
void ReagirBis()
{
if(digitalRead(canalBD) == HIGH)
{
comptD ++;
}
else
{
comptD--;
}
}
