Button

read

Functionality:

Read the button state: 0, released; 1, pressed

Function Prototype:

uint8_t read()

Usage Example:

#include <M5Stack.h>

void setup() {
  M5.begin();
  M5.Lcd.println("Please press Button A.");
}

void loop() {
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.printf("Button A Status: %d ", M5.BtnA.read());    // Print the state of Button A being pressed
}

lastChange

Functionality:

Returns the time of the last state change

Function Prototype:

uint32_t lastChange()

Note:
1. The returned time starts from the moment the M5Core is initialized, in milliseconds

Usage Example:

#include <M5Stack.h>

void setup() {
  M5.begin();
  M5.Lcd.println("Please press Button A.");
}

void loop() {
  M5.update();
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.printf("The last change at %d ms /n", M5.BtnA.lastChange());    // Print the time of the last state change for Button A
}

isPressed

Functionality:

Returns the button press state: If the button is pressed, return true; otherwise, return false

Function Prototype:

uint8_t isPressed()

Usage Example:

#include <M5Stack.h>

void setup() {
  M5.begin();
  M5.Lcd.println("Please press Button A.");
}

void loop() {
  M5.update();    // You need to add M5.update() to read the button state, see System for details
  M5.Lcd.setCursor(0, 0);
  if (M5.BtnA.isPressed()) {    // If the button is pressed
    M5.Lcd.println("Button is Pressed.");
  } else {
    M5.Lcd.println("Button is Released.");
  }
  delay(20);
}

wasPressed

Functionality:

Returns the button press state: If the button was pressed, it returns true once; otherwise, it returns false

Function Prototype:

uint8_t wasPressed()

Usage Example:

#include <M5Stack.h>

void setup() {
  M5.begin();
  M5.Lcd.println("Please press Button A.");
}

void loop() {
  M5.update();
  if (M5.BtnA.wasPressed()) {    // If the button was pressed
    M5.Lcd.println("Button is pressed.");
  } 
  delay(20);
}

pressedFor

Functionality:

Returns the button press state: If the button is pressed for a specified time, return true; otherwise, return false

Function Prototype:

uint8_t pressedFor(uint32_t ms)

Parameter Type Description
ms uint32_t Button press duration (milliseconds)

Usage Example:

#include <M5Stack.h>

void setup() {
  M5.begin();
  M5.Lcd.println("Please press Button A.");
}

void loop() {
  M5.update();
  if (M5.BtnA.pressedFor(2000)) {    // If the button is pressed for more than 2 seconds
    M5.Lcd.println("Button A was pressed for more than 2 seconds.");
    delay(1000);
  }
}

isReleased

Functionality:

Returns the button release state: If the button is released, return true; otherwise, return false

Function Prototype:

uint8_t isPressed()

Usage Example:

#include <M5Stack.h>

void setup() {
  M5.begin();
}

void loop() {
  M5.update();    // You need to add M5.update() to read the button state, see System for details
  if (M5.BtnA.isReleased()) {    // If the button is released
    M5.Lcd.println("Button is released.");
  } else {
    M5.Lcd.println("Button is Pressed.");
  }
  delay(20);
}
On This Page