Read the buttons
What this example shows
Reading the three pushbuttons and reacting only when one changes. Buttons.ino prints a line to the Serial Monitor each time a button is pressed or released.
The example code
/**
**************************************************
*
* @file Buttons.ino
* @brief Button test for Soldered Button, LED & Buzzer Board
* Host: ESP32 WROVER-E
*
* Prints button states to Serial Monitor (115200 baud).
* Shows press and release events.
*
* @authors Fran Fodor @ soldered.com
***************************************************/
#include "ButtonLedBuzzerBoard-SOLDERED.h"
ButtonLedBuzzerBoard_Soldered board;
uint8_t lastButtons = 0;
void setup()
{
Serial.begin(115200);
Wire.begin(); // if not using Qwiic, specify I2C pins: Wire.begin(21, 22)
board.begin();
delay(2000);
Serial.println("Button, LED & Buzzer Board - Button Test");
Serial.println("Press any button...");
}
void loop()
{
uint8_t buttons = board.readButtons();
if (buttons != lastButtons)
{
for (uint8_t i = 0; i < 3; i++)
{
uint8_t mask = 1 << i;
if ((buttons & mask) && !(lastButtons & mask))
{
Serial.print("BTN"); Serial.print(i + 1); Serial.println(" pressed");
}
else if (!(buttons & mask) && (lastButtons & mask))
{
Serial.print("BTN"); Serial.print(i + 1); Serial.println(" released");
}
}
lastButtons = buttons;
}
delay(20);
}
This example also uses Wire, which is not part of the Soldered-Button-LED-Buzzer-Board library: it comes with your Arduino board package. Wire.begin() joins the I2C bus as a controller. Without Qwiic, the comment in the sketch shows how to pass your own I2C pins, for example Wire.begin(21, 22) on the ESP32 host the example was written for.
Expected result:
Open the Serial Monitor at 115200 baud. After about two seconds the sketch prints its header. Pressing and releasing BTN1 then adds the last two lines; the other buttons print BTN2 and BTN3 the same way.
Button, LED & Buzzer Board - Button Test
Press any button...
BTN1 pressed
BTN1 released
Functions used
ButtonLedBuzzerBoard_Soldered(uint8_t address = BLB_DEFAULT_ADDRESS)returns NoneCreates the board object and remembers its I2C address. All three LEDs start as off in the library's colour buffer. The example uses the default address, 0x30, which the board has with all three address switches OFF.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | address | The board's I2C address, 0x30 to 0x37 as set by the address switch. Defaults to BLB_DEFAULT_ADDRESS (0x30). |
begin()returns voidStarts the I2C bus by calling Wire.begin(). Call it once in setup(), before any other function.
ReturnsNothing.
readButtons()returns uint8_tReads the board's button register and returns the state of all three buttons in one byte. Bit 0 is BTN1, bit 1 is BTN2 and bit 2 is BTN3; a bit is 1 while its button is pressed. If the board does not answer, the function returns 0, which looks the same as no button pressed.
ReturnsThe button bits: 0x01 for BTN1, 0x02 for BTN2, 0x04 for BTN3, combined when several are pressed.
Putting it together
readButtons() returns all three buttons as bits of one byte, so the sketch reads the board once per loop and compares the result with the previous one (lastButtons). A bit that turned on is a press and a bit that turned off is a release, and the loop repeats every 20 ms. Unchanged buttons print nothing, so holding a button down prints pressed once.