pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Atom Button

read

Description:

Reads the button state: 0, released; 1, pressed

Syntax:

uint8_t read();

Example:

cpp
1 2 3 4 5 6 7 8 9 10
#include <M5Atom.h> void setup() { M5.begin(); } void loop() { Serial.println(M5.Btn.read()); //Open the serial monitor in Arduino, set the baud rate to 115200 to see the read button state delay(20); }

lastChange

Syntax:

uint32_t lastChange();

Description:

Returns the time of the last state change

Note:
1. The returned time starts counting from the moment the Atom is initialized, in milliseconds

Example:

cpp
1 2 3 4 5 6 7 8 9 10
#include <M5Atom.h> void setup() { M5.begin(); } void loop() { M5.update(); Serial.printf("The last change at %d ms /n",M5.Btn.lastChange()); //Serial output of the last time the button state changed }

isPressed

Description:

Returns the button pressed state: returns true if the button is pressed; otherwise, returns false

Syntax:

uint8_t isPressed();

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <M5Atom.h> void setup() { M5.begin(); } void loop() { M5.update(); //Add M5.update() to read the button state, details please see System if (M5.Btn.isPressed()) { //If the button is pressed Serial.println("Button is pressed."); } delay(20); }

wasPressed

Description:

Returns the button pressed state: if the button is pressed, it returns true only once; otherwise, it returns false

Syntax:

uint8_t wasPressed();

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#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); }

pressedFor

Description:

Returns the button pressed state: returns true if the button is pressed for more than the specified time; otherwise, returns false

Syntax:

uint8_t pressedFor(uint32_t ms);
Parameter Type Description
ms uint32_t Button pressed time (milliseconds)

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#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); } }

isReleased

Description:

Returns the button release state: returns true if the button is released; otherwise, returns false

Syntax:

uint8_t isPressed();

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <M5Atom.h> void setup() { M5.begin(); } void loop() { M5.update(); //Add M5.update() to read the button state, details please see System if (M5.Btn.isReleased()) { //If the button is released Serial.println("Button is released."); } delay(20); }

releasedFor

Description:

Returns the button release state: returns true if the button is released for more than the specified time; otherwise, returns false

Syntax:

uint8_t releasedFor(uint32_t ms);
Parameter Type Description
ms uint32_t Button release time (milliseconds)

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <M5Atom.h> void setup() { M5.begin(); } void loop() { M5.update(); if (M5.Btn.releasedFor(2000)) { //If the button is released for more than 2 seconds Serial.println("Button A was released for more than 2 seconds."); delay(1000); } }

wasReleased

Description:

Returns the button release state: if the button is released, it returns true only once; otherwise, it returns false

Syntax:

uint8_t wasReleased();

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <M5Atom.h> void setup() { M5.begin(); } void loop() { M5.update(); if (M5.Btn.wasReleased()) { //If the button is released Serial.println("Button is Released."); } delay(20); }

wasReleasefor

Syntax:

uint8_t wasReleasefor(uint32_t ms);

Description:

Returns the button release state: if the button is pressed and then released after more than the specified time, it returns true only once; otherwise, returns false

Parameter Type Description
ms uint32_t Button pressed time (milliseconds)

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12
#include <M5Atom.h> void setup() { M5.begin(); } void loop() { M5.update(); if (M5.Btn.wasReleasefor(3000)) { //If the button is pressed for 3s and then released Serial.println("clear screen"); } }