Button

Status

read()

Function:

Read button state: 0, release; 1, press

Syntax:

uint8_t read()

Example:

#include <M5Atom.h>

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

void loop() {
  Serial.println(M5.Btn.read());    // Open the serial port monitor on the Arduino and set the baud rate to 115200 to see the read button state
    delay(20);
}

lastChange()

Syntax:

uint32_t lastChange()

Function:

Returns the time of the last state change

Pay attention:
1.The time returned is measured in milliseconds from the moment Atom was initialized

Example:

#include <M5Atom.h>

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

void loop() {
  M5.update();
  Serial.printf("The last change at %d ms /n",M5.Btn.lastChange());    //串口输出按键状态最后一次发生变化的时间
}

Press

isPressed()

Function:

Returns true if the key is down; Otherwise return false

Syntax:

uint8_t isPressed()

Example:

#include <M5Atom.h>

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

void loop() {
  M5.update();    //You need to add m5.update () to read the state of the key, see System for details
  if (M5.Btn.isPressed()) {    //If the button is pressed
    Serial.println("Button is pressed.");
  }
  delay(20);
}

pressedFor()

Function:

Returns true if the key has been held down for longer than the specified time. Otherwise return false

Syntax:

uint8_t pressedFor(uint32_t ms)

Parameter Type Description
ms uint32_t Key press time (ms)

Example:

#include <M5Atom.h>

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

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

wasPressed()

Function:

Return key down state: true only once if the key is down, false otherwise

Syntax:

uint8_t wasPressed()

Example:

#include <M5Atom.h>

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

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

Released

isReleased()

Function:

Returns the key release status: true if the key is released; Otherwise return false

Syntax:

uint8_t isPressed()

Example:

#include <M5Atom.h>

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

void loop() {
  M5.update();    //You need to add m5.update () to read the state of the key, see System for details
  if (M5.Btn.isReleased()) {    //If the key is released
    Serial.println("Button is released.");
  }
  delay(20);
}

releasedFor()

Function:

Returns the key release status: true if the key release has lasted longer than the specified time; Otherwise return false

Syntax:

uint8_t pressedFor(uint32_t ms)

Parameter Type Description
ms uint32_t Key release time (ms)

Example:

#include <M5Atom.h>

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

void loop() {
  M5.update();
  if (M5.Btn.releasedFor(2000)) {    //If the key release exceeds 2 seconds
    Serial.println("Button A was released for more than 2 seconds.");
    delay(1000);
  }
} 

wasReleased()

Function:

Return key release status: true once if the key is released, false otherwise

Syntax:

uint8_t wasReleased()

Example:

#include <M5Atom.h>

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

void loop() {
  M5.update();
  if (M5.Btn.wasReleased()) {    // If the key is released
    Serial.println("Button is Released.");
  }
  delay(20);
}

wasReleasefor()

Syntax:

uint8_t wasReleasefor(uint32_t ms)

Function:

Return key release status: true only once if the key is held down and released after the specified time, false otherwise

Parameter Type Description
ms uint32_t Key press time (ms)

Example:

#include <M5Atom.h>

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

void loop() {
  M5.update();
  if (M5.Btn.wasReleasefor(3000)) {    //If the key is pressed for 3s after release
      Serial.println("clear screen");
  }
}
On This Page