I use an Arduino Mega, with a LN298 motor controller
I have 4 engines.
My problem is that an engine does not work when it should go forward, but, curiously, it does work when it should go backward. That is, it is not a connection problem, change the LN298 board and the problem continues, change the arduino pin and the problem continues, I think it is because of the code, I don't know, but it is something very basic, high and low.
My code is the following:
#include <Wire.h>
// Motor A
int ENA = 13;
int IN1 = 12;
int IN2 = 11;
// Motor B
int IN3 = 10;
int IN4 = 9;
int ENB = 8;
// Motor A1
int ENA1 = 7;
int IN11 = 6;
int IN21 = 5;
// Motor B1
int IN31 = 4;
int IN41 = 3;
int ENB1 = 2;
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(115200);
Serial.setTimeout(0);
// Indicaciones para el control
Serial.println("Velocidad 255 max. - 128 medio - 0 min.");
Serial.println("Adelante - 1");
Serial.println("Atrás - 2");
Serial.println("Derecha adelante - 3");
Serial.println("Izquierda adelante - 4");
Serial.println("Derecha atrás - 5");
Serial.println("Izquierda atrás - 6");
Serial.println("Parar - 7");
Serial.println("Servo 1 - 8");
Serial.println("Servo 2 - 9");
Serial.println("Voltage - a");
Serial.println("Gyro, Accel - b");
// Declaramos todos los pines para los motores 0
pinMode (ENA, OUTPUT);
pinMode (ENB, OUTPUT);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (IN3, OUTPUT);
pinMode (IN4, OUTPUT);
// Declaramos todos los pines para los motores 1
pinMode (ENA1, OUTPUT);
pinMode (ENB1, OUTPUT);
pinMode (IN11, OUTPUT);
pinMode (IN21, OUTPUT);
pinMode (IN31, OUTPUT);
pinMode (IN41, OUTPUT);
}
void loop() {
recvWithEndMarker();
arduinoControl();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0';
ndx = 0;
newData = true;
}
}
}
void arduinoControl() {
if (newData == true) {
// Adelante
if (receivedChars[0] == '1') {
Serial.println("Adelante");
Serial.println(atoi(&receivedChars[2]));
//Direccion motor A
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
analogWrite (ENA, atoi(&receivedChars[2])); //Velocidad motor A
//Direccion motor B
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENB, atoi(&receivedChars[2])); //Velocidad motor B
//Direccion motor A 1
digitalWrite (IN11, LOW);
digitalWrite (IN21, HIGH);
analogWrite (ENA1, atoi(&receivedChars[2])); //Velocidad motor A 1
//Direccion motor B 1
digitalWrite (IN31, LOW);
digitalWrite (IN41, HIGH);
analogWrite (ENB1, atoi(&receivedChars[2])); //Velocidad motor B 1
}
// Atrás
else if (receivedChars[0] == '2') {
Serial.println("Atrás");
Serial.println(atoi(&receivedChars[2]));
//Direccion motor A
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
analogWrite (ENA, atoi(&receivedChars[2])); //Velocidad motor A
//Direccion motor B
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
analogWrite (ENB, atoi(&receivedChars[2])); //Velocidad motor B
//Direccion motor A 1
digitalWrite (IN11, HIGH);
digitalWrite (IN21, LOW);
analogWrite (ENA1, atoi(&receivedChars[2])); //Velocidad motor A 1
//Direccion motor B 1
digitalWrite (IN31, HIGH);
digitalWrite (IN41, LOW);
analogWrite (ENB1, atoi(&receivedChars[2])); //Velocidad motor B 1
}
// Parar
else if (receivedChars[0] == '7') {
Serial.println("Parar");
Serial.println(atoi(&receivedChars[2]));
//Direccion motor A
digitalWrite (IN1, LOW);
digitalWrite (IN2, LOW);
analogWrite (ENA, 0); //Velocidad motor A
//Direccion motor B
digitalWrite (IN3, LOW);
digitalWrite (IN4, LOW);
analogWrite (ENB, 0); //Velocidad motor B
//Direccion motor A1
digitalWrite (IN11, LOW);
digitalWrite (IN21, LOW);
analogWrite (ENA1, 0); //Velocidad motor A1
//Direccion motor B1
digitalWrite (IN31, LOW);
digitalWrite (IN41, LOW);
analogWrite (ENB1, 0); //Velocidad motor B1
}
newData = false;
}
}
The problem is in:
//Direccion motor B
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENB, atoi(&receivedChars[2])); //Velocidad motor B