Question with arduino code

Hello, good morning! I have created an RC car for testing, but I'm encountering a problem with a part of the code that I don’t understand.

I'm using an Arduino RP2040, but my mobile device doesn't support the Bluetooth and Wi-Fi of the Arduino RP2040, so I’m using the HC-06 Bluetooth module with the Arduino RP2040, I’m also using a small DC motor, which I'm connecting to the Mini L298N module.
To control the motor and the servo using a slider on my mobile via Bluetooth, I created an application in MIT App Inventor for do it. Here's the code, with the part that I don't understand very well:

int motorPin1 = 2; // Pin IN1 (clockwise) for the DC motor int motorPin2 = 3; // Pin IN2 (counterclockwise) for the DC motor

These two pins are impossible to connect because the Mini L298N module doesn't have an input for them, only IN1 and IN2 for one DC motor. I leave these two pins (2 and 3) unconnected, but if I remove the code, the slider doesn't work correctly.
If I remove the code, I no longer have speed control.(with the slider)
To clarify, when I remove the code for pins 2 and 3, the slider stops working as a slider and instead works as a button. Can someone explain this to me?

I'm sharing the complete code. I'm new to Arduino and have only a few hours of experience in programming the Arduino.

The code works correctly, but I don't understand why it doesn't work well when I remove pins 2 and 3, even though they are not connected to anything. My theory is that the Arduino is using pins 2 and 3 to calculate the slider, or something similar. It's strange because I have pins 9 and 11 connected to the Mini L298N module for this purpose. Thank you very much in advance for your help!

#include <Servo.h>
int motorPin1 = 2; // Pin IN1 (sentido horario) para el motor DC
int motorPin2 = 3; // Pin IN2 (sentido antihorario) para el motor DC
int enablePin = 9; // Pin PWM para la velocidad del motor DC (sentido horario)
int enablePin2 = 11; // Pin PWM para la velocidad del motor DC (sentido antihorario)
int servoPin = 6; // Pin para el control del servo motor
Servo myservo; // Objeto de la clase Servo para controlar el servo motor
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(enablePin2, OUTPUT);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 0);
analogWrite(enablePin2, 0);
myservo.attach(servoPin); // Asocia el pin del servo motor con el objeto myservo
Serial.begin(9600); // Inicia la comunicación serial con el monitor serie
Serial1.begin(9600); // Inicia la comunicación serial con el módulo Bluetooth HC-06 (pines Rx y Tx del Arduino RP2040)
}
void loop() {
// Espera comandos a través de la comunicación Bluetooth
// Recibe los comandos desde la aplicación móvil y actúa en consecuencia
if (Serial1.available()) {
char data = Serial1.read();
if (data == 'S') {
// Recibe el valor de velocidad del deslizador en la aplicación para el motor DC
int speed = Serial1.parseInt();
// Ajusta el valor de velocidad a un rango válido (0 - 255) para el motor DC
speed = constrain(speed, 0, 255);
// Controla la dirección del motor DC en sentido horario
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
// Establece la velocidad del motor DC utilizando el pin enablePin
analogWrite(enablePin, speed);
analogWrite(enablePin2, 0); // Detiene el motor DC en sentido antihorario
}
if (data == 'D') {
// Recibe el valor de velocidad del deslizador en la aplicación para el motor DC
int speed = Serial1.parseInt();
// Ajusta el valor de velocidad a un rango válido (0 - 255) para el motor DC
speed = constrain(speed, 0, 255);
// Controla la dirección del motor DC en sentido antihorario
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
// Establece la velocidad del motor DC utilizando el pin enablePin2
analogWrite(enablePin2, speed);
analogWrite(enablePin, 0); // Detiene el motor DC en sentido horario
}
if (data == 'P') {
// Recibe el valor del deslizador en la aplicación para el servo motor
int sliderValue = Serial1.parseInt();
// Ajusta el valor del deslizador a un rango válido (0 - 180) para el servo motor
int servoPosition = constrain(sliderValue, 0, 180);
// Controla el servo motor
myservo.write(servoPosition); // Mueve el servo motor a la posición correspondiente
}
}
}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

