Motor controll with hall sensor

hello guys i am using a slew drive , its made of a regular 24V dc motor with a gear reduction and hall sensor the sensor has 2 pullup resistors on its outputs that give a 90deg phase difference
the pulses they give can be used to control the angle of the motor , the problem I'm having tho is even tho I count the pulses and figure out the angle , every time I do so a small error ocures due to not being able to figure out the last position , so what do you suggest i do to fix this little issue, i couldn't find any library or algorithm made for this purpose

void motorcontroll(float RELATIVEAngleToGoTo){

int pulse=0;

int varA = 0;
int varB = 0;
//if needed to turn clock wise
if(RELATIVEAngleToGoTo>0) { ////might need to give it a margin of error later
Turncw();
while((pulse/PULSETODEG)<abs(RELATIVEAngleToGoTo)){
if(digitalRead(MpulseA) > varA)
{
varA = 1;
pulse++;
Serial.print(pulse);
Serial.print(F(" pulse"));
// Put an "s" if the amount of pulses is greater than 1.
if(pulse > 1) {Serial.print(F("s"));}
Serial.println(F(" detectedA."));
}
if(digitalRead(MpulseA) == 0) {varA = 0;}
delay(2); // Delay for stability.
if(digitalRead(MpulseB) > varB)
{
varB = 1;
pulse++;
Serial.print(pulse);
Serial.print(F(" pulse"));

// Put an "s" if the amount of pulses is greater than 1.
if(pulse > 1) {Serial.print(F("s"));}

Serial.println(F(" detectedB."));
}

if(digitalRead(MpulseB) == 0) {varB = 0;}

delay(3); // Delay for stability.}
}
stopTurning();
}
////////////////////////////////////
//if needed to turn counter-clock wise
if(RELATIVEAngleToGoTo<0) { ////might need to give it a margin of error later
Turnccw();
while((pulse/PULSETODEG)<abs(RELATIVEAngleToGoTo)){
if(digitalRead(MpulseA) > varA)
{
varA = 1;
pulse++;
Serial.print(pulse);
Serial.print(F(" pulse"));
// Put an "s" if the amount of pulses is greater than 1.
if(pulse > 1) {Serial.print(F("s"));}
Serial.println(F(" detectedA."));
}
if(digitalRead(MpulseA) == 0) {varA = 0;}
delay(2); // Delay for stability.
if(digitalRead(MpulseB) > varB)
{
varB = 1;
pulse++;
Serial.print(pulse);
Serial.print(F(" pulse"));

// Put an "s" if the amount of pulses is greater than 1.
if(pulse > 1) {Serial.print(F("s"));}

Serial.println(F(" detectedB."));
}

if(digitalRead(MpulseB) == 0) {varB = 0;}

delay(3); // Delay for stability.}
}
stopTurning();
}
delay(10);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.