
Arduino Quick Start
APIs and example programs related to Chain DualKey Button.
#include "M5Unified.h"
#define pin_Key1 0
#define pin_Key2 17
m5::Button_Class Key1;
m5::Button_Class Key2;
void setup() {
pinMode(pin_Key1, INPUT);
pinMode(pin_Key2, INPUT);
Serial.begin(115200);
}
void loop() {
uint32_t ms = millis();
Key1.setRawState(ms, !digitalRead(pin_Key1));
Key2.setRawState(ms, !digitalRead(pin_Key2));
if (Key1.wasPressed()) {
Serial.println("Key1 was pressed");
}
if (Key1.wasReleased()) {
Serial.println("Key1 was released");
}
if (Key1.wasSingleClicked()) {
Serial.println("Key1 was single clicked");
}
if (Key1.wasDoubleClicked()) {
Serial.println("Key1 was double clicked");
}
if (Key1.wasHold()) {
Serial.println("Key1 was held");
}
if (Key1.wasReleaseFor(5000)) { // ms
Serial.println("Key1 was released after being held for 5000 ms");
}
if (Key2.wasPressed()) {
Serial.println("Key2 was pressed");
}
if (Key2.wasReleased()) {
Serial.println("Key2 was released");
}
if (Key2.wasSingleClicked()) {
Serial.println("Key2 was single clicked");
}
if (Key2.wasDoubleClicked()) {
Serial.println("Key2 was double clicked");
}
if (Key2.wasHold()) {
Serial.println("Key2 was held");
}
if (Key2.wasReleaseFor(5000)) { // ms
Serial.println("Key2 was released after being held for 5000 ms");
}
delay(10);
}The program detects the states of the two buttons on the device (the one farther from the lanyard hole is Key1, and the one closer to the lanyard hole is Key2) — including pressed, released, single short press, double short press, long press, and released after a 5000 ms long press — and prints messages to the Serial Monitor.
The button driver of Chain DualKey uses Button_Class from the M5Unified library. For more APIs, see: