Skip to content

Button

read()

Syntax:

1
uint8_t read();

Description:

This function returns reading the state of the button directly. 1: pressed, 0: released.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <M5Stack.h>

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

void loop() {
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.print("Button A Status: ");
  M5.Lcd.println(M5.BtnA.read());
}

isPressed()

Syntax:

1
uint8_t isPressed();

Description:

This function returns the state of the button the last time Button.read() was called. 1: pressed, 0: released.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <M5Stack.h>

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

void loop() {
  M5.update(); // need to call update()
  M5.Lcd.setCursor(0, 0);
  if (M5.BtnA.isPressed()) {
    M5.Lcd.printf("A button is pressed.");
  } else {
    M5.Lcd.printf("A button is released.");
  }
}

wasPressed()

Syntax:

1
uint8_t wasPressed();

Description:

This function returns 1 only once each time the button is pressed. 1: pressed, 0: released.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <M5Stack.h>

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

void loop() {
  M5.update();
  M5.Lcd.clear();
  M5.Lcd.setCursor(0, 0);
  if (M5.BtnA.wasPressed()) {
    M5.Lcd.printf("Button A was pressed.");
    delay(1000);
  }
}

pressedFor()

Syntax:

1
uint8_t pressedFor(uint32_t ms);

Description:

This function returns 1 if button has been pressed for more than specified time. 1: pressed, 0: released.

argument description type
ms pressing time (ms) uint32_t

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <M5Stack.h>

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

void loop() {
  M5.update();
  M5.Lcd.clear();
  M5.Lcd.setCursor(0, 0);
  if (M5.BtnA.pressedFor(2000)) {
    M5.Lcd.printf("Button A was pressed for more than 2 seconds.");
    delay(1000);
  }
}