Conmutador Y Rebote de Contactos
Conmutador Y Rebote de Contactos
Conmutador usando delay(); al pulsar: int sw6 = 6; int led = 10; void setup() { pinMode(sw6,INPUT_PULLUP); // pin 6 entrada y pullup ON pinMode(led,OUTPUT); // pin 8 salida digitalWrite(led,LOW); // valor inicial del led: Apagado } void loop() { if(digitalRead(sw6)==0) { delay(20); if(digitalRead(led)==0) digitalWrite(led,HIGH); else digitalWrite(led,LOW); while(digitalRead(sw6)==0) {} delay(20); } }
2
Conmutador usando delay(); al soltar: int sw6 = 6; int led = 10; void setup() { pinMode(sw6,INPUT_PULLUP); // pin 6 entrada y pullup ON pinMode(led,OUTPUT); // pin 8 salida digitalWrite(led,LOW); // valor inicial del led: Apagado } void loop() { if(digitalRead(sw6)==0) { delay(20); while(digitalRead(sw6)==0) {} delay(20); if(digitalRead(led)==0) digitalWrite(led,HIGH); else digitalWrite(led,LOW); } }
Conmutador usando delay(); y operador booleano de negacin, al pulsar: int sw6 = 6; int led = 10; void setup() { pinMode(sw6,INPUT_PULLUP); // pin 6 entrada y pullup ON pinMode(led,OUTPUT); // pin 8 salida digitalWrite(led,LOW); // valor inicial del led: Apagado } void loop() { if(!digitalRead(sw6)) { delay(20); digitalWrite(led,!digitalRead(led)); while(digitalRead(sw6)==0) {} delay(20); } }
Conmutador usando delay(); y operador booleano de negacin, al soltar: int sw6 = 6; int led = 10; void setup()
3
{ pinMode(sw6,INPUT_PULLUP); // pin 6 entrada y pullup ON pinMode(led,OUTPUT); // pin 8 salida digitalWrite(led,LOW); // valor inicial del led: Apagado } void loop() { if(!digitalRead(sw6)) { delay(20); while(!digitalRead(sw6)) {} delay(20); digitalWrite(led,!digitalRead(led)); } }
Conmutador usando muestreo cada 1 ms, al presionar: int sw6 = 6; int led = 10; void setup() { pinMode(sw6,INPUT_PULLUP); // pin 6 entrada y pullup ON pinMode(led,OUTPUT); // pin 10 salida digitalWrite(led,LOW); // valor inicial del led: Apagado } void loop() { for (int conta = 0; conta <= 10; conta++) // se espera a que sw6=0 (pulsado) { delay(1); // retardo de 1 ms if (digitalRead(sw6)== 1) // si hay rebote (sw6=1) conta = 0; // restart count // contador rebote a cero } // hasta que sw6 est en 0, durante 10 lecturas sucesivas digitalWrite(led,!digitalRead(led)); // se conmuta la salida // se espera a que sw6=1 (soltado) retardo de 1 ms si hay rebote (sw6=0) contador rebote a cero que sw6 est en 1, durante 10 lecturas sucesivas
for (int conta = 0; conta <= 10; conta++) { delay(1); // if (digitalRead(sw6)== 0) // conta = 0; // } // hasta }
Conmutador usando muestreo cada 1 ms, al soltar: int sw6 = 6; int led = 10; void setup() { pinMode(sw6,INPUT_PULLUP); // pin 6 entrada y pullup ON pinMode(led,OUTPUT); // pin 10 salida
4
digitalWrite(led,LOW); } void loop() { // int conta; // contador rebote for (int conta = 0; conta <= 10; conta++) // se espera a que sw6=0 (pulsado) { delay(1); // retardo de 1 ms if (digitalRead(sw6)== 1) // si hay rebote (sw6=1) conta = 0; // restart count // contador rebote a cero } // hasta que sw6 est en 0, durante 10 lecturas sucesivas for (int conta = 0; conta <= 10; conta++) { delay(1); // if (digitalRead(sw6)== 0) // conta = 0; // } // hasta digitalWrite(led,!digitalRead(led)); } // se espera a que sw6=1 (soltado) retardo de 1 ms si hay rebote (sw6=0) contador rebote a cero que sw6 est en 1, durante 10 lecturas sucesivas // valor inicial del led: Apagado
// se conmuta la salida
Conmutador usando millis(), al soltar: int sw6 = 6; int Led = 10; long int tiempo_inicial_6=0; // guarda el contenido de millis() en la lectura de sw6 boolean Lectura_sw6_realizada=false; // variable para saber si ya se ha leido sw6 void setup() { pinMode(sw6,INPUT_PULLUP); // pin 6 entrada y pullup ON pinMode(Led,OUTPUT); // pin 10 salida digitalWrite(Led,LOW); // valor inicial del Led: Apagado } void loop() { ///Leemos el pulsador 6 if(digitalRead(sw6)==0 && Lectura_sw6_realizada==false) { Lectura_sw6_realizada=true; tiempo_inicial_6=millis(); } if(millis()-tiempo_inicial_6 > 20 && Lectura_sw6_realizada==true) { tiempo_inicial_6=0; Lectura_sw6_realizada=false; if(digitalRead(sw6)==HIGH) { digitalWrite(Led,!digitalRead(Led)); // se conmuta la salida}
5
} } }
Bounce myButton = Bounce(pin, milliseconds); Create a Bounce object called myButton, using "pin" and waiting for bouncing to end within "milliseconds" time. You may create multiple Bounce objects, for as many different pins as necessary. myButton.update(); Read the button and update its status. This update() function must be called regularly, so input on the pin is properly recognized. Return is true if the input changes, or false otherwise. myButton.fallingEdge(); Check for a high to low transition. For a pushbutton connected between the pin and ground, this corresponds to the button being pressed. myButton.risingEdge(); Check for a low to high transition. For a pushbutton connected between the pin and ground, this corresponds to the button being released.
Descargar la librera de: http://playground.arduino.cc/Code/Bounce #include <Bounce.h> int led = 10; // #define led 10 int sw6 = 6; // #define sw6 6 Bounce rebote = Bounce(sw6,20); void setup() { pinMode(sw6,INPUT_PULLUP); pinMode(led,OUTPUT); digitalWrite(led,LOW); } void loop() { if (rebote.update()) { if ( rebote.read() == LOW) digitalWrite(led,!digitalRead(led)); } } // tiempo de 20 ms
Conmutador usando librera Bounce, al soltar: #include <Bounce.h> int led = 10; int sw6 = 6; Bounce entrada6 = Bounce(sw6,20); void setup() // tiempo de 20 ms
6
{ pinMode(sw6,INPUT_PULLUP); pinMode(led,OUTPUT); digitalWrite(led,LOW); } void loop() { if (entrada6.update()) { if (entrada6.risingEdge()) { if (entrada6.read() == HIGH) { digitalWrite(led,!digitalRead(led)); } } } }
Conmutador usando librera Bounce, al pulsar, usando fallingEdge(): #include <Bounce.h> int led = 10; int sw6 = 6; Bounce entrada6 = Bounce(sw6,20); void setup() { pinMode(sw6,INPUT_PULLUP); pinMode(led,OUTPUT); } void loop() { if (entrada6.update()) { if (entrada6.fallingEdge()) { if (entrada6.read() == LOW) { digitalWrite(led,!digitalRead(led)); } } } } // // tiempo de 20 ms