Arduino Nano 33 IOT - BLE - Servo motor problems

Hello,

I'm developing a bluetooth-controlled robot but I'm having trouble controlling a 180 servo motor. In addition to the servo motor, the robot is equipped with two dc motors that respond well to Bluetooth messages.
To test the control of the motors I'm using the BlueLight app.
The circuit consists of an Arduino Nano IOT 33 board, an H bridge (L298N) connected to two DC motors, Servo Motor 180. The system is powered by a 5.5 v battery that supplies energy to the motors and the board. In the case of the board, a voltage reducer from 5.5v to 3.3v was fitted (connected to the board's VIN). There is also an LED connected to pin 12 for controlling commands.
I'll present the code for you to analyse.


#include <ArduinoBLE.h>
#include <Servo.h>

// Definições de Pinos e Variáveis
#define LED_PIN 12 

// Motores
int ENA = 5;
int IN1 = 4;
int IN2 = 2;
int ENB = 3;
int IN3 = 7;
int IN4 = 8;

// Servo motor
Servo meuServo;

// UUIDs para o Serviço e Características BLE
const char* UUID_serv = "84582cd0-3df0-4e73-9496-29010d7445dd";
const char* UUID_ledControl = "84582cd9-3df0-4e73-9496-29010d7445dd";
const char* UUID_Av = "84582cd10-3df0-4e73-9496-29010d7445dd";
const char* UUID_Rc = "84582cd11-3df0-4e73-9496-29010d7445dd";
const char* UUID_Dt = "84582cd12-3df0-4e73-9496-29010d7445dd";
const char* UUID_Es = "84582cd13-3df0-4e73-9496-29010d7445dd";
const char* UUID_Servo = "84582cd14-3df0-4e73-9496-29010d7445dd";
const char* UUID_Stop = "84582cd15-3df0-4e73-9496-29010d7445dd"; // UUID para parar os motores

// Serviço BLE e Características
BLEService myService(UUID_serv); 
BLEByteCharacteristic chLEDControl(UUID_ledControl, BLERead | BLEWrite);
BLEByteCharacteristic chAv(UUID_Av, BLERead | BLEWrite);
BLEByteCharacteristic chRc(UUID_Rc, BLERead | BLEWrite);
BLEByteCharacteristic chDt(UUID_Dt, BLERead | BLEWrite);
BLEByteCharacteristic chEs(UUID_Es, BLERead | BLEWrite);
BLEByteCharacteristic chServo(UUID_Servo, BLERead | BLEWrite);
BLEByteCharacteristic chStop(UUID_Stop, BLERead | BLEWrite); // Característica para parar os motores

void setup() {
  meuServo.attach(6);
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);

  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  if (!BLE.begin()) {
    Serial.println("Falha ao iniciar BLE!");
    while (1);
  }

  BLE.setLocalName("NANO33IOT_NOVO");
  BLE.setAdvertisedService(myService);

  myService.addCharacteristic(chLEDControl);
  myService.addCharacteristic(chAv);
  myService.addCharacteristic(chRc);
  myService.addCharacteristic(chDt);
  myService.addCharacteristic(chEs);
  myService.addCharacteristic(chServo);
  myService.addCharacteristic(chStop);

  BLE.addService(myService);
  BLE.advertise();
  Serial.println("BLE pronto e anunciando");
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Conectado a: ");
    Serial.println(central.address());

    while (central.connected()) {
      updateLED();
      checkCommands();
    }

    Serial.print("Desconectado de: ");
    Serial.println(central.address());
  }
}

void updateLED() {
  if (chLEDControl.written()) {
    byte ledValue = chLEDControl.value();
    digitalWrite(LED_PIN, ledValue == 1 ? HIGH : LOW);
  }
}

void checkCommands() {
  if (chAv.written()) {
    byte command = chAv.value();
    if (command == 2) Avanca();
  }
  if (chRc.written()) {
    byte command = chRc.value();
    if (command == 3) Recua();
  }
  if (chDt.written()) {
    byte command = chDt.value();
    if (command == 4) Esquerda();
    else if (command == 5) Direita();
  }
  if (chEs.written()) {
    byte command = chEs.value();
    if (command == 6) Stop();
  }
  if (chStop.written()) {
    byte command = chStop.value();
    if (command == 7) Stop(); // Adicionando comando para parar os motores
  }

  if (chServo.written()) {
    byte command = chServo.value();
    Serial.print("Comando Servo recebido: ");
    Serial.println(command);
    if (command == 10) {
      meuServo.write(90);
      Serial.println("Movendo Servo: 0 -> 90");
    } else if (command == 11) {
      meuServo.write(0);
      Serial.println("Movendo Servo: 90 -> 0");
    }
  }
}

// Funções de movimento...
void Avanca() {
  Serial.println("Avança");
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 255);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENB, 255);
}


void Recua() {
  Serial.println("Recua");
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 255);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENB, 255);
}
void Stop() {
  Serial.println("Parar");
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 0);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  analogWrite(ENB, 0);
}

void Esquerda() {
  Serial.println("Esquerda");
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW); 
  analogWrite(ENA, 255);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  analogWrite(ENB, 0);
}

void Direita() {
  Serial.println("Direita");
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW); 
  analogWrite(ENA, 255);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENB, 0);
}
digite ou cole aqui o código

Thank you in advance for your help. My compliments to the members of the Forum

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