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
}
}
}