Amigos, tengo un codigo en el cual estoy usando un switch, en cada uno se definen varios estados de 3 servos y un relé ademas que para pasar de case a case utilizo botones diferentes todo corre bien hasta que que le agrego delay para que los servos y el relé cumplan una secuencia,es decir luego de implementar los delay ya no se realiza el cambio entre estados en el switch.
//unsigned long time;
//unsigned long t ;
//int Dt = 100;
#include <Servo.h>
// DECLARACION DE LOS SERVOS
Servo myservo_S1;
Servo myservo_S2;
Servo myservo_S3;
// PIN DE SALIDA DEL RELÉ (3)
int RELE = 3;
//ESTABLECE EL GIRO DE LOS SERVOS
int Giro_On = 0; //Angulo de giro del servo Cerrado
int Giro_Off = 90; //Angulo de giro del servo Abierto
//define tiempo
int Time = 50;
// Arreglo de botones y último estado del botón
// Nota: Los siguientes "DEFINE" son únicamente para
// mejorar la lectura del código al momento de codificar.
#define BTN_FS 0 //BTN_MENU 0
#define BTN_NORMAL 1 //BTN_EXIT 1
#define BTN_MTTO 2 //BTN_UO 2
#define BTN_RESET 3 //BTN_DOWN 3
// Este arreglo contiene los pines utilizados para los botones
uint8_t button[4] = {
12,
11,
10,
9,
};
uint8_t button_state[4];
// Estado de nuestro autómata
#define S_FS 0
#define S_NORMAL 1
#define S_MTTO 2
#define S_RESET 3
uint8_t estado = S_FS;
// Facilita la detección de flancos de subidan en los pines
// monitoreados. Asume la existencia de un arreglo button
// con la asignación actual de pines y un arreglo button_estate
// con los valores de línea
uint8_t flancoSubida(int btn) {
uint8_t valor_nuevo = digitalRead(button[btn]);
uint8_t result = button_state[btn] != valor_nuevo && valor_nuevo == 1;
button_state[btn] = valor_nuevo;
return result;
}
void setup() {
// Configurar como PULL-UP para ahorrar resistencias
pinMode(button[BTN_FS], INPUT_PULLUP);
pinMode(button[BTN_NORMAL], INPUT_PULLUP);
pinMode(button[BTN_MTTO], INPUT_PULLUP);
pinMode(button[BTN_RESET], INPUT_PULLUP);
// Se asume que el estado inicial es HIGH
button_state[0] = HIGH;
button_state[1] = HIGH;
button_state[2] = HIGH;
button_state[3] = HIGH;
// establecer que el pin digital es una señal de salida
pinMode(RELE, OUTPUT);
myservo_S1.attach(0); // establecer que el pin digital es una señal de salida
myservo_S2.attach(1); // establecer que el pin digital es una señal de salida
myservo_S3.attach(2); // establecer que el pin digital es una señal de salida
}
// Máquina de estados
void loop() {
switch (estado) {
//FUERA DE SERVICIO
case S_FS:
digitalWrite(RELE, HIGH);
delay(90);
myservo_S1.write(Giro_Off);
delay(300);
myservo_S2.write(Giro_Off);
delay(200);
myservo_S3.write(Giro_Off);
if (flancoSubida(BTN_NORMAL)) {
estado = S_NORMAL;
}
if (flancoSubida(BTN_MTTO)) {
estado = S_MTTO;
}
break;
//ESTADO NORMAL DE OPERACION
case S_NORMAL:
myservo_S2.write(Giro_On);
delay(100);
myservo_S1.write(Giro_On);
delay(100);
digitalWrite(RELE, LOW);
delay(300);
myservo_S3.write(Giro_Off);
if (flancoSubida(BTN_FS)) {
estado = S_FS;
}
if (flancoSubida(BTN_MTTO)) {
estado = S_MTTO;
}
if (flancoSubida(BTN_RESET)) {
estado = S_RESET;
}
break;
//ESTADO DE MANTENOMIENTO
case S_MTTO:
myservo_S3.write(Giro_On);
delay(90);
digitalWrite(RELE, HIGH);
delay(300);
myservo_S1.write(Giro_Off);
delay(200);
myservo_S2.write(Giro_Off);
if (flancoSubida(BTN_FS)) {
estado = S_FS;
}
if (flancoSubida(BTN_NORMAL)) {
estado = S_NORMAL;
}
break;
// ESTADO AL QUE LLEVA LA FALLA
case S_RESET:
digitalWrite(RELE,HIGH);
if (flancoSubida(BTN_NORMAL)) {
estado = S_NORMAL;
}
if (flancoSubida(BTN_FS)) {
estado = S_FS;
}
break;
}
}