Hello, sorry my mistake, I don't know where to post :sweat_smile:
It is free to move this post :slight_smile:

Ok, but please edit your first post and make sure all the code is enclosed inside "CODE" tags, to make it more readable and copied for testing. Thanks.

1 Like

Hi, I will try thank you :slight_smile:

:wink:

1 Like

Can you please provide a link to the driver module you are using.

Can you also provide a hand drawn schematic of how all the components are connected.

L298DriverPic
L298Schematic-1
L298Schematic-2

1 Like

Captura de pantalla 2023-07-10 a las 20.54.24

Module.........(Arduino)
Mini L298N........Positive(VIN), Negative (GND), IN1 (pin9), IN2 (pin11), Motor-A (small DC motor )

So, you can see, I leave pin (2 and 3) unconnected, but if I remove the code, the slider doesn't work correctly....I think arduino needs it for logic...something strange for me, because I already have it connected to pin 9 and 11 using sliders...

I recently started working on a prototype car, and I'm quite pleased with its progress so far. My goal now is to explore the possibility of controlling it over the internet using WiFi. In this code, at the moment, everything is functioning correctly, and I have updated the code to include lights and LEDs. These components can be controlled using a slider, which provides a convenient way to adjust their behavior.

In my setup, I have integrated a servo motor, a DC motor, and a couple of LEDs. The slider functionality allows me to control the various aspects of the car's behavior. To facilitate this control, I created an application using MIT App Inventor.

While I initially tried using applications from the app stores, I encountered some issues and decided to develop my own solution. I must admit that the Arduino platform is not as straightforward as I initially thought, especially since I had no prior experience with the C programming language. As a way to engage with the community and learn more, I started participating in forums, even though I might be a bit late to the Arduino scene. It's fascinating to see that Arduino has been around since 2005, which means I'm joining the community at a slightly later stage.

Regarding the code, I have made the necessary updates to incorporate lights and LEDs. An interesting feature is that by connecting LEDs to pins 9 and 11, they light up alongside the motor slider. As the motor power increases, the brightness of these LEDs also increases. This behavior is possible because these pins support PWM (Pulse Width Modulation) output.

I hope this clarifies the updates and improvements

