// Definir miniservos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
// Pines de los miniservos
int servoPin1 = 3;
int servoPin2 = 5;
int servoPin3 = 6;
int servoPin4 = 11;
// Pines para los pulsadores y LEDs
int boton1Pin = 7; // Pin del botón de encendido
int boton2Pin = 8; // Pin del botón de apagado
int led1Pin = 12; // LED que indica encendido
int led2Pin = 13; // LED que indica apagado
// Pines del sensor ultrasonico
const int trigPin = 9;
const int echoPin = 10;
long duracion;
int distancia;
// Variables para almacenar el estado de los botones
int botonPin1State = 0;
int botonPin2State = 0;
bool sistemaEncendido = false; // Estado del sistema
void setup() {
// Inicializar pines de servos
servo1.attach(servoPin1);
servo2.attach(servoPin2);
servo3.attach(servoPin3);
servo4.attach(servoPin4);
// Inicializar pines de pulsadores, LEDs y sensor ultrasonico
pinMode(boton1Pin, INPUT_PULLUP);
pinMode(boton2Pin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Apagar LEDs al inicio
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
}
void loop() {
// Leer el estado de los botones
botonPin1State = digitalRead(boton1Pin);
botonPin2State = digitalRead(boton2Pin);
// Si el botón de inicio es presionado
if (botonPin1State == LOW && !sistemaEncendido) { // Cambiado a LOW debido a INPUT_PULLUP
digitalWrite(led1Pin, HIGH); // Encender LED de encendido
digitalWrite(led2Pin, LOW); // Apagar LED de apagado
sistemaEncendido = true; // Cambiar el estado del sistema
}
// Si el botón de paro es presionado
if (botonPin2State == LOW && sistemaEncendido) { // Cambiado a LOW debido a INPUT_PULLUP
digitalWrite(led1Pin, LOW); // Apagar LED de encendido
digitalWrite(led2Pin, HIGH); // Encender LED de apagado
apagarServos();
sistemaEncendido = false; // Cambiar el estado del sistema
}
// Control del sistema si está encendido
if (sistemaEncendido) {
// Medir la distancia usando el sensor ultrasónico
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Leer el tiempo de duración del eco
duracion = pulseIn(echoPin, HIGH);
// Calcular la distancia en centímetros
distancia = duracion * 0.034 / 2;
// Controlar servos según la distancia
if (distancia <= 10) {
servo1.write(50); // Mover servo 1
servo2.write(0);
servo3.write(0);
servo4.write(0);
} else if (distancia <= 20) {
servo1.write(0);
servo2.write(80); // Mover servo 2
servo3.write(0);
servo4.write(0);
} else if (distancia <= 30) {
servo1.write(0);
servo2.write(0);
servo3.write(100); // Mover servo 3
servo4.write(0);
} else if (distancia <= 40) {
servo1.write(0);
servo2.write(0);
servo3.write(0);
servo4.write(150); // Mover servo 4
} else {
apagarServos(); // Apagar todos los servos si la distancia es mayor a 40
}
}
}
// Función para apagar todos los servos
void apagarServos() {
servo1.write(90); // Usar un valor más neutro
servo2.write(90);
servo3.write(90);
servo4.write(90);
}
w4rr10r
copilot
Image
c498
c498
copilot
boton1Pin
andboton2Pin
.boton1Pin
is pressed and the system is not already "on" (sistemaEncendido
isfalse
):led1Pin
on andled2Pin
off.sistemaEncendido
totrue
.boton2Pin
is pressed and the system is "on":led1Pin
off andled2Pin
on.apagarServos()
to presumably turn off or reset something connected to servos (although the function itself is not shown).sistemaEncendido
tofalse
.boton1Pin
,boton2Pin
,led1Pin
,led2Pin
.botonPin1State
,botonPin2State
,sistemaEncendido
.apagarServos()
or any additional hardware interfaces. Let me know if you have specific questions or need further assistance with it!copilot
delay(50)
. Esto funciona, pero puede causar que el microcontrolador quede inactivo durante esos milisegundos y podría ser menos eficiente si hay otras tareas que ejecutar. Una alternativa podría ser implementar una lógica de debounce que no usedelay
, como un contador o un temporizador.apagarServos()
sería útil.apagarServos()
: Esta función se menciona pero no se ve en el código proporcionado. Asegúrate de que esté bien definida en tu código completo. Esto podría significar incluir acciones específicas para apagar los servos, posiblemente relacionados con los controladores de motores, como el TB6612FNG en tu proyecto.boton1Pin
,boton2Pin
,led1Pin
,led2Pin
, ysistemaEncendido
estén correctamente definidas en la configuración inicial del programa (setup()
), incluyendo la configuración de pines como entradas o salidas usandopinMode()
.c498
c498
copilot
Servo
library.trigPin
andechoPin
are used for measuring distance.apagarServos
is appropriate for your application. You might need to adjust the "neutral" position depending on the mechanical setup.copilot
pinMode
in thesetup
function:INPUT_PULLUP
, which means they are active low (they read LOW when pressed).trigPin
are configured asOUTPUT
.echoPin
is configured asINPUT
.digitalRead
. For debounce, it assumes proper mechanical button design with no additional code for bounce.pulseIn
to measure echo time and calculate distance.apagarServos()
.delay()
calls. Consider improving this with non-blocking methods if necessary.apagarServos
:c498
c498
copilot
Servo
library for controlling the servos.pinMode()
.LOW
due to pull-up configuration), it toggles the system state.copilot
delayMicroseconds
provides sufficient time for sensor stabilization. The current setup seems sufficient, but testing might prove whether adjustments are needed.duracion
returning 0).c498
copilot
millis()
function to ensure that button presses are accounted for only once over a specified debounce period.