Button

Status

read()

Description:

Read button status: 0, release; 1, press.

Syntax:

uint8_t read()

Example:

#include <M5Stack.h>

void setup() {
  M5.begin();
  M5.Lcd.println("Please pressed 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 pressed. 
}

lastChange()

Description:

Returns the time when the state was last changed.

Syntax:

uint32_t lastChange()

Pay Attention:
1.The time returned is timed from the moment M5Core is initialized, in milliseconds.

Example:

#include <M5Stack.h>

void setup() {
  M5.begin();
  M5.Lcd.println("Please pressed 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 of button A
}

Press

isPressed()

Description:

Return to the keystroke state: if the key is pressed, return to true; otherwise return to false.

Syntax:

uint8_t isPressed()

Example:

#include <M5Stack.h>

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

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

pressedFor()

Description:

Return to the keystroke state: return to true; if the keystroke has been pressed for more than a specified time, otherwise return false.

Syntax:

uint8_t pressedFor(uint32_t ms)

Parameters Types Description
ms uint32_t Press time (milliseconds)

Example:

#include <M5Stack.h>

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

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

wasPressed()

Description:

Return to keystroke state: if the key is pressed, true will be returned only once, otherwise false will be returned.

Syntax:

uint8_t wasPressed()

Example:

#include <M5Stack.h>

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

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

Released

isReleased()

Description:

Return to the key release status: if you press the key to release, return true; otherwise return false.

Syntax:

uint8_t isPressed()

Example:

#include <M5Stack.h>

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

void loop() {
  M5.update();    //You need to add M5.update () to read the status of the keystroke. For details, please see System. 
  if (M5.BtnA.isReleased()) {    //If you press the key to release
    M5.Lcd.println("Button is released.");
  }else{
    M5.Lcd.println("Button is Pressed .");
  }
  delay(20);
}

releasedFor()

Description:

Return to keystroke release status: return true; if the key is released for more than a specified time, otherwise return false.

Syntax:

uint8_t pressedFor(uint32_t ms)

Parameters Types Description
ms uint32_t Key release time (milliseconds).

Example:

#include <M5Stack.h>

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

void loop() {
  M5.update();
  M5.Lcd.setCursor(0, 0);
  if (M5.BtnA.releasedFor(2000)) {    //If you press the key to release for more than 2 seconds. 
    M5.Lcd.println("Button A was released for more than 2 seconds.");
    delay(1000);
  }else{
    M5.Lcd.println("Button A is pressed                           ");
  }
}

wasReleased()

Description:

Return to keystroke release status: if you press the key to release, true will be returned only once, otherwise false will be returned.

Syntax:

uint8_t wasReleased()

Example:

#include <M5Stack.h>

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

void loop() {
  M5.update();
  if(M5.BtnA.wasReleased()) {    //If you press the key to release.
    M5.Lcd.println("Button is Released.");
  }
  delay(20);
}

wasReleasefor()

Description:

Return to keystroke release status: if the key is pressed and released after the specified time, true will be returned only once, otherwise false will be returned.

Syntax:

uint8_t wasReleasefor(uint32_t ms)

Parameters Types Description
ms uint32_t Press time (milliseconds).

Example:

#include <M5Stack.h>

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

void loop() {
  M5.update();
  if (M5.BtnA.wasReleasefor(3000)) {    //If button An is pressed for 3s and then released
      M5.Lcd.println("OK");
  }
}
On This Page