Skip to main content

Full demo

What this example shows​

All three parts of the board in one sketch: the button states printed to the Serial Monitor, all LEDs changing colour every two seconds, and a short beep on each change.

The example code​

Select Board
FullDemo.ino
/**
**************************************************
*
* @file FullDemo.ino
* @brief Full demo for Soldered Button, LED & Buzzer Board
* Host: ESP32 WROVER-E
*
* - Prints button states to Serial (115200 baud)
* - Cycles all LEDs through colors every 2 seconds
* - Short beep on each color change
*
* @authors Fran Fodor @ soldered.com
***************************************************/

#include "ButtonLedBuzzerBoard-SOLDERED.h"

ButtonLedBuzzerBoard_Soldered board;

const uint8_t colors[][3] = {
{255, 0, 0}, // red
{ 0, 255, 0}, // green
{ 0, 0, 255}, // blue
{255, 255, 0}, // yellow
{ 0, 255, 255}, // cyan
{255, 0, 255}, // magenta
{255, 255, 255}, // white
{ 0, 0, 0}, // off
};
const uint8_t numColors = sizeof(colors) / sizeof(colors[0]);
uint8_t colorIdx = 0;

unsigned long lastColorChange = 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 - Full Demo");
board.setAllLEDs(0, 0, 0);
}

void loop()
{
Serial.print("BTN1:");
Serial.print(board.isButton1Pressed() ? "ON " : "off");
Serial.print(" BTN2:");
Serial.print(board.isButton2Pressed() ? "ON " : "off");
Serial.print(" BTN3:");
Serial.println(board.isButton3Pressed() ? "ON " : "off");

if (millis() - lastColorChange > 2000)
{
uint8_t r = colors[colorIdx][0];
uint8_t g = colors[colorIdx][1];
uint8_t b = colors[colorIdx][2];
board.setAllLEDs(r, g, b);

board.setBuzzer(2000);
delay(100);
board.setBuzzer(0);

colorIdx = (colorIdx + 1) % numColors;
lastColorChange = millis();
}

delay(100);
}

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 the header, a status line appears about every 0.1 s. A pressed button shows ON instead of off, for example the last line below is BTN1 held down.

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Button, LED & Buzzer Board - Full Demo
BTN1:off BTN2:off BTN3:off
BTN1:off BTN2:off BTN3:off
BTN1:ON BTN2:off BTN3:off

At the same time, every 2 s all three LEDs change together through red, green, blue, yellow, cyan, magenta, white and off, and the buzzer beeps at 2000 Hz for 0.1 s with each change.

Functions used​

ButtonLedBuzzerBoard_Soldered(uint8_t address = BLB_DEFAULT_ADDRESS)returns None

Creates 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

TypeNameDescription
uint8_taddressThe board's I2C address, 0x30 to 0x37 as set by the address switch. Defaults to BLB_DEFAULT_ADDRESS (0x30).
begin()returns void

Starts the I2C bus by calling Wire.begin(). Call it once in setup(), before any other function.

ReturnsNothing.

setAllLEDs(uint8_t r, uint8_t g, uint8_t b)returns void

Sets all three LEDs to the same colour and sends them to the board. setAllLEDs(0, 0, 0) turns every LED off.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_trRed brightness, 0 (off) to 255 (full).
uint8_tgGreen brightness, 0 to 255.
uint8_tbBlue brightness, 0 to 255.
isButton1Pressed()returns bool

Reads the buttons and reports whether BTN1 is pressed. Each call reads the board.

Returnstrue while BTN1 is pressed, false otherwise.

isButton2Pressed()returns bool

Reads the buttons and reports whether BTN2 is pressed. Each call reads the board.

Returnstrue while BTN2 is pressed, false otherwise.

isButton3Pressed()returns bool

Reads the buttons and reports whether BTN3 is pressed. Each call reads the board.

Returnstrue while BTN3 is pressed, false otherwise.

setBuzzer(uint16_t freq)returns void

Sends a tone frequency in Hz to the passive buzzer. The examples pass 0 to stop the tone.

ReturnsNothing.

Parameters

TypeNameDescription
uint16_tfreqTone frequency in Hz. The examples pass 0 to stop the tone.

Putting it together​

The sketch checks the time with millis() instead of waiting, so the button lines keep printing while the colours change. The colours live in the colors[] table as red, green and blue values, and colorIdx steps through it. isButton1Pressed(), isButton2Pressed() and isButton3Pressed() each read the board separately, because each one calls readButtons() and keeps one bit. When you need all three at once, one readButtons() call, as on the other pages, returns all three buttons in one byte.