Created
November 21, 2025 17:44
-
-
Save yeffrimic/c3de84d0e73da018293fe68d390cb8c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <AFMotor.h> // Librería Adafruit Motor Shield | |
| #include <SoftwareSerial.h> // Librería Software Serial | |
| // Definir los motores conectados a los puertos M1 y M2 | |
| AF_DCMotor motor_izquierdo(1); // Motor en M1 | |
| AF_DCMotor motor_derecho(2); // Motor en M2 | |
| // Configurar Software Serial en pines A0 (RX) y A1 (TX) | |
| SoftwareSerial bluetooth(A0, A1); // RX, TX | |
| // Variables para el control | |
| char comando; // Almacena el comando recibido vía Bluetooth | |
| int velocidad = 200; // Velocidad inicial de los motores (0-255) | |
| void setup() { | |
| bluetooth.begin(9600); // Iniciar comunicación serial con Bluetooth | |
| Serial.begin(9600); // También iniciar Serial por hardware para debugging | |
| // Configurar velocidad de los motores | |
| motor_izquierdo.setSpeed(velocidad); | |
| motor_derecho.setSpeed(velocidad); | |
| // Inicialmente detener los motores | |
| motor_izquierdo.run(RELEASE); | |
| motor_derecho.run(RELEASE); | |
| bluetooth.println("Bluetooth conectado - Listo para recibir comandos"); | |
| Serial.println("Sistema iniciado - Esperando comandos Bluetooth..."); | |
| } | |
| void loop() { | |
| if (bluetooth.available() > 0) { | |
| comando = bluetooth.read(); // Leer el comando recibido por Bluetooth | |
| // Opcional: enviar eco al Serial por hardware para debugging | |
| Serial.print("Comando recibido: "); | |
| Serial.println(comando); | |
| // Ejecutar acción según el comando | |
| switch(comando) { | |
| case 'F': // Adelante | |
| motor_izquierdo.run(FORWARD); | |
| motor_derecho.run(FORWARD); | |
| bluetooth.println("Adelante"); | |
| break; | |
| case 'B': // Atrás | |
| motor_izquierdo.run(BACKWARD); | |
| motor_derecho.run(BACKWARD); | |
| bluetooth.println("Atras"); | |
| break; | |
| case 'L': // Izquierda | |
| motor_izquierdo.run(BACKWARD); | |
| motor_derecho.run(FORWARD); | |
| bluetooth.println("Izquierda"); | |
| break; | |
| case 'R': // Derecha | |
| motor_izquierdo.run(FORWARD); | |
| motor_derecho.run(BACKWARD); | |
| bluetooth.println("Derecha"); | |
| break; | |
| case 'S': // Detener | |
| motor_izquierdo.run(RELEASE); | |
| motor_derecho.run(RELEASE); | |
| bluetooth.println("Detenido"); | |
| break; | |
| case '1': // Velocidad baja | |
| velocidad = 100; | |
| actualizarVelocidad(); | |
| bluetooth.println("Velocidad: Baja"); | |
| break; | |
| case '2': // Velocidad media | |
| velocidad = 170; | |
| actualizarVelocidad(); | |
| bluetooth.println("Velocidad: Media"); | |
| break; | |
| case '3': // Velocidad alta | |
| velocidad = 255; | |
| actualizarVelocidad(); | |
| bluetooth.println("Velocidad: Alta"); | |
| break; | |
| default: | |
| bluetooth.println("Comando no reconocido"); | |
| break; | |
| } | |
| } | |
| } | |
| void actualizarVelocidad() { | |
| motor_izquierdo.setSpeed(velocidad); | |
| motor_derecho.setSpeed(velocidad); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment