Arduino Bluetooth Car

I have an arduino mega with an adafruit motor shield in order to control 4 DC Double Shaft Gearbox Motor, 1 LCD I2C, 4 LEDs, and to power all of this I'm using 4 1.5V batteries (AAA size in Spain). When I connect the arduino mega to my computer and I use the 4 batteries to power the "car" it works perfectly, but when I already upload my code to the arduino plate, I disconnect it from my computer to try it in the ground, but it works like this video when it should work how this video but with the PC disconnected. I'm using App Inventor to controll the car by sending some letters as code to do something like turn on the LEDS. I tried by putting some more energy by adding 2 batteries of 1.5V but it's happening the same. Thanks for your response. This is the code I'm using:

`#include <AFMotor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Configuración de los motores
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

// Configuración de la pantalla LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Configuración de los LEDs
int LUZF1 = 43;
int LUZF2 = 41;
int LUZB1 = 33;
int LUZB2 = 31;
int ESTADO1 = LOW;
int ESTADO2 = LOW;
int ESTADO3 = LOW;
int ESTADO4 = LOW;

// Variable que almacena el mensaje recibido
String message = "";
char command;

void setup() {
  // Inicialización del puerto serie y Bluetooth
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Esperando comandos Bluetooth...");

  // Inicialización de la pantalla LCD
  lcd.init();
  lcd.backlight();
  lcd.print("Esperando...");

  // Configuración de los LEDs como salida
  pinMode(LUZF1, OUTPUT);
  pinMode(LUZF2, OUTPUT);
  pinMode(LUZB1, OUTPUT);
  pinMode(LUZB2, OUTPUT);
  digitalWrite(LUZF1, LOW);
  digitalWrite(LUZF2, LOW);
  digitalWrite(LUZB1, LOW);
  digitalWrite(LUZB2, LOW);
}

void loop() {
  // Si hay datos disponibles del módulo Bluetooth
  if (Serial1.available()) {
    delay(100);
    message = ""; // Reinicia el mensaje anterior
    while (Serial1.available()) {
      char c = Serial1.read();
      message += c; // Acumula los caracteres recibidos en el mensaje
    }

    Serial.print("Recibido: ");
    Serial.println(message);

    Stop(); // Inicializa con los motores detenidos

    command = message.charAt(0); // Lee el primer carácter del mensaje

    // Control de motores según el comando
    switch (command) {
      case 'f': // Adelante
        forward();
        break;
      case 'b': // Atrás
        back();
        break;
      case 'l': // Izquierda
        left();
        break;
      case 'r': // Derecha
        right();
        break;
      case 's': // Detener
        Stop();
        break;
      case 'w': // Encender/apagar LEDs
        toggleLeds();
        break;
      default:
        displayMessage(message); // Mostrar el mensaje en la pantalla LCD
        break;
    }
  }
}

// Función para mover los motores hacia adelante
void forward() {
  motor1.setSpeed(255);
  motor1.run(FORWARD);
  motor2.setSpeed(255);
  motor2.run(FORWARD);
  motor3.setSpeed(255);
  motor3.run(FORWARD);
  motor4.setSpeed(255);
  motor4.run(FORWARD);
}

// Función para mover los motores hacia atrás
void back() {
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(BACKWARD);
  motor3.setSpeed(255);
  motor3.run(BACKWARD);
  motor4.setSpeed(255);
  motor4.run(BACKWARD);
}

// Función para girar a la izquierda
void left() {
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(BACKWARD);
  motor3.setSpeed(255);
  motor3.run(FORWARD);
  motor4.setSpeed(255);
  motor4.run(FORWARD);
}

// Función para girar a la derecha
void right() {
  motor1.setSpeed(255);
  motor1.run(FORWARD);
  motor2.setSpeed(255);
  motor2.run(FORWARD);
  motor3.setSpeed(255);
  motor3.run(BACKWARD);
  motor4.setSpeed(255);
  motor4.run(BACKWARD);
}

// Función para detener los motores
void Stop() {
  motor1.setSpeed(0);
  motor1.run(RELEASE);
  motor2.setSpeed(0);
  motor2.run(RELEASE);
  motor3.setSpeed(0);
  motor3.run(RELEASE);
  motor4.setSpeed(0);
  motor4.run(RELEASE);
}

// Función para encender/apagar los LEDs
void toggleLeds() {
  ESTADO1 = digitalRead(LUZF1);
  ESTADO2 = digitalRead(LUZF2);
  ESTADO3 = digitalRead(LUZB1);
  ESTADO4 = digitalRead(LUZB2);
  digitalWrite(LUZF1, !ESTADO1);
  digitalWrite(LUZF2, !ESTADO2);
  digitalWrite(LUZB1, !ESTADO3);
  digitalWrite(LUZB2, !ESTADO4);
}

// Función para mostrar el mensaje en la pantalla LCD
void displayMessage(String msg) {
  if (msg.length() <= 7) {
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print(msg);
  } else {
    lcd.clear();
    lcd.print("Error");
  }
}`

How is the controller powered here?

with 4 batteries of 1.5V each

Connected to what controller pin?

they are connected to M+ and GND from the Adafruit motor shield

What is that? Pen and paper would likely make it clear.
Know that Vin needs 7.5 to 12 volt to work. Connecting to the 5 volt pin will damage the controller.


It's connected to this two pins, GND, and +M, it's said in all the videos I saw about this shield

What is connected to this position? What pins of that device?

Please draw a schematic of how everything is connected

I checked the videos, your car is probably drawing more current then what the batteries can provide, try using rechargeable batteries, 4 nimh aa sized or 2 18650 batteries.
If you go with the 18650 remember that these have a nominal voltage of 3.7 volts so 2 batteries wil give 7.4 volts.
Make sure your electronics can handle this voltage and use a voltage regulator if necessary

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