Estimados, buenas tardes, hace un tiempo me ayudaron con un problema en el foro "AUTOMATIZACIÓN DE SIRENA DE BOMBEROS" el sistema y código funciona pero sucede lo que algunos de los expertos mencionaban sobre el uso de pulsadores, el rebote o ruido del sistema hace fallar a veces al programa.
Lei sobre el concepto de "Debounce" pero no logro comprender como encuadrarlo en el código mio.
Les comparto el foro anterior para ponerlos en tema:
https://forum.arduino.cc/index.php?topic=624089.0
y el código con las modificaciones que estoy intentando formular y donde me estoy quedando haciendo aguas,
const int BOTON1TOQUE = A0 ;// el número del pin del botón
const int BOTON2TOQUES = A1 ; // el número del pin del botón
const int BOTON3TOQUES = A2 ;// el número del pin del botón
const int LED1TOQUE = 11 ; // el número del pin del LED
const int LED2TOQUES = 10 ; // el número del pin del LED
const int LED3TOQUES = 9 ; // el número del pin del LED
const int ARRANQUECONTACTOR1 = 13 ; // el número del pin del contactor
const int ARRANQUECONTACTOR2 = 12 ; // el número del pin del contactor
// Las variables cambiarán:
int ledState = HIGH ; // el estado actual del pin de salida
int buttonState; // la lectura actual del pin de entrada
int lastButtonState = LOW ; // la lectura anterior del pin de entrada
// las siguientes variables son longitudes sin signo porque el tiempo, medido en
// milisegundos, se convertirá rápidamente en un número mayor que el que se puede almacenar en un int.
unsigned long lastDebounceTime = 0 ; // la última vez que se activó el pin de salida
unsigned long debounceDelay = 50 ; // el tiempo de rebote; aumentar si la salida parpadea
int SubidaPunto1 = 25000; //tiempo en levantar la sirena en punto 1
int BajadaPunto1 = 5000; //tiempo en bajar la sirena en punto 1
int DelayEntrePunto1y2 = 1500; //tiempo de conmutacion entre punto 1 y 2
int SubidaPunto2 = 18000; //tiempo en levantar la sirena en punto 2
int BajadaPunto2 = 10000; //tiempo en bajar la sirena en punto 2
void setup(){
pinMode(BOTON1TOQUE, INPUT);
pinMode(BOTON2TOQUES, INPUT);
pinMode(BOTON3TOQUES, INPUT); //INPUT_PULLUP comando anterior
pinMode(LED1TOQUE, OUTPUT);
pinMode(LED2TOQUES, OUTPUT);
pinMode(LED3TOQUES, OUTPUT);
pinMode(ARRANQUECONTACTOR1, OUTPUT);
pinMode(ARRANQUECONTACTOR2, OUTPUT);
digitalWrite ( LED1TOQUE , ledState ) ;
digitalWrite ( LED2TOQUES , ledState ) ;
digitalWrite ( LED3TOQUES , ledState ) ;
digitalWrite ( ARRANQUECONTACTOR1 , ledState ) ;
digitalWrite ( ARRANQUECONTACTOR2 , ledState ) ;
}
void loop()
{
// lee el estado del switch en una variable local:
int reading = digitalRead ( BOTON1TOQUE ) ;
int reading = digitalRead ( BOTON2TOQUES ) ;
int reading = digitalRead ( BOTON3TOQUES ) ;
// verifique si acaba de presionar el botón
// (es decir, la entrada pasó de BAJA a ALTA), y ha esperado lo suficiente
// desde la última vez que presionó para ignorar cualquier ruido:
// Si el interruptor cambió, debido al ruido o al presionar:
if ( lectura ! = LastButtonState ) {
// restablecer el temporizador
antirrebote lastDebounceTime = millis ( ) ;
}
if ( ( millis ( ) - lastDebounceTime ) > debounceDelay ) {
// sea cual sea la lectura, ha estado allí por más tiempo que el debounce
// delay, así que tómalo como el estado actual real:
// si el estado del botón tiene cambiado:
if ( lectura ! = buttonState) {
buttonState = lectura ;
//HASTA ACA VOY BIEN... LO COMPRENDO,.. NO SE COMO INTRODUCIR EL CODIGO MIO
//DE LOS IF DE MAS ABAJO PARA REALIZAR LA LECTURA DE LOS BOTONES DE LOS 3 TOQUES
// Y COMO FUNCIONA EL RESETEO DE CADA UNA DE LAS BVARIABLES DE CADA PULSADOR
// solo alterna el LED si el nuevo estado del botón es ALTO
si ( buttonState == HIGH ) {
ledState = ! ledState ;
}
}
}
// establece el LED:
digitalWrite ( ledPin , ledState ) ;
// guarda la lectura. La próxima vez a través del ciclo, será lastButtonState:
lastButtonState = reading ;
if (digitalRead(BOTON1TOQUE) == LOW)
{
//1 toque
digitalWrite(LED1TOQUE, HIGH);
//Principal
digitalWrite(ARRANQUECONTACTOR1, HIGH);
delay(SubidaPunto1); //Wait for 25 second(s)
digitalWrite(ARRANQUECONTACTOR1, LOW);
delay(DelayEntrePunto1y2); //Wait for 1.5 second(s)
//Toque 1
digitalWrite(ARRANQUECONTACTOR2, HIGH);
delay(SubidaPunto2); //Wait for 18 second(s)
digitalWrite(ARRANQUECONTACTOR2, LOW);
delay(BajadaPunto2); //Wait for 10 second(s)
digitalWrite(LED1TOQUE, LOW);
}
if (digitalRead(BOTON2TOQUES) == LOW) {
//2 toques
digitalWrite(LED2TOQUES, HIGH);
digitalWrite(LED1TOQUE, HIGH);
//Principal
digitalWrite(ARRANQUECONTACTOR1, HIGH);
delay(SubidaPunto1); // Wait for 25 second(s)
digitalWrite(ARRANQUECONTACTOR1, LOW);
delay(DelayEntrePunto1y2); //Wait for 1.5 second(s)
//Toque 1
digitalWrite(ARRANQUECONTACTOR2, HIGH);
delay(SubidaPunto2); //Wait for 18 second(s)
digitalWrite(ARRANQUECONTACTOR2, LOW);
delay(BajadaPunto2); //Wait for 10 second(s)
//Toque 2
digitalWrite(ARRANQUECONTACTOR2, HIGH);
delay(SubidaPunto2); //Wait for 18 second(s)
digitalWrite(ARRANQUECONTACTOR2, LOW);
delay(BajadaPunto2); //Wait for 10 second(s)
digitalWrite(LED1TOQUE, LOW);
digitalWrite(LED2TOQUES, LOW);
}
if (digitalRead(BOTON2TOQUES) == LOW) {
//3 toques
digitalWrite(LED3TOQUES, HIGH);
digitalWrite(LED2TOQUES, HIGH);
digitalWrite(LED1TOQUE, HIGH);
//Principal
digitalWrite(ARRANQUECONTACTOR1, HIGH);
delay(SubidaPunto1); //Wait for 25 second(s)
digitalWrite(ARRANQUECONTACTOR1, LOW);
delay(DelayEntrePunto1y2); // Wait for 1.5 second(s)
//Toque 1
digitalWrite(ARRANQUECONTACTOR2, HIGH);
delay(SubidaPunto2); // Wait for 18 second(s)
digitalWrite(ARRANQUECONTACTOR2, LOW);
delay(BajadaPunto2); // Wait for 10 second(s)
//Toque 2
digitalWrite(ARRANQUECONTACTOR2, HIGH);
delay(SubidaPunto2); // Wait for 18 second(s)
digitalWrite(ARRANQUECONTACTOR2, LOW);
delay(BajadaPunto2); // Wait for 10 second(s)
//Toque 3
digitalWrite(ARRANQUECONTACTOR2, HIGH);
delay(SubidaPunto2); // Wait for 18 second(s)
digitalWrite(ARRANQUECONTACTOR2, LOW);
delay(BajadaPunto2); // Wait for 10 second(s)
digitalWrite(LED3TOQUES, LOW);
digitalWrite(LED2TOQUES, LOW);
digitalWrite(LED1TOQUE, LOW);
}
}