ESP32 – ezButton

Table of Contents

Links

Some testing with ezButton.h

Starter Code Still in Progress

				
					#include <ezButton.h>

#define BUTTON_PIN     9
#define DEBOUNCE_TIME  50

const int SHORT_PRESS_TIME = 1000;
const int LONG_PRESS_TIME  = 1000;
const int MULTI_PRESS_TIME = 500;

unsigned long multiPressTimer = 0;

unsigned long pressedTime  = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;

int multiPressCount = 0;

ezButton button(BUTTON_PIN);

void setup() {
  button.setDebounceTime(DEBOUNCE_TIME);

  button.setCountMode(COUNT_FALLING);

  Serial.begin(115200);
  Serial.println();
}

void loop() {
  button.loop();

  int btnState = button.getState();
  // Serial.println(btnState);

  unsigned long count = button.getCount();
  if (count >= 100) {
    Serial.println(count);
    button.resetCount();
  }

  if (button.isPressed())
    Serial.println("The button is pressed");

  if (button.isReleased())
    Serial.println("The button is released");

  if (button.isPressed()) {
    pressedTime = millis();
    isPressing = true;
    isLongDetected = false;
  }

  if (button.isReleased()) {
    isPressing = false;
    releasedTime = millis();

    long pressDuration = releasedTime - pressedTime;

    if ( pressDuration < SHORT_PRESS_TIME ) {
      Serial.println("A short press is detected");
      ++multiPressCount;
      Serial.println(multiPressCount);
      multiPressTimer = millis();
    }
  }
  unsigned long now = millis();
  if (isPressing == false && now - multiPressTimer > MULTI_PRESS_TIME) {
    multiPressCount = 0;
  }

  //if (isPressing == true && multiPressCount > 0) {
  // Serial.println(multiPressCount);
  //}

  if (isPressing == true && isLongDetected == false) {
    long pressDuration = millis() - pressedTime;

    if ( pressDuration > LONG_PRESS_TIME ) {
      Serial.println("A long press is detected");
      isLongDetected = true;
    }
  }
}
				
			

Leave a Comment