
Arduino Quick Start
NanoH2 Button sample program.
#define pin_BtnA 9
#define DEBOUNCE_MS 20 // Debounce time
#define HOLD_MS 500 // Holding threshold
bool last_state = HIGH;
unsigned long last_time = 0;
bool is_holding = false;
void setup() {
pinMode(pin_BtnA, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
bool current_state = digitalRead(pin_BtnA);
unsigned long now = millis();
// Debouncing
if (current_state != last_state && now - last_time > DEBOUNCE_MS) {
last_state = current_state;
last_time = now;
if (current_state == LOW) { // Button pressed
Serial.println("Button A pressed");
} else { // Button released
Serial.println("Button A released");
if (!is_holding) {
Serial.println("Button A single clicked");
}
is_holding = false;
}
}
// Long press detection
if (current_state == LOW && now - last_time > HOLD_MS && !is_holding) {
is_holding = true;
Serial.println("Button A held");
}
delay(5);
}This program detects the state of the front input button on the NanoH2 and prints button events to the Serial Monitor:
