2019-02-27 11:57:45 +08:00
# Button
## read()
**Syntax:**
2020-11-05 11:39:06 +08:00
`uint8_t read()`
2019-02-27 11:57:45 +08:00
**Description:**
This function returns reading the state of the button directly. 1: pressed, 0: released.
2019-02-28 15:56:00 +08:00
**Example:**
2019-02-27 11:57:45 +08:00
2021-02-26 17:27:20 +08:00
```clike
2019-02-27 11:57:45 +08:00
#include <M5Stack.h>
void setup() {
M5.begin();
}
void loop() {
M5.Lcd.setCursor(0, 0);
M5.Lcd.print("Button A Status: ");
M5.Lcd.println(M5.BtnA.read());
}
```
## isPressed()
**Syntax:**
2020-11-05 11:39:06 +08:00
`uint8_t isPressed()`
2019-02-27 11:57:45 +08:00
**Description:**
This function returns the state of the button the last time Button.read() was called. 1: pressed, 0: released.
2019-02-28 15:56:00 +08:00
**Example:**
2019-02-27 11:57:45 +08:00
2021-02-26 17:27:20 +08:00
```clike
2019-02-27 11:57:45 +08:00
#include <M5Stack.h>
void setup() {
M5.begin();
}
void loop() {
M5.update(); // need to call update()
M5.Lcd.setCursor(0, 0);
if (M5.BtnA.isPressed()) {
M5.Lcd.printf("A button is pressed.");
} else {
M5.Lcd.printf("A button is released.");
}
}
```
## wasPressed()
**Syntax:**
2020-11-05 11:39:06 +08:00
`uint8_t wasPressed()`
2019-02-27 11:57:45 +08:00
**Description:**
This function returns 1 only once each time the button is pressed. 1: pressed, 0: released.
2019-02-28 15:56:00 +08:00
**Example:**
2019-02-27 11:57:45 +08:00
2021-02-26 17:27:20 +08:00
```clike
2019-02-27 11:57:45 +08:00
#include <M5Stack.h>
void setup() {
M5.begin();
}
void loop() {
M5.update();
M5.Lcd.clear();
M5.Lcd.setCursor(0, 0);
if (M5.BtnA.wasPressed()) {
M5.Lcd.printf("Button A was pressed.");
delay(1000);
}
}
```
2019-08-15 19:29:40 +08:00
## releasedFor()
**Syntax:**
2020-11-05 11:39:06 +08:00
`uint8_t releasedFor(uint32_t ms)`
2019-08-15 19:29:40 +08:00
**Description:**
releasedFor(ms) check to see if the button is pressed (or released), and has been in that state for the specified time in milliseconds. Returns false (0) or true (1) accordingly.
**Example:**
2021-02-26 17:27:20 +08:00
```clike
2019-08-15 19:29:40 +08:00
#include <M5Stack.h>
void setup() {
M5.begin();
}
void loop() {
M5.update();
M5.Lcd.clear();
M5.Lcd.setCursor(0, 0);
if (M5.BtnA.wasPressed()) {
M5.Lcd.printf("Button A was pressed.");
delay(1000);
}
}
```
2019-02-27 11:57:45 +08:00
## pressedFor()
**Syntax:**
2020-11-05 11:39:06 +08:00
`uint8_t pressedFor(uint32_t ms);`
2019-02-27 11:57:45 +08:00
**Description:**
This function returns 1 if button has been pressed for more than specified time. 1: pressed, 0: released.
| argument | description | type |
| --- | --- | -- |
| ms | pressing time (ms) | uint32_t |
2019-02-28 15:56:00 +08:00
**Example:**
2019-02-27 11:57:45 +08:00
2021-02-26 17:27:20 +08:00
```clike
2019-02-27 11:57:45 +08:00
#include <M5Stack.h>
void setup() {
M5.begin();
}
void loop() {
M5.update();
M5.Lcd.clear();
M5.Lcd.setCursor(0, 0);
if (M5.BtnA.pressedFor(2000)) {
M5.Lcd.printf("Button A was pressed for more than 2 seconds.");
delay(1000);
}
}
```
2019-08-15 19:29:40 +08:00
## lastChange()
**Syntax:**
2020-11-05 11:39:06 +08:00
`uint32_t lastChange(void);`
2019-08-15 19:29:40 +08:00
**Description:**
Returns the last state time point .
**Example:**
2021-02-26 17:27:20 +08:00
```clike
2019-08-15 19:29:40 +08:00
#include <M5Stack.h>
void setup() {
M5.begin();
}
void loop() {
M5.update();
int current_time = M5.BtnB.lastChange();
M5.Lcd.setCursor(0, 0);
M5.Lcd.printf("The last change at No. %d ms",current_time);
}
```
## wasReleasefor()
**Syntax:**
2020-11-05 11:39:06 +08:00
`uint8_t wasReleasefor(uint32_t ms);`
2019-08-15 19:29:40 +08:00
**Description:**
releasedFor(ms) check to see if the button is released, and has been in that state for the specifiedtime in milliseconds. Returns false (0) or true (1) accordingly .
**Example:**
2021-02-26 17:27:20 +08:00
```clike
2019-08-15 19:29:40 +08:00
#include <M5Stack.h>
void setup() {
M5.begin();
M5.Lcd.setCursor(0, 0);
M5.Lcd.print("press Button A to display message");
delay(3000);
}
void loop() {
M5.update();
if (M5.BtnA.isPressed()){
M5.Lcd.setCursor(0, 0);
M5.Lcd.print("release Button A, 3 seconds will clear screen");
}
2019-08-16 16:31:00 +08:00
if (M5.BtnA.wasReleasefor(3000)) {
2019-08-15 19:29:40 +08:00
M5.Lcd.clear(BLACK);
}
}
```
## wasReleased()
**Syntax:**
2020-11-05 11:39:06 +08:00
`uint8_t wasReleased(void)`
2019-08-15 19:29:40 +08:00
**Description:**
This function returns 1 only once each time the button is pressed. 1: pressed, 0: released.
**Example:**
2021-02-26 17:27:20 +08:00
```clike
2019-08-15 19:29:40 +08:00
#include <M5Stack.h>
void setup() {
M5.begin();
M5.Lcd.print("Press Button A and release, you will see the message");
}
void loop() {
M5.update();
if (M5.BtnA.wasReleased()){
M5.Lcd.clear();
M5.Lcd.setCursor(0, 0);
M5.Lcd.print("Button A was Released");
}
}
```