pdf-icon

Arduino 上手教程

Atom Button 按键

read

功能说明:

读取按键状态: 0,松开; 1,按下

函数原型:

uint8_t read();

案例程序:

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()); //在Arduino打开串口监视器,波特率设置115200即可看到读取到的按键状态
delay(20);
}

lastChange

函数原型:

uint32_t lastChange();

功能说明:

返回最后一次状态发生变化的时间

注意:
1.返回的时间是从 Atom初始化的那一刻开始计时,单位为毫秒

案例程序:

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()); //串口输出按键状态最后一次发生变化的时间
}

isPressed

功能说明:

返回按键按下状态: 如果按键按下,返回 true; 否则返回 false

函数原型:

uint8_t isPressed();

案例程序:

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(); //需添加 M5.update()才能读取到按键的状态,细节请见 System
if (M5.Btn.isPressed()) { //如果按键按下
Serial.println("Button is pressed.");
}
delay(20);
}

wasPressed

功能说明:

返回按键按下状态: 如果按键按下,只会返回一次 true,否则返回 false

函数原型:

uint8_t wasPressed();

案例程序:

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()) { //如果按键按下
Serial.println("Button is pressed.");
}
delay(20);
}

pressedFor

功能说明:

返回按键按下状态: 如果按键按下超过指定时间后,返回 true; 否则返回 false

函数原型:

uint8_t pressedFor(uint32_t ms);
参数 类型 描述
ms uint32_t 按键按下时间 (毫秒)

案例程序:

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)) { //如果按键按下超过2秒
Serial.println("Button A was pressed for more than 2 seconds.");
delay(1000);
}
}

isReleased

功能说明:

返回按键释放状态: 如果按键释放,返回 true; 否则返回 false

函数原型:

uint8_t isPressed();

案例程序:

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(); //需添加M5.update()才能读取到按键的状态,细节请见System
if (M5.Btn.isReleased()) { //如果按键释放
Serial.println("Button is released.");
}
delay(20);
}

releasedFor

功能说明:

返回按键释放状态: 如果按键释放超过指定时间后,返回 true; 否则返回 false

函数原型:

uint8_t releasedFor(uint32_t ms);
参数 类型 描述
ms uint32_t 按键释放时间 (毫秒)

案例程序:

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.releasedFor(2000)) { //如果按键释放超过2秒
Serial.println("Button A was released for more than 2 seconds.");
delay(1000);
}
}

wasReleased

功能说明:

返回按键释放状态: 如果按键释放,只会返回一次 true,否则返回 false

函数原型:

uint8_t wasReleased();

案例程序:

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()) { //如果按键释放
Serial.println("Button is Released.");
}
delay(20);
}

wasReleasefor

函数原型:

uint8_t wasReleasefor(uint32_t ms);

功能说明:

返回按键释放状态: 如果按键按下,在超过指定时间后释放,只会返回一次 true,否则返回 false

参数 类型 描述
ms uint32_t 按键按下时间 (毫秒)

案例程序:

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)) { //如果按键按下3s之后释放
Serial.println("clear screen");
}
}
On This Page