pdf-icon

Button

read

Syntax:

uint8_t read();

Description:

  • Read Button status

Parameters:

  • null

Return:

  • uint8_t:
    • 1:Button Press
    • 0:Button Release

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());
}

lastChange

Syntax:

uint32_t lastChange();

Description:

  • Get the time of the last status update

Parameters:

  • null

Return:

  • uint32_t: Time of last status update ms

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());
}

isPressed

Syntax:

uint8_t isPressed();

Description:

  • Determine if a Button is pressed

Parameters:

  • null

Return:

  • uint8_t:
    • 1: Button is pressed
    • 0: Button is not pressed

Example:

#include <M5Stack.h>

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

wasPressed

Syntax:

uint8_t wasPressed();

Description:

  • Determine if a Button has changed from released to pressed.

Parameters:

  • null

Return:

  • uint8_t:
    • 1: Trigger Button to change from released to pressed state
    • 0: Untriggered Button changes from released state to pressed state

Example:

#include <M5Stack.h>

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

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

pressedFor

Syntax:

uint8_t pressedFor(uint32_t ms);

Description:

  • Determine if a Button has been pressed for longer than the specified timeout period.

Parameters:

  • uint32_t ms:
    • Button press timeout ms

Return:

  • uint8_t:
    • 1: Trigger Button release beyond timeout
    • 0: non-triggered

Example:

#include <M5Stack.h>

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

isReleased

Syntax:

uint8_t isPressed();

Description:

  • Determine if a Button is in the released state

Parameters:

  • null

Return:

  • uint8_t:
    • 1: Button is released.
    • 0: Button are not released.

Example:

#include <M5Stack.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

Syntax:

uint8_t pressedFor(uint32_t ms);

Description:

  • Determine Button release timeout

Parameters:

  • uint32_t ms:
    • Button release timeout ms

Return:

  • uint8_t:
    • 1: Trigger Button release beyond timeout
    • 0: non-triggered

Example:

#include <M5Stack.h>

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

void loop() {
  M5.update();
  M5.Lcd.setCursor(0, 0);
  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

Syntax:

uint8_t wasReleased();

Description:

  • Determine if a Button is pressed and released.

Parameters:

  • null

Return:

  • uint8_t:
    • 1: Trigger button changes from pressed state to released state
    • 0: Untriggered Button changes from pressed to released state

Example:

#include <M5Stack.h>

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

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

wasReleasefor

Syntax:

uint8_t wasReleasefor(uint32_t ms);

Description:

  • Determine if a Button has been pressed for more than the specified timeout, and trigger when the Button is released.

Parameters:

  • uint32_t ms:
    • Button release timeout ms

Return:

  • uint8_t:
    • 1: Trigger Button release beyond timeout
    • 0: non-triggered

Example:

#include <M5Stack.h>

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

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