I build a function to control 2 Dc motors from two momentary 2 way switches:
void Motor (int MotNo, char DIR, int Force)
{ if (MotNo == 1)
{ if (DIR == 'U')
{ digitalWrite ( M1_A1, 0);
digitalWrite ( M1_A2, 1);
}
if (DIR == 'D')
{ digitalWrite ( M1_A1, 1);
digitalWrite ( M1_A2, 0);
}
analogWrite ( M1_PA , Force);
}
if (MotNo == 2)
{ if (DIR == 'U')
{ digitalWrite ( M2_B1, 0);
digitalWrite ( M2_B2, 1);
}
if (DIR == 'D')
{ digitalWrite ( M2_B1, 1);
digitalWrite ( M2_B2, 0);
}
analogWrite ( M2_PB , Force);
}
}
Then in the loop I wrote the conditions:
(using "IF" did not work, motors start and stop with each refresh of the loop)
while (digitalRead(M1_UP_Pin) == LOW)
{ Motor(1, 'U', 144); //Motor 1, going UP, at 144 PWM
}
while (digitalRead(M1_DN_Pin) == LOW)
{ Motor(1, 'D', 244); //Motor 1, going DOWN, at 244 PWM
}
while (digitalRead(M2_UP_Pin) == LOW)
{ Motor(2, 'U', 244); //Motor 1, going UP, at 144 PWM
}
while (digitalRead(M2_DN_Pin) == LOW)
{ Motor(2, 'D', 144); //Motor 1, going DOWN, at 244 PWM
}
Everything works great unless I try to run the motors at the same time.
Whoever gets the signal first starts, while the other one does nothing.
What to do,
Thanks
Mitch