Contador de Pulsos en Arduino
motorPin). Una vez que el contador de pulsos llega a 15, el motor se detiene, se espera durante 5 segundos y luego se activa un servo que rota a 180° y regresa a 0°.Descripción del CódigoCpp
#include <Servo.h> // Definición de pines const int buttonPin = 2; // Primer botón: cuenta pulsos (INPUT_PULLUP) const int motorBtnPin = 6; // Segundo botón: activa motor (INPUT_PULLUP) const int greenLed = 3; // LED verde const int yellowLed = 4; // LED amarillo const int redLed = 5; // LED rojo const int motorPin = 8; // Motor 1 const int servoPin = 9; // Servo motor Servo myServo; // Instancia para controlar el servo // Variables para el primer botón (conteo de pulsos) con debounce int buttonState = HIGH; int lastButtonState = HIGH; unsigned long lastDebounceTime = 0; const unsigned long debounceDelay = 50; // Tiempo de debounce en ms int pulseCount = 0; // Variables para el segundo botón (motor) con debounce int motorBtnState = HIGH; int lastMotorBtnState = HIGH; unsigned long lastMotorDebounceTime = 0; const unsigned long motorDebounceDelay = 50; // Bandera que indica que el motor está activado bool motorActive = false; void setup() { // Configurar pines pinMode(buttonPin, INPUT_PULLUP); pinMode(motorBtnPin, INPUT_PULLUP); pinMode(greenLed, OUTPUT); pinMode(yellowLed, OUTPUT); pinMode(redLed, OUTPUT); pinMode(motorPin, OUTPUT); // Iniciar el servo y colocarlo en 0 grados myServo.attach(servoPin); myServo.write(0); // Apagar todos los LEDs y el motor inicial digitalWrite(greenLed, LOW); digitalWrite(yellowLed, LOW); digitalWrite(redLed, LOW); digitalWrite(motorPin, LOW); Serial.begin(9600); } void loop() { // Lectura del primer botón (conteo de pulsos) int reading = digitalRead(buttonPin); if (reading != lastButtonState) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; // Se detecta flanco de bajada (presión del botón) if (buttonState == LOW) { pulseCount++; Serial.print("Pulsos: "); Serial.println(pulseCount); // Apagar todos los LEDs antes de encender el correspondiente digitalWrite(greenLed, LOW); digitalWrite(yellowLed, LOW); digitalWrite(redLed, LOW); if (pulseCount >= 1 && pulseCount <= 5) { digitalWrite(greenLed, HIGH); } else if (pulseCount >= 6 && pulseCount <= 10) { digitalWrite(yellowLed, HIGH); } else if (pulseCount >= 11 && pulseCount <= 15) { digitalWrite(redLed, HIGH); } } } } lastButtonState = reading; // Lectura del segundo botón (activación del motor) int motorReading = digitalRead(motorBtnPin); if (motorReading != lastMotorBtnState) { lastMotorDebounceTime = millis(); } if ((millis() - lastMotorDebounceTime) > motorDebounceDelay) { if (motorReading != motorBtnState) { motorBtnState = motorReading; if (motorBtnState == LOW) { motorActive = true; Serial.println("Motor activado"); } } } lastMotorBtnState = motorReading; // Si el motor está activado y el recuento de pulsos es menor a 15, // se mantiene el motor encendido if (motorActive && pulseCount < 15) { digitalWrite(motorPin, HIGH); } // Cuando se llega a 15 pulsos, se detiene el motor y se realiza la acción del servo. if (motorActive && pulseCount >= 15) { digitalWrite(motorPin, LOW); Serial.println("Motor detenido. Esperando 5 segundos..."); delay(5000); Serial.println("Moviendo servo a 180 grados"); myServo.write(180); delay(1000); // Mantener la posición 180° durante 1 segundo Serial.println("Regresando servo a 0 grados"); myServo.write(0); // Reiniciar variables para la siguiente operación pulseCount = 0; motorActive = false; // Apagar LEDs tras el reinicio digitalWrite(greenLed, LOW); digitalWrite(yellowLed, LOW); digitalWrite(redLed, LOW); } }
INPUT_PULLUP para los botones, por lo que se deben conectar a GND al ser presionados.pulseCount.motorPin), siempre que el contador de pulsos sea menor a 15.pulseCount alcanza 15 y el motor está activo, se detiene el motor.Talk to Flux to get started.