So I have a project running, where I'm building a driving robot controlled with an app (from MIT App Inventor 2) via Bluetooth (I am using the HC-05).
The robot is driving forwards, backwards and sideways, but my 2 DC motors aren't the same speed. How can I fix this in the code? I was thinking about a new slider in the App controlling the difference of the speeds, but I am unsure how to put this in my code
I would appreciate it, if someone could help me.
int order;
// Motor A - RIGHT
int Dir_A1 = 4;
int Dir_A2 = 5;
int En_A = 6;
// Motor B - LEFT
int Dir_B1 = 7;
int Dir_B2 = 8;
int En_B = 9;
// How much gas is given
int mengeGas = 120;
// Whether gas is on
boolean gasStatus = false;
void setup() {
Serial.begin(9600);
pinMode(En_A, OUTPUT);
pinMode(Dir_A1, OUTPUT);
pinMode(Dir_A2, OUTPUT);
pinMode(En_B, OUTPUT);
pinMode(Dir_B1, OUTPUT);
pinMode(Dir_B2, OUTPUT);
// Vehicle is switched to "Forward" - but does not drive away
richtungVorwaerts();
}
// change of direction
void richtungRueckwaerts() {
// Motor 1 backwards
digitalWrite(Dir_A1, HIGH);
digitalWrite(Dir_A2, LOW);
// Motor 2 backwards
digitalWrite(Dir_B1, HIGH);
digitalWrite(Dir_B2, LOW);
}
void richtungVorwaerts() {
// Motor 1 forwards
digitalWrite(Dir_A1, LOW);
digitalWrite(Dir_A2, HIGH);
// Motor 2 forwards
digitalWrite(Dir_B1, LOW);
digitalWrite(Dir_B2, HIGH);
}
// Steering left
void lenkeLinks() {
// Right engine at full power - steers left
// The car keeps spinning until a new order arrives.
while (Serial.available() == false) {
analogWrite(En_A, 135);
analogWrite(En_B, 0);
}
}
// Steering Right
void lenkeRechts() {
/* Left motor at half power - steers right
Half the power, because the left motor is about twice as powerful
is as fast as the right one
and all at a similar speed
have to turn
The car keeps spinning until a new
Command is there.
*/
while (Serial.available() == false) {
analogWrite(En_B, 150);
analogWrite(En_A, 0);
}
}
//Car steps on the gas
void gas() {
if (mengeGas <= 15) {
analogWrite(En_A, 0);
analogWrite(En_B, 0);
}
else {
int mengeGasRechts = mengeGas + 50;
if (mengeGasRechts > 255) {
analogWrite(En_A, 255);
analogWrite(En_B, 250);
}
else {
analogWrite(En_A, mengeGasRechts);
analogWrite(En_B, mengeGas);
}
}
gasStatus = true;
}
// Car stops
void bremse() {
analogWrite(En_A, 0);
analogWrite(En_B, 0);
gasStatus = false;
}
// Reads the entries in the app
void loop() {
if (Serial.available() > 0) {
order = Serial.read();
}
if (order >= 10) {
mengeGas = order;
if (gasStatus == true) {
gas();
}
delay(200);
}
if (order == 2) {
lenkeRechts();
}
if (order == 3) {
lenkeLinks();
}
if (order == 4) {
gas();
}
if (order == 5) {
bremse();
}
if (order == 6) {
richtungVorwaerts();
}
if (order == 7) {
richtungRueckwaerts();
}
}