/*------------------------------------------------------- PRUEBA DE PULSADORES Y LEDS - ARDUINO NANO LED1 -> D13 LED2 -> D12 SW1 -> D4 SW2 -> D2 Descripción: - Si se presiona SW1: enciende LED1 - Si se presiona SW2: enciende LED2 - Si se presionan ambos: inicia rutina llamativa Autor: [Ja-Bots.com] Fecha: [2025] -------------------------------------------------------*/ // Definición de pines #define LED1 13 #define LED2 12 #define SW1 4 #define SW2 2 // Prototipos de funciones void leerPulsadores(); void rutinaLlamativa(); void encenderLed(int pin, bool estado); void setup() { // Configuración de pines pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(SW1, INPUT_PULLUP); // Pulsadores con resistencia pull-up interna pinMode(SW2, INPUT_PULLUP); } void loop() { leerPulsadores(); } /*------------------------------------------------------- Función: encenderLed Descripción: Controla el estado ON/OFF de un LED. Parámetros: - pin: número de pin del LED - estado: true (encender) / false (apagar) -------------------------------------------------------*/ void encenderLed(int pin, bool estado) { digitalWrite(pin, estado ? HIGH : LOW); } /*------------------------------------------------------- Función: leerPulsadores Descripción: Lee el estado de los pulsadores y ejecuta acciones: - SW1 presionado: LED1 encendido - SW2 presionado: LED2 encendido - Ambos presionados: rutina llamativa -------------------------------------------------------*/ void leerPulsadores() { bool estadoSW1 = !digitalRead(SW1); // Activo en LOW bool estadoSW2 = !digitalRead(SW2); if (estadoSW1 && estadoSW2) { rutinaLlamativa(); } else { encenderLed(LED1, estadoSW1); encenderLed(LED2, estadoSW2); } } /*------------------------------------------------------- Función: rutinaLlamativa Descripción: Efecto alternado rápido entre LED1 y LED2. -------------------------------------------------------*/ void rutinaLlamativa() { for (int i = 0; i < 6; i++) { digitalWrite(LED1, HIGH); digitalWrite(LED2, LOW); delay(100); digitalWrite(LED1, LOW); digitalWrite(LED2, HIGH); delay(100); } digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); }