#include <Servo.h>
int motorPin1 = 2; // Pin IN1 (sentido horario) para el motor DC
int motorPin2 = 3; // Pin IN2 (sentido antihorario) para el motor DC
int enablePin = 9; // Pin PWM para la velocidad del motor DC (sentido horario)
int enablePin2 = 11; // Pin PWM para la velocidad del motor DC (sentido antihorario)
int servoPin = 6; // Pin para el control del servo motor
int ledPin1 = 5; // Pin para el primer LED analógico
int ledPin2 = 4; // Pin para el segundo LED analógico
int ledPin3 = 7; // Pin para el tercer LED analógico
int ledPin4 = 8; // Pin para el cuarto LED analógico
Servo myservo; // Objeto de la clase Servo para controlar el servo motor
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(enablePin2, OUTPUT);
pinMode(ledPin1, OUTPUT); // Configurar el primer pin del LED como salida
pinMode(ledPin2, OUTPUT); // Configurar el segundo pin del LED como salida
pinMode(ledPin3, OUTPUT); // Configurar el tercer pin del LED como salida
pinMode(ledPin4, OUTPUT); // Configurar el cuarto pin del LED como salida
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 0);
analogWrite(enablePin2, 0);
myservo.attach(servoPin); // Asocia el pin del servo motor con el objeto myservo
Serial.begin(9600); // Inicia la comunicación serial con el monitor serie
Serial1.begin(9600); // Inicia la comunicación serial con el módulo Bluetooth HC-06 (pines Rx y Tx del Arduino RP2040)
}
void loop() {
// Espera comandos a través de la comunicación Bluetooth
// Recibe los comandos desde la aplicación móvil y actúa en consecuencia
if (Serial1.available()) {
char data = Serial1.read();
if (data == 'S') {
// Recibe el valor de velocidad del deslizador en la aplicación para el motor DC
int speed = Serial1.parseInt();
// Ajusta el valor de velocidad a un rango válido (0 - 255) para el motor DC
speed = constrain(speed, 0, 255);
// Controla la dirección del motor DC en sentido horario
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
// Establece la velocidad del motor DC utilizando el pin enablePin
analogWrite(enablePin, speed);
analogWrite(enablePin2, 0); // Detiene el motor DC en sentido antihorario
}
if (data == 'D') {
// Recibe el valor de velocidad del deslizador en la aplicación para el motor DC
int speed = Serial1.parseInt();
// Ajusta el valor de velocidad a un rango válido (0 - 255) para el motor DC
speed = constrain(speed, 0, 255);
// Controla la dirección del motor DC en sentido antihorario
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
// Establece la velocidad del motor DC utilizando el pin enablePin2
analogWrite(enablePin2, speed);
analogWrite(enablePin, 0); // Detiene el motor DC en sentido horario
}
if (data == 'P') {
// Recibe el valor del deslizador en la aplicación para el servo motor
int sliderValue = Serial1.parseInt();
// Ajusta el valor del deslizador a un rango válido (0 - 180) para el servo motor
int servoPosition = constrain(sliderValue, 0, 180);
// Controla el servo motor
myservo.write(servoPosition); // Mueve el servo motor a la posición correspondiente
}
if (data == 'L') {
// Recibe el valor del deslizador en la aplicación para el brillo del primer LED
int brightnessValue1 = Serial1.parseInt();
// Ajusta el valor del deslizador a un rango válido (0 - 255) para el brillo del primer LED
int ledBrightness1 = constrain(brightnessValue1, 0, 255);
// Controla el brillo del primer LED analógico utilizando PWM
analogWrite(ledPin1, ledBrightness1);
}
if (data == 'M') {
// Recibe el valor del deslizador en la aplicación para el brillo del segundo LED
int brightnessValue2 = Serial1.parseInt();
// Ajusta el valor del deslizador a un rango válido (0 - 255) para el brillo del segundo LED
int ledBrightness2 = constrain(brightnessValue2, 0, 255);
// Controla el brillo del segundo LED analógico utilizando PWM
analogWrite(ledPin2, ledBrightness2);
}
if (data == 'N') {
// Recibe el valor del deslizador en la aplicación para el brillo del tercer LED
int brightnessValue3 = Serial1.parseInt();
// Ajusta el valor del deslizador a un rango válido (0 - 255) para el brillo del tercer LED
int ledBrightness3 = constrain(brightnessValue3, 0, 255);
// Controla el brillo del tercer LED analógico utilizando PWM
analogWrite(ledPin3, ledBrightness3);
}
if (data == 'O') {
// Recibe el valor del deslizador en la aplicación para el brillo del cuarto LED
int brightnessValue4 = Serial1.parseInt();
// Ajusta el valor del deslizador a un rango válido (0 - 255) para el brillo del cuarto LED
int ledBrightness4 = constrain(brightnessValue4, 0, 255);
// Controla el brillo del cuarto LED analógico utilizando PWM
analogWrite(ledPin4, ledBrightness4);
}
}
}

Hi, what's is TO? :sweat_smile:

:laughing: Thanks

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

1)press Ctrl-T for autoformatting your code
2)do a rightclick with the mouse and choose "copy for forum"
3)paste clipboard into write-window of a posting

Some questions.

Are you using a Nano RP2040 Connect?
Are you using the Arduino Mbed OS Nano Boards core?

Mini L298N........Positive(VIN), Negative (GND), IN1 (pin9), IN2 (pin11), Motor-A (small DC motor )

So, you can see, I leave pin (2 and 3) unconnected, but if I remove the code, the slider doesn't work correctly

What does "doesn't work correctly" mean specifically?

but my mobile device doesn't support the Bluetooth and Wi-Fi of the Arduino RP2040

Can you explain more about this?

Hello, thanks, I tried to use (press Ctrl-T for autoformatting your code) it doesn't do anything, I'm using MacOS Catalina and Arduino IDE 2.1.1. (unfortunately the Mac with arduino is a chestnut of incompatibility).

