#include <Adafruit_SoftServo.h>
#define LED_BUTTON_PIN 0
#define SERVO_BUTTON_PIN 5
#define SERVO_PIN 3
#define LED1_PIN 1
#define LED2_PIN 2
#define LED3_PIN 4
#define DEBOUNCE_DELAY 200
#define BLINK_INTERVAL 300
#define LONG_PRESS_TIME 1000 // 1 segundo
#define START_BLINK_DURATION 1000 // 1 segundo de parpadeo simultáneo al iniciar
int led_pins[] = {LED1_PIN, LED2_PIN, LED3_PIN};
int led_times[] = {1000, 5000, 10000};
int current_led = 0;
int current_time = 0;
unsigned long last_short_press_time = 0;
Adafruit_SoftServo myServo;
int initial_position = 0;
bool button_was_pressed = false;
unsigned long button_press_start = 0;
bool counting_active = false;
unsigned long counting_start_time = 0;
bool servo_moved = false;
unsigned long servo_move_time = 0;
// Variables para aviso inicio parpadeo simultáneo
bool start_blink_active = false;
unsigned long start_blink_start_time = 0;
void setup() {
pinMode(LED_BUTTON_PIN, INPUT_PULLUP);
pinMode(SERVO_BUTTON_PIN, INPUT_PULLUP);
for (int i = 0; i < 3; i++) {
pinMode(led_pins[i], OUTPUT);
digitalWrite(led_pins[i], LOW);
}
digitalWrite(led_pins[current_led], HIGH);
current_time = led_times[current_led];
myServo.attach(SERVO_PIN);
myServo.write(initial_position);
}
void loop() {
unsigned long now = millis();
bool button_state = digitalRead(LED_BUTTON_PIN) == LOW;
// --- Detectar pulsación corta o larga ---
if (button_state) {
if (!button_was_pressed) {
button_press_start = now;
button_was_pressed = true;
}
} else {
if (button_was_pressed) {
unsigned long press_duration = now - button_press_start;
button_was_pressed = false;
if (press_duration < LONG_PRESS_TIME && (now - last_short_press_time) > DEBOUNCE_DELAY) {
// Pulsación corta: cambiar tiempo y LED
last_short_press_time = now;
digitalWrite(led_pins[current_led], LOW);
current_led = (current_led + 1) % 3;
digitalWrite(led_pins[current_led], HIGH);
current_time = led_times[current_led];
} else if (press_duration >= LONG_PRESS_TIME && !counting_active && !start_blink_active) {
// Pulsación larga: iniciar aviso de parpadeo simultáneo
start_blink_active = true;
start_blink_start_time = now;
}
}
}
// --- Aviso inicio: parpadeo simultáneo ---
if (start_blink_active) {
unsigned long elapsed = now - start_blink_start_time;
int blink_phase = (elapsed / BLINK_INTERVAL) % 2;
// Prendemos o apagamos todos los LEDs juntos
for (int i = 0; i < 3; i++) {
digitalWrite(led_pins[i], blink_phase ? HIGH : LOW);
}
if (elapsed >= START_BLINK_DURATION) {
start_blink_active = false;
counting_active = true;
counting_start_time = now;
servo_moved = false;
}
}
// --- Conteo con parpadeo secuencial ---
if (counting_active) {
unsigned long elapsed = now - counting_start_time;
int led_index = (elapsed / BLINK_INTERVAL) % 3;
for (int i = 0; i < 3; i++) {
digitalWrite(led_pins[i], (i == led_index) ? HIGH : LOW);
}
if (!servo_moved && elapsed >= current_time) {
myServo.write(190);
servo_move_time = now;
servo_moved = true;
}
if (servo_moved && (now - servo_move_time >= 1000)) {
myServo.write(initial_position);
counting_active = false;
servo_moved = false;
// Apagar LEDs y prender LED seleccionado
for (int i = 0; i < 3; i++) {
digitalWrite(led_pins[i], LOW);
}
digitalWrite(led_pins[current_led], HIGH);
}
}
myServo.refresh();
}
Una pulsación corta en el botón cambiará
el LED seleccionado (y su tiempo asociado).
Una pulsación larga en el mismo botón
iniciará el ciclo del servo, usando el
tiempo del LED que estaba seleccionado
en ese momento.