pdf-icon

Buttons

Class Name: BtnA / BtnB / BtnC

read()

Function:

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

Syntax:

uint8_t read()

Example:

#include <M5Core2.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());
}

lastChange()

Function:

Returns the time of the last status change.

Syntax:

uint32_t lastChange()

Note:
1. The returned time is measured from the initialization of the M5Core, in milliseconds.

Example:

#include <M5Core2.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());
}

Press

isPressed()

Function:

Returns the button press state: true if the button is pressed, false otherwise.

Syntax:

uint8_t isPressed()

Example:

#include <M5Core2.h>

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

void loop() {
  M5.update();
  M5.Lcd.setCursor(0, 0);
  if (M5.BtnA.isPressed()) {
    M5.Lcd.println("Button is Pressed.");
  }else{
    M5.Lcd.println("Button is Released.");
  }
  delay(20);
}

pressedFor()

Function:

Returns true if the button has been pressed for a specified duration, otherwise returns false.

Syntax:

uint8_t pressedFor(uint32_t ms)

Parameter Type Description
ms uint32_t Press duration (ms)

Example:

#include <M5Core2.h>

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

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

wasPressed()

Function:

Returns true if the button was pressed, but only once.

Syntax:

uint8_t wasPressed()

Example:

#include <M5Core2.h>

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

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

Released

isReleased()

Function:

Returns true if the button is released, otherwise returns false.

Syntax:

uint8_t isPressed()

Example:

#include <M5Core2.h>

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

void loop() {
  M5.update();
  if (M5.BtnA.isReleased()) {
    M5.Lcd.println("Button is released.");
  }else{
    M5.Lcd.println("Button is Pressed.");
  }
  delay(20);
}

releasedFor()

Function:

Returns true if the button has been released for a specified duration, otherwise returns false.

Syntax:

uint8_t pressedFor(uint32_t ms)

Parameter Type Description
ms uint32_t Release duration (ms)

Example:

#include <M5Core2.h>

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

void loop() {
  M5.update();
  if (M5.BtnA.releasedFor(2000)) {
    M5.Lcd.println("Button A was released for more than 2 seconds.");
    delay(1000);
  }else{
    M5.Lcd.println("Button A is pressed");
  }
}

wasReleased()

Function:

Returns true if the button was released, but only once.

Syntax:

uint8_t wasReleased()

Example:

#include <M5Core2.h>

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

void loop() {
  M5.update();
  if(M5.BtnA.wasReleased()) {
    M5.Lcd.println("Button is Released.");
  }
  delay(20);
}

wasReleasefor()

Function:

Returns true if the button was pressed and then released after a specified duration, but only once.

Syntax:

uint8_t wasReleasefor(uint32_t ms)

Parameter Type Description
ms uint32_t Press duration (ms)

Example:

#include <M5Core2.h>

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

void loop() {
  M5.update();
  if (M5.BtnA.wasReleasefor(3000)) {
      M5.Lcd.println("OK");
  }
}
On This Page