The Button2 sketch didn't get enough output

Hi everyone.
The example sketch from button2.h

/////////////////////////////////////////////////////////////////

#include "Button2.h"

/////////////////////////////////////////////////////////////////

#define BUTTON_PIN  2

/////////////////////////////////////////////////////////////////

Button2 button;

/////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);
  delay(50);
  Serial.println("\n\nButton Demo");

  button.begin(BUTTON_PIN);
  // button.setLongClickTime(1000);
  // button.setDoubleClickTime(400);

  Serial.println(" Longpress Time:\t" + String(button.getLongClickTime()) + "ms");
  Serial.println(" DoubleClick Time:\t" + String(button.getDoubleClickTime()) + "ms");
  Serial.println();

  // button.setChangedHandler(changed);
  // button.setPressedHandler(pressed);
  button.setReleasedHandler(released);

  // button.setTapHandler(tap);
  button.setClickHandler(click);
  button.setLongClickDetectedHandler(longClickDetected);
  button.setLongClickHandler(longClick);
  
  button.setDoubleClickHandler(doubleClick);
  button.setTripleClickHandler(tripleClick);
}

/////////////////////////////////////////////////////////////////

void loop() {
  button.loop();
}

/////////////////////////////////////////////////////////////////

void pressed(Button2& btn) {
    Serial.println("pressed");
}
void released(Button2& btn) {
    Serial.print("released: ");
    Serial.println(btn.wasPressedFor());
}
void changed(Button2& btn) {
    Serial.println("changed");
}
void click(Button2& btn) {
    Serial.println("click\n");
}
void longClickDetected(Button2& btn) {
    Serial.println("long click detected");
}
void longClick(Button2& btn) {
    Serial.println("long click\n");
}
void doubleClick(Button2& btn) {
    Serial.println("double click\n");
}
void tripleClick(Button2& btn) {
    Serial.println("triple click\n");
}
void tap(Button2& btn) {
    Serial.println("tap");
}

Serial Monitor:

long click detected
released: 1024
long click

it is the output when pressed button once, should it output all void result such as and why didn't:
pressed
released = 1024
changed
click/1
etc....

thanks
Adam

You didn't register a pressed or changed handler so why should it print anything on these events?

1 Like

Thanks.
I guess it is the point I like to make clear:

void pressed(Button2& btn) {
    Serial.println("pressed");
}
void released(Button2& btn) {
    Serial.print("released: ");
    Serial.println(btn.wasPressedFor());
}

What's the different above two voids, why the first one doesn't print, and second one does.

You defined the routines but you didn't register the pressed and changed event callbacks!

1 Like

YES YES .
Thank you.
Removed the commented out, OK now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.