Diferencia entre revisiones de «Bounce»

De ArduWiki
Saltar a: navegación, buscar
(Página creada con «== Descripción == == Placas aplicables == == Sintaxis == <pre> </pre> == Métodos == == Comentarios == == Advertencias == == Ejemplo == <syntaxhighlight lang="c++">...»)
 
(Referencias externas)
 
(No se muestran 11 ediciones intermedias del mismo usuario)
Línea 1: Línea 1:
 
== Descripción ==
 
== Descripción ==
 +
La librería [https://github.com/thomasfredericks/Bounce2/wiki Bounce2.h] de Thomas Frefericks que esta en el gestos de librerias del [[IDE]].
 +
 +
{{Nota|Para saber mas del efecto Rebote.}}
  
 
== Placas aplicables ==
 
== Placas aplicables ==
Línea 5: Línea 8:
 
== Sintaxis ==
 
== Sintaxis ==
 
<pre>
 
<pre>
 +
#include <Bounce2.h>
 +
Bounce rebote = Bounce();
 +
rebote.attach(pin);
 +
rebote.interval(pausa);
 
</pre>
 
</pre>
 +
 +
== Parametros ==
 +
;pin:pin del pulsador.
 +
;pausa:Valor de retardo.
  
 
== Métodos ==
 
== Métodos ==
 +
{| class="wikitable"
 +
|+Metodos disponibles en libreria Bounce2.h
 +
|-
 +
! Método !! Descripción
 +
|-
 +
| rebote.attach(pin) || Asigna el pin
 +
|-
 +
| rebote.interval(pausa) || Asigna el tiempo en milisegundos
 +
|-
 +
| rebote.update() || Debido a que Bounce no usa interrupciones, debe "actualizar" el objeto antes de leer su valor y debe hacerlo con la mayor frecuencia posible (eso significa incluirlo en su bucle loop()). El método update() actualiza el objeto y devuelve verdadero (1) si el estado del pin cambia. Falso (0) si no. Solo llame a update() una vez por loop().
 +
|-
 +
| rebote.read() || Lee el estado del pin luego de update().
 +
|-
 +
| rebote.fell() || Devuelve true si las transiciones de señal del pin es de HIGH a LOW (falling).
 +
|-
 +
| rebote.rose() || Devuelve true si las transiciones de señal del pin es de LOW a HIGH (rissing).
 +
|}
  
 
== Comentarios ==
 
== Comentarios ==
Línea 13: Línea 41:
 
== Advertencias ==
 
== Advertencias ==
  
== Ejemplo ==
+
== Ejemplo 1 ==
 +
El ejemplo basico, un solo pulsador en pin 2.
 +
 
 +
<syntaxhighlight lang="c++">
 +
#include <Bounce2.h>
 +
#define PIN 2
 +
#define LED 13
 +
Bounce rebote = Bounce();  //Instancia objeto
 +
 
 +
void setup() {
 +
  pinMode(PIN, INPUT_PULLUP);
 +
  rebote.attach(PIN);      //Cuando ya esta definido el pin
 +
  rebote.interval(5);
 +
  pinMode(LED, OUTPUT);
 +
}
 +
 
 +
void loop() {
 +
  rebote.update();          //Actualiza
 +
  bool valor = rebote.read();
 +
  if (valor) {
 +
      digitalWrite(LED, LOW);    //Apaga LED
 +
  }else{
 +
      digitalWrite(LED, HIGH);  //Prende LED
 +
  }
 +
}
 +
</syntaxhighlight>
 +
 
 +
== Ejemplo 2 ==
 +
En este ejemplo configuramos 8 pines.
 +
 
 +
<syntaxhighlight lang="c++">
 +
#include <Bounce2.h>
 +
#define LED 13
 +
#define NUM 8
 +
const byte PULSADOR[NUM] = {2, 3, 4, 5, 6, 7, 8, 9};
 +
Bounce * boton = new Bounce[NUM];              //Instancia todos los pines
 +
 
 +
void setup() {
 +
  for (byte i=0; i<NUM; i++) {
 +
      boton[i].attach(PULSADOR[i], INPUT_PULLUP);
 +
      boton[i].interval(25);
 +
  }
 +
  pinMode(LED, OUTPUT);
 +
}
 +
 
 +
void loop() {
 +
  for (byte i=0; i<NUM; i++)  {
 +
      boton[i].update();                        //Actualiza
 +
      if (boton[i].fell()) {
 +
        digitalWrite(LED, !digitalRead(LED));  //Conmuta LED
 +
      }
 +
  }
 +
}
 +
</syntaxhighlight>
 +
 
 +
== Ejemplo 3 ==
 +
Muestra los milisegundos entre dos pulsaciones.
 +
 
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 +
#include <Bounce2.h>
 +
 +
#define BOTON 2
 +
Bounce rebote = Bounce();
 +
unsigned long tiempo = 0;
 +
 +
void setup() {
 +
  Serial.begin(57600);
 +
  pinMode(BOTON, INPUT_PULLUP);
 +
  rebote.attach(BOTON);
 +
  rebote.interval(5); 
 +
}
 +
 +
void loop() {
 +
  rebote.update();
 +
  if (rebote.fell()) {;
 +
      Serial.println(millis()-tiempo);
 +
      tiempo = millis(); 
 +
  }
 +
}
 +
</syntaxhighlight>
 +
 +
== Ejemplo 4 ==
 +
Muestra cuando el boton el presionado y soltado. Ademas si lo mantienes presionado te da un mensaje cada medio segundo.
 +
 +
<syntaxhighlight lang="c++">
 +
#include <Bounce2.h>
 +
 +
#define BOTON 2
 +
#define LED 13
 +
 +
Bounce rebote = Bounce();
 +
unsigned long tiempo = 0;
 +
bool valor = false;
 +
 +
void setup() {
 +
  Serial.begin(57600);
 +
  pinMode(BOTON, INPUT_PULLUP);
 +
  rebote.attach(BOTON);
 +
  rebote.interval(5);
 +
  pinMode(LED, OUTPUT);
 +
}
 +
 +
void loop() {
 +
  if (rebote.update()) {
 +
      if (rebote.read()){
 +
        digitalWrite(LED, LOW);
 +
        Serial.println("Boton soltado (HIGH)"); 
 +
        valor = false;
 +
      }else{
 +
        digitalWrite(LED, HIGH);
 +
        Serial.println("Boton presionado (LOW)");
 +
        tiempo = millis();
 +
        valor = true;
 +
      }
 +
  }
 +
  if (valor) {
 +
      if (millis()-tiempo >= 500) {
 +
        tiempo = millis();
 +
        digitalWrite(LED, LOW);
 +
        Serial.println("Boton reactivado");
 +
      }
 +
  }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Línea 22: Línea 171:
 
== Referencias externas ==
 
== Referencias externas ==
 
* [https://www.arduinolibraries.info/libraries All Libraries]
 
* [https://www.arduinolibraries.info/libraries All Libraries]
 +
* [https://playground.arduino.cc/Code/Bounce/ Bounce2]
  
 
[[Category:Librerias]]
 
[[Category:Librerias]]

Revisión actual del 19:33 4 may 2019

Descripción

La librería Bounce2.h de Thomas Frefericks que esta en el gestos de librerias del IDE.

Nota: Para saber mas del efecto Rebote.


Placas aplicables

Sintaxis

#include <Bounce2.h>
Bounce rebote = Bounce(); 
rebote.attach(pin);
rebote.interval(pausa);

Parametros

pin
pin del pulsador.
pausa
Valor de retardo.

Métodos

Metodos disponibles en libreria Bounce2.h
Método Descripción
rebote.attach(pin) Asigna el pin
rebote.interval(pausa) Asigna el tiempo en milisegundos
rebote.update() Debido a que Bounce no usa interrupciones, debe "actualizar" el objeto antes de leer su valor y debe hacerlo con la mayor frecuencia posible (eso significa incluirlo en su bucle loop()). El método update() actualiza el objeto y devuelve verdadero (1) si el estado del pin cambia. Falso (0) si no. Solo llame a update() una vez por loop().
rebote.read() Lee el estado del pin luego de update().
rebote.fell() Devuelve true si las transiciones de señal del pin es de HIGH a LOW (falling).
rebote.rose() Devuelve true si las transiciones de señal del pin es de LOW a HIGH (rissing).

Comentarios

Advertencias

Ejemplo 1

El ejemplo basico, un solo pulsador en pin 2.

#include <Bounce2.h>
#define PIN 2
#define LED 13
Bounce rebote = Bounce();   //Instancia objeto

void setup() {
   pinMode(PIN, INPUT_PULLUP);
   rebote.attach(PIN);      //Cuando ya esta definido el pin
   rebote.interval(5);
   pinMode(LED, OUTPUT);
}

void loop() {
   rebote.update();          //Actualiza
   bool valor = rebote.read();
   if (valor) {
      digitalWrite(LED, LOW);    //Apaga LED
   }else{
      digitalWrite(LED, HIGH);   //Prende LED
   }
}

Ejemplo 2

En este ejemplo configuramos 8 pines.

#include <Bounce2.h>
#define LED 13
#define NUM 8
const byte PULSADOR[NUM] = {2, 3, 4, 5, 6, 7, 8, 9};
Bounce * boton = new Bounce[NUM];              //Instancia todos los pines

void setup() {
   for (byte i=0; i<NUM; i++) {
      boton[i].attach(PULSADOR[i], INPUT_PULLUP);
      boton[i].interval(25);
   }
   pinMode(LED, OUTPUT);
}

void loop() {
   for (byte i=0; i<NUM; i++)  {
      boton[i].update();                         //Actualiza
      if (boton[i].fell()) {
         digitalWrite(LED, !digitalRead(LED));   //Conmuta LED
      }
   }
}

Ejemplo 3

Muestra los milisegundos entre dos pulsaciones.

#include <Bounce2.h>

#define BOTON 2
Bounce rebote = Bounce(); 
unsigned long tiempo = 0;

void setup() {
  Serial.begin(57600); 
  pinMode(BOTON, INPUT_PULLUP);
  rebote.attach(BOTON);
  rebote.interval(5);  
}

void loop() { 
   rebote.update();
   if (rebote.fell()) {;
      Serial.println(millis()-tiempo);
      tiempo = millis();  
   }
}

Ejemplo 4

Muestra cuando el boton el presionado y soltado. Ademas si lo mantienes presionado te da un mensaje cada medio segundo.

#include <Bounce2.h>

#define BOTON 2
#define LED 13

Bounce rebote = Bounce(); 
unsigned long tiempo = 0;
bool valor = false;

void setup() { 
  Serial.begin(57600);
  pinMode(BOTON, INPUT_PULLUP);
  rebote.attach(BOTON);
  rebote.interval(5); 
  pinMode(LED, OUTPUT);
}

void loop() {
   if (rebote.update()) {
      if (rebote.read()){
         digitalWrite(LED, LOW); 
         Serial.println("Boton soltado (HIGH)");  
         valor = false;
      }else{
         digitalWrite(LED, HIGH);
         Serial.println("Boton presionado (LOW)");
         tiempo = millis();
         valor = true;
      }
   }
   if (valor) {
      if (millis()-tiempo >= 500) {
         tiempo = millis();
         digitalWrite(LED, LOW);
         Serial.println("Boton reactivado");
      }
   }
}

Vea también


Referencias externas