pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Chain DualKey Button

APIs and example programs related to Chain DualKey Button.

Example Program

Build Requirements

  • M5Stack Board Manager version ≥ 3.2.4
  • Board option = M5ChainDualKey
  • M5Unified library version ≥ 0.2.11
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#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.

API

The button driver of Chain DualKey uses Button_Class from the M5Unified library. For more APIs, see:

On This Page