Arduino - Conteo de botones - OLED

En este tutorial, vamos a usar Arduino:

En este tutorial, el botón también tiene antirrebote sin usar la función delay(). Ver ¿Por qué necesitamos antirrebote?

Acerca de OLED y el botón

Si no conoces OLED y un botón (disposición de pines, cómo funciona, cómo programar ...), aprende sobre ellos en los siguientes tutoriales:

Diagrama de Cableado

Diagrama de cableado de Arduino para botón y OLED

This image is created using Fritzing. Click to enlarge image

Código de Arduino - mostrando el conteo de botones en OLED

/* * Este código de Arduino fue desarrollado por es.newbiely.com * Este código de Arduino se proporciona al público sin ninguna restricción. * Para tutoriales completos y diagramas de cableado, visite: * https://es.newbiely.com/tutorials/arduino/arduino-button-count-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <ezButton.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C ezButton button(7); // create ezButton object that attach to pin 7; unsigned long lastCount = 0; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display button.setDebounceTime(50); // set debounce time to 50 milliseconds button.setCountMode(COUNT_FALLING); } void loop() { button.loop(); // MUST call the loop() function first unsigned long count = button.getCount(); if (lastCount != count) { Serial.println(count); // print count to Serial Monitor oled.clearDisplay(); // clear display oled.println(count); // display count oled.display(); // show on OLED lastCount != count; } }

Pasos R\u00e1pidos

  • Ve al icono de Bibliotecas en la barra izquierda del IDE de Arduino.
  • Busca “ezButton”, y luego encuentra la biblioteca de botones de ArduinoGetStarted.
  • Haz clic en el botón Instalar para instalar la biblioteca ezButton.
Librería de botones de Arduino
  • Busca “SSD1306”, luego encuentra la biblioteca SSD1306 de Adafruit
  • Haz clic en el botón Instalar para instalar la biblioteca.
Biblioteca OLED para Arduino
  • Se le pedirá instalar algunas dependencias de bibliotecas adicionales.
  • Haga clic en el botón Instalar todo para instalar todas las dependencias de las bibliotecas.
Biblioteca de sensores de Arduino Adafruit GFX
  • Copia el código anterior y ábrelo con Arduino IDE
  • Haz clic en el botón Subir en Arduino IDE para subir el código al Arduino
  • Presiona el botón varias veces
  • Observa cómo el número de conteo cambia en el OLED

El código anterior solo muestra el contador de pulsaciones del botón en la esquina superior izquierda. ¡Modifiquemos el código para centrarlo!

Código de Arduino - Alineación vertical y horizontal centrada en OLED

/* * Este código de Arduino fue desarrollado por es.newbiely.com * Este código de Arduino se proporciona al público sin ninguna restricción. * Para tutoriales completos y diagramas de cableado, visite: * https://es.newbiely.com/tutorials/arduino/arduino-button-count-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <ezButton.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C ezButton button(7); // create ezButton object that attach to pin 7; unsigned long lastCount = 0; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display button.setDebounceTime(50); // set debounce time to 50 milliseconds button.setCountMode(COUNT_FALLING); } void loop() { button.loop(); // MUST call the loop() function first unsigned long count = button.getCount(); if (lastCount != count) { Serial.println(count); // print count to Serial Monitor String text = String(count); oledDisplayCenter(text); lastCount != count; } } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

※ Nota:

El código anterior alinea automáticamente el texto en el centro horizontal y vertical de la pantalla OLED. Consulte Cómo centrar horizontal y verticalmente el texto en OLED para más detalles.

Video Tutorial

Estamos considerando crear tutoriales en video. Si considera que los tutoriales en video son importantes, suscríbase a nuestro canal de YouTube para motivarnos a crear los videos.

※ NUESTROS MENSAJES

  • No dude en compartir el enlace de este tutorial. Sin embargo, por favor no use nuestro contenido en otros sitios web. Hemos invertido mucho esfuerzo y tiempo en crear el contenido, ¡por favor respete nuestro trabajo!