This code has a button, it works:
- If I press the button lights a "led" and "fan".
- Again I press the button, the fan turns off and the LED blinks every second.
- If djo down the button for 3 seconds, LED and fan off.
byte Pulsador =PIN_B0; // Pines usados
byte Led =PIN_D0;
byte Ventilador=PIN_D1;
#define ON HIGH
#define OF LOW
boolean Estado_Pulsador;
boolean Estado_Anterior = false;
boolean Led_Encendido=false;
boolean Ven_Encendido=false;
byte Secuencia = 0; // Para manejar la secuencia de salidas
unsigned long Parpadeo=1000; // Tiempo de parpadeo de led
unsigned long T_Anterior=0; // Auxiliar para controlar tiempo de parpadeo
unsigned long T_Apagar=3000; // Tiempo a pulsar para apagar
unsigned long T_Pulsado=0; // Auxiliar para controlar tiempo de boton pulsado
void setup()
{
pinMode(Pulsador, INPUT);
pinMode(Led, OUTPUT);
pinMode(Ventilador, OUTPUT);
digitalWrite(Led, OF);
digitalWrite(Ventilador, OF);
}
void loop()
{
Estado_Pulsador = !digitalRead(Pulsador); // Lee el estado del botón.
delay(20); // Antirebotes.
if (Estado_Pulsador && !Estado_Anterior) // Pulsador pasa de low a high
{
T_Pulsado=millis()+T_Apagar; // Para controlar el timpo que pasa pulsado
Estado_Anterior=true; // No enta en el if hasta q pulsador pase de high a low
Secuencia++; // Pasamos a siguiente secuencia
}
if (!Estado_Pulsador && Estado_Anterior) // Pulsador pasa de high a low
{
Estado_Anterior=false; // No enta en el if hasta q pulsador pase de low a high
T_Pulsado=0; // Al soltar pulsador ya no controlamos el tiempo
}
if (T_Pulsado>0 && Estado_Pulsador && millis()>T_Pulsado)
{ // Hay tiempo cargado para controlar el fin y pulsador en high y ha transcurrido el tiempo del pulsador activo
T_Pulsado=0; // Ya no controlamos timepo de pulsado
Secuencia=0; // Secuencia 0, todo apagado
}
if (Secuencia==0) // Todo apagado
{
Led_Encendido=false;
Ven_Encendido=false;
T_Anterior=0; // Para que pueda funcionar el parpadeo de nuevo
}
else if (Secuencia==1) // Pulsamos por 1ª vez, todo encendido
{
Led_Encendido=true;
Ven_Encendido=true;
}
else if (Secuencia==2) // Pulsamos por 2ª vez, Apaga ventilador y led parpadea
{
Ven_Encendido=false;
if (millis()>T_Anterior) // Tiempo parpadeo finalizado
{
T_Anterior=millis() + Parpadeo;// Tiempo a esperar para siguiente parpadeo
Led_Encendido=!Led_Encendido; // de encendido a apagado y viceversa
}
}
else {Secuencia=1; }
if (Led_Encendido) {digitalWrite(Led, ON);} else {digitalWrite(Led, OF);}
if (Ven_Encendido) {digitalWrite(Ventilador, ON);} else {digitalWrite(Ventilador, OF);}
}
What I want is stored in EEPROM and any status Led ventidador when Teensy + + 2.0 loses power. When you turn Teensy + + 2.0 automatically retrieves the saved state of Led and fan.
You have to modify the code but do not.
Any help?
Greeting.
PD: Sorry if this topic does not go here.