Correct, I am using the arduino Nano RP2040 Connect with the HC6 bluetooth module, ( Arduino Mbed OS Nano Boards core...yes I think...I not sure here ^^") I use the module bluetooth because the Arduino module requires using a mobile with bluetooth 5.0, something like that, my mobile is very old and does not have this low power bluetooth connection, Bluetooth BLE. This is why I use the module, I had bought other arduinos too, but the nano 33 BLE doesn't work either, the Nano RP2040 Connect is what works best for me. The bluetooth problem is that my mobile is not compatible, I think so, I have not tried a newer mobile yet. but if you have a mobile compatible with bluetooth BLE, no extra module is needed.

but the doubt was that if I eliminate the code int motorPin1 = 2; // Pin IN1
int motorPin2 = 3; // Pin IN2

The slider stops working correctly, it works but without control, it would be like pressing a button, I don't understand why it does this, since I have it connected directly to pin 9 and 11 (IN1 and IN2) on the Mini L298N module, meanwhile It is more a curiosity.

I am new to the world of electronics but I did not imagine that it would be so complicated, the good thing is that I have learned several things that I did not even imagine to do, how to use the soldering iron, (nothing better than YouTube to learn, videos like "1958 "ABOVE AND BEYOND" NASA SOLDER & SOLDERING TECHNIQUES ELECTRONICS INSTRUCTIONAL FILM XD48824"
and " Basic Soldering Lesson 1 - "Solder & Flux" from paceworldwide help me a lot,
I am very happy to learn these things :slight_smile: I never imagined that I would be able to weld something and even create a mobile application.

#include <Servo.h>
int motorPin1 = 2; // Pin IN1 (sentido horario) para el motor DC
int motorPin2 = 3; // Pin IN2 (sentido antihorario) para el motor DC
int enablePin = 9; // Pin PWM para la velocidad del motor DC (sentido horario)
int enablePin2 = 11; // Pin PWM para la velocidad del motor DC (sentido antihorario)
int servoPin = 6; // Pin para el control del servo motor
int ledPin1 = 5; // Pin para el primer LED analógico
int ledPin2 = 4; // Pin para el segundo LED analógico
int ledPin3 = 7; // Pin para el tercer LED analógico
int ledPin4 = 8; // Pin para el cuarto LED analógico
Servo myservo; // Objeto de la clase Servo para controlar el servo motor
void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(enablePin2, OUTPUT);
  pinMode(ledPin1, OUTPUT); // Configurar el primer pin del LED como salida
  pinMode(ledPin2, OUTPUT); // Configurar el segundo pin del LED como salida
  pinMode(ledPin3, OUTPUT); // Configurar el tercer pin del LED como salida
  pinMode(ledPin4, OUTPUT); // Configurar el cuarto pin del LED como salida
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  analogWrite(enablePin, 0);
  analogWrite(enablePin2, 0);
  myservo.attach(servoPin); // Asocia el pin del servo motor con el objeto myservo
  Serial.begin(9600); // Inicia la comunicación serial con el monitor serie
  Serial1.begin(9600); // Inicia la comunicación serial con el módulo Bluetooth HC-06 (pines Rx y Tx del Arduino RP2040)
}
void loop() {
  // Espera comandos a través de la comunicación Bluetooth
  // Recibe los comandos desde la aplicación móvil y actúa en consecuencia
  if (Serial1.available()) {
    char data = Serial1.read();
    if (data == 'S') {
      // Recibe el valor de velocidad del deslizador en la aplicación para el motor DC
      int speed = Serial1.parseInt();
      // Ajusta el valor de velocidad a un rango válido (0 - 255) para el motor DC
      speed = constrain(speed, 0, 255);
      // Controla la dirección del motor DC en sentido horario
      digitalWrite(motorPin1, HIGH);
      digitalWrite(motorPin2, LOW);
      // Establece la velocidad del motor DC utilizando el pin enablePin
      analogWrite(enablePin, speed);
      analogWrite(enablePin2, 0); // Detiene el motor DC en sentido antihorario
    }
    if (data == 'D') {
      // Recibe el valor de velocidad del deslizador en la aplicación para el motor DC
      int speed = Serial1.parseInt();
      // Ajusta el valor de velocidad a un rango válido (0 - 255) para el motor DC
      speed = constrain(speed, 0, 255);
      // Controla la dirección del motor DC en sentido antihorario
      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, HIGH);
      // Establece la velocidad del motor DC utilizando el pin enablePin2
      analogWrite(enablePin2, speed);
      analogWrite(enablePin, 0); // Detiene el motor DC en sentido horario
    }
    if (data == 'P') {
      // Recibe el valor del deslizador en la aplicación para el servo motor
      int sliderValue = Serial1.parseInt();
      // Ajusta el valor del deslizador a un rango válido (0 - 180) para el servo motor
      int servoPosition = constrain(sliderValue, 0, 180);
      // Controla el servo motor
      myservo.write(servoPosition); // Mueve el servo motor a la posición correspondiente
    }
    if (data == 'L') {
      // Recibe el valor del deslizador en la aplicación para el brillo del primer LED
      int brightnessValue1 = Serial1.parseInt();
      // Ajusta el valor del deslizador a un rango válido (0 - 255) para el brillo del primer LED
      int ledBrightness1 = constrain(brightnessValue1, 0, 255);
      // Controla el brillo del primer LED analógico utilizando PWM
      analogWrite(ledPin1, ledBrightness1);
    }
    if (data == 'M') {
      // Recibe el valor del deslizador en la aplicación para el brillo del segundo LED
      int brightnessValue2 = Serial1.parseInt();
      // Ajusta el valor del deslizador a un rango válido (0 - 255) para el brillo del segundo LED
      int ledBrightness2 = constrain(brightnessValue2, 0, 255);
      // Controla el brillo del segundo LED analógico utilizando PWM
      analogWrite(ledPin2, ledBrightness2);
    }
    if (data == 'N') {
      // Recibe el valor del deslizador en la aplicación para el brillo del tercer LED
      int brightnessValue3 = Serial1.parseInt();
      // Ajusta el valor del deslizador a un rango válido (0 - 255) para el brillo del tercer LED
      int ledBrightness3 = constrain(brightnessValue3, 0, 255);
      // Controla el brillo del tercer LED analógico utilizando PWM
      analogWrite(ledPin3, ledBrightness3);
    }
    if (data == 'O') {
      // Recibe el valor del deslizador en la aplicación para el brillo del cuarto LED
      int brightnessValue4 = Serial1.parseInt();
      // Ajusta el valor del deslizador a un rango válido (0 - 255) para el brillo del cuarto LED
      int ledBrightness4 = constrain(brightnessValue4, 0, 255);
      // Controla el brillo del cuarto LED analógico utilizando PWM
      analogWrite(ledPin4, ledBrightness4);
    }
  }
}

This is your code posted properly using the code tags. If you are going to use this forum, you will have to learn how to use this format.
There's a discussion here as well as in #7 here.

I understand you Bluetooth and BLE issues. The ESP32 is an advanced board with Bluetooth, BLE, and wifi. You might want to take a look at it.

The issue with pins 2 and 3 and the slider make no sense to me. I don't have an Nano RP2040 to investigate.

In other Arduinos, because of the timers used, there issue with Servos and PWM on some pins. I don't know how this applies to the Nano RP2040, but your anomaly about pins 9 and 10 not having pwm unless pins 2 and 3 are in the code deserves some investigation.

1 Like

Thank you :slight_smile: I will check it.

Hi, because I am using the pin 9 and 11, instead of 2 and 3, at the moment I have not tried to eliminate 9 and 11... in theory it should work correctly only with 9 and 11... keep in mind that I have nothing connected to these pins 2 and 3.

How are you powering the L298 - from the Arduino 5V output? The L298 isn't a great choice for a low voltage supply because it drops at least 2V internally. It is a very old bipolar design, there are much better modern drivers using MOSFETs available. Anyway you should not expect the Arduino to supply motor currents, the L298 +ve supply should come from a separate supply (for example a battery). Please post your schematic. Also it looks like that L298 module (the small one) doesn't have any on-board commutating diodes such as shown on the schematic in post #8 - these are needed to swallow voltage spikes when the motor turns off.

Hello, thanks for trying to help me. I will try to explain a bit. At the moment, I have no problems because I am using a small DC motor that is powered by a 1.2V AA battery. To power it, I'm using a 6V+ power supply, specifically 6 AA batteries in series.

I'm using the mini version of the module, not the regular version, just like you mentioned. I use the module because the Arduino is just the brain and it wouldn't be able to drive a motor directly.

I connect the VIN pin of the Arduino to the breadboard (positive), and I also connect the mini module to the breadboard on the same side (positive), then I connect the battery to the positive. I could try connecting the battery directly to the module, but for now I connect it to the breadboard and from there the Arduino and the module are powered.

I also make the other connections using the LEDs, each one with its Anode (positive) pin, and the negative leg connects to the negative of the breadboard.

For now, as I mentioned before, I use 6 AA batteries of 1.2V each in series, which does not reach 8V (considering the losses). It's within the recommended range to use the VIN pin of the Arduino I think.

The maximum would be about 12V, but after testing, there is plenty of power because it is a small motor, like a 9G micro servo (the servo is connected directly to the VIN pin of the Arduino to get the 5V).

I'm testing it and it works great, since the slider allows me to have full and precise control of the motor and servo. I have also tried with a precision motor of about 6V. For now everything is working fine with no power supply issues. Although not as powerful as a Kyosho Mini Z, I'm not looking for speed and power, but something smaller.

For example, I want to make a Turbo Racing or a Kyosho Dnano (which they no longer make), but which can be parked. The idea I have now is to turn a Hot Wheels into a remote control vehicle, but as I mentioned, I need precision (hence the slider) to be able to park it easily.

I've seen some videos on YouTube and the truth is that the battery I'm using is more than enough for this. For now, I have no problems, but I appreciate your input. If we are talking about a powerful and fast remote control vehicle, you would definitely have to use another module.

For now, I'm working on something small, a miniature remote control vehicle (RC Nano). However, I'm also thinking of making a boat and a submarine, where the 12V will be insufficient to power various motors, sensors, and servos.

Fortunately, I started with a small project, but the truth is that these projects are not as simple as they seem in YouTube videos. There is a lot of work that is not seen...

Now the idea is to make an RC with theses 2€ cars, 1:43 it has a lot of space but at 1:64 the truth is that it gets quite complicated.


Thanks I will try, I also have an ESP32 module, but I haven't powered it on yet. I have soldered the pins, even on the Arduino 33BLE, one of the first ones. I have so much experience that I have soldered the legs on the contrary (rsrsrsrs).

I understand that they all perform similar functions (the basics, of course). I believe the ESP also works with Bluetooth BLE. For this purpose, I use the Nano RP2040 Connect with Bluetooth HC6 module

it should work correctly only with 9 and 11... keep in mind that I have nothing connected to these pins 2 and 3.

Are you saying that this code, with the motor pin digital writes commented out, does not apply pwm signals to pins 9 and 11?

if (data == 'S') {
      // Recibe el valor de velocidad del deslizador en la aplicación para el motor DC
      int speed = Serial1.parseInt();
      // Ajusta el valor de velocidad a un rango válido (0 - 255) para el motor DC
      speed = constrain(speed, 0, 255);
      // Controla la dirección del motor DC en sentido horario
     // digitalWrite(motorPin1, HIGH);
      //digitalWrite(motorPin2, LOW);
      // Establece la velocidad del motor DC utilizando el pin enablePin
      analogWrite(enablePin, speed);
      analogWrite(enablePin2, 0); // Detiene el motor DC en sentido antihorario
    }
    if (data == 'D') {
      // Recibe el valor de velocidad del deslizador en la aplicación para el motor DC
      int speed = Serial1.parseInt();
      // Ajusta el valor de velocidad a un rango válido (0 - 255) para el motor DC
      speed = constrain(speed, 0, 255);
      // Controla la dirección del motor DC en sentido antihorario
     // digitalWrite(motorPin1, LOW);
     // digitalWrite(motorPin2, HIGH);
      // Establece la velocidad del motor DC utilizando el pin enablePin2
      analogWrite(enablePin2, speed);
      analogWrite(enablePin, 0); // Detiene el motor DC en sentido horario
    }

Can you run a simple test with led's connected to pins 9 and 11 and verify response to the slider without any code related to pins 2 and 3? Also, for testing can you add Serial output to your code and verify the values being read with Serial1.parseInt() ?