4-9.Button Long Press/Short Press Debounce/ long press and Release on Proteus Simulation Arduino Uno

Поделиться
HTML-код
  • Опубликовано: 21 окт 2024

Комментарии • 2

  • @WiwiwiWiwi-zg1do
    @WiwiwiWiwi-zg1do 25 дней назад +1

    ❤❤❤❤❤ 0:21

  • @LED_Electronic
    @LED_Electronic  26 дней назад +1

    #include
    const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
    const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
    ezButton button(7); // create ezButton object that attach to pin 7;
    unsigned long pressedTime = 0;
    unsigned long releasedTime = 0;
    bool isPressing = false;
    bool isLongDetected = false;
    void setup() {
    Serial.begin(9600);
    button.setDebounceTime(50); // set debounce time to 50 milliseconds
    }
    void loop() {
    button.loop(); // MUST call the loop() function first
    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("short press is detected");// press < long is short(1000)
    }
    if(isPressing == true && isLongDetected == false) {
    long pressDuration = millis() - pressedTime;
    if( pressDuration > LONG_PRESS_TIME ) {
    Serial.println("long press is detected");//press > short is long(1000)
    isLongDetected = true;
    }
    }
    }