Need help synchronizing 2 motors, hall sensor gives false reading
Basically I am making Sit/Stand table that has 2 motor driven legs (basic bosh DC motors), because load on table can be uneven motors need to be synchronized. It does have magnet ring on geer and I mounted 2 hall sensors (might be slightly off). 34 pin changes per rotation I count them using external interrupts and compare them between motors . It works well when motor is not loaded and NOT switched on/ off frequently . However when code starts to spot one of the motors(using replay to cut power) to compensate for load, the count of rotation gets bad. The number reported by arduino is same on 2 motors but legs are not at the same height (as bad as by 30-40 pulses ). Am I doing something wrong or sensor just gets messed up by motor start/stops.
I was thinking about accelerometer/gyro using filtering to get angle of the table and adjust it to gravity but still don know how it will react to motion of the table.
code sorry its a bit messy i am begginer and just try to make it kind of work and then polish it.
volatile int half_revolutions;
volatile int lf_revolutions;
volatile int vect;
int rpm;
int rpm2;
int timeold;
int timeold2;
int leveler;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, rpm_fun, FALLING);
attachInterrupt(1, rpm_2, FALLING);
//attachInterrupt(0, rpm_fun, CHANGE);
//attachInterrupt(1, rpm_2, CHANGE);
half_revolutions = 0;
lf_revolutions = 0;
rpm = 0;
rpm2 = 0;
timeold2 = 0;
timeold = 0;
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
while (digitalRead(9)==LOW)
{
vect=1;
m1_up();
m2_up();
levelup();
Serial.println(lf_revolutions,DEC);
Serial.println(half_revolutions,DEC);
delay(100);
}
m1_stop();
m2_stop();
while (digitalRead(10)==LOW)
{
vect=-1;
m1_dwn();
m2_dwn();
leveldwn();
Serial.println(lf_revolutions,DEC);
Serial.println(half_revolutions,DEC);
delay(100);
}
m1_stop();
m2_stop();
if (digitalRead(8)==LOW)
{
half_revolutions=0;
lf_revolutions=0;
Serial.println("reset");
}
if (half_revolutions%1==0)
{
if (timeold!=half_revolutions)
{
Serial.print("M1 Pulses:");
Serial.print(half_revolutions,DEC);
Serial.print("RPM:");
rpm=half_revolutions/34;
Serial.println(rpm,DEC);
}
}
timeold=half_revolutions;
if (lf_revolutions%1==0)
{
if (timeold2!=lf_revolutions)
{
Serial.print(" M2 Pulses: ");
Serial.print(lf_revolutions,DEC);
Serial.print("Rotations:");
rpm2=lf_revolutions/34;
Serial.println(rpm2,DEC);
}
}
timeold2=lf_revolutions;
}
void m1_stop()
{
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(50);
}
void m2_stop()
{
digitalWrite(6, LOW);
digitalWrite(7, LOW);
delay(50);
}
void m1_up()
{
digitalWrite(4, HIGH);
}
void m2_up()
{
digitalWrite(6, HIGH);
}
void m1_dwn()
{
digitalWrite(5, HIGH);
}
void m2_dwn()
{
digitalWrite(7, HIGH);
}
void leveldwn()
{
leveler=half_revolutions-lf_revolutions;
if (leveler<-5)
{
m2_stop();
}
if (leveler>5)
{
m1_stop();
}
if (digitalRead(9)==LOW)
{
leveldwn();
}
}
void levelup()
{
leveler=half_revolutions-lf_revolutions;
if (leveler<-5)
{
m1_stop();
}
if (leveler>5)
{
m2_stop();
}
if (digitalRead(10)==LOW)
{
levelup();
}
}
void rpm_fun()
{
half_revolutions=half_revolutions+vect;
//Each rotation, this interrupt function is run twice
}
void rpm_2()
{
lf_revolutions=lf_revolutions+vect;
//Each rotation, this interrupt function is run twice
}
//----------------------------------------