English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

StickC Button

APIs and example programs related to button input on StickC.

Note
Include the M5.update() function in the main loop to update button states, and minimize blocking operations; otherwise, button changes may not be detected promptly.

Example Program

Build Requirements

  • M5Stack board package version >= 3.3.8
  • Board selection = M5StickC
  • M5Unified library version >= 0.2.18
  • M5GFX library version >= 0.2.24
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <M5Unified.h>

constexpr uint8_t TEXT_SIZE = 2;

void showMessage(const char* message) {
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextSize(TEXT_SIZE);
    M5.Display.setTextColor(TFT_YELLOW, TFT_BLACK);

    int16_t textWidth = M5.Display.textWidth(message);
    int16_t textHeight = M5.Display.fontHeight();
    int16_t x = (M5.Display.width() - textWidth) / 2;
    int16_t y = (M5.Display.height() - textHeight) / 2;

    M5.Display.setCursor(x, y);
    M5.Display.print(message);
}

void setup() {
    auto cfg = M5.config();
    M5.begin(cfg);

    M5.Display.setRotation(1);
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextColor(TFT_YELLOW, TFT_BLACK);
    M5.Display.setTextSize(TEXT_SIZE);
}

void loop() {
    M5.update();

    if (M5.BtnA.wasReleased()) {
        showMessage("A Btn Pressed");
    }
    if (M5.BtnB.wasReleased()) {
        showMessage("B Btn Pressed");
    }
}

After button A or B is pressed and released, the program displays the corresponding button state in the center of the screen.

Page Tools
PDF
On This Page