Hey every one, I'm new at programming and have truble acctivating a gear motor with module speed. I'm also working with servo at the same time, the servo works well but I can't make the gear motor work with a L293D. What I want is to ask for speed and then make it to the gear motor.
#include <Servo.h>
// Motor con L293D
int MDC1 = 3;
int in1 = 4; // Asegúrate de conectar este pin adecuadamente al L293D
Servo SM;
void setup() {
pinMode(MDC1, OUTPUT); // Control del motor
pinMode(in1, OUTPUT); // Control del motor
digitalWrite(in1, LOW);
Serial.begin(9600);
SM.attach(9);
Serial.println("Dirección del viento: N, NO, O, SO, S");
Serial.println("Velocidad de viento: valor del 0 a 25 m/s");
}
void loop() {
analogWrite(MDC1, 0);
// Espera hasta recibir la dirección del viento
while (Serial.available() <= 0) {
delay(100); // Pequeña pausa para no saturar el procesador
}
String direccion = Serial.readStringUntil('\n');
direccion.trim();
// Definir la posición del servo conforme a las letras posibles
if (direccion == "N") {
SM.write(180);
} else if (direccion == "NO") {
SM.write(135);
} else if (direccion == "O") {
SM.write(90);
} else if (direccion == "SO") {
SM.write(45);
} else if (direccion == "S") {
SM.write(0);
}
Serial.println(direccion);
delay(4000); // Retraso opcional para dar tiempo a cambiar la dirección del viento
// Espera hasta recibir la velocidad del viento
while (Serial.available() <= 0) {
delay(100); // Pequeña pausa para no saturar el procesador
}
String strVelocidad = Serial.readStringUntil('\n');
strVelocidad.trim();
int velocidad = strVelocidad.toInt();
int pwmVal = map(velocidad, 0, 25, 0, 255); // Conversión a valor PWM
// Controla el motor basado en la velocidad
if (velocidad > 20) {
analogWrite(MDC1, 0);
digitalWrite(in1, LOW);
} else if (velocidad > 3) {
analogWrite(MDC1, pwmVal);
digitalWrite(in1, HIGH);
} else {
analogWrite(MDC1, 0);
digitalWrite(in1, LOW);
}
Serial.println(velocidad);
delay(1000); // Retraso antes de la siguiente lectura
}