mathertel / OneButton

An Arduino library for using a single button for multiple purpose input.
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
Other
920 stars 230 forks source link

interrupt exception #105

Closed SunZongming closed 2 years ago

SunZongming commented 2 years ago

esp8266 When the delay time is relatively long(like 1000), the interrupt response is abnormal

mathertel commented 2 years ago

Please provide more details on your finding.

SunZongming commented 2 years ago
#include "OneButton.h"
#define PIN_INPUT D3
#define PIN_LED D4

OneButton button(PIN_INPUT, true);

int ledState = LOW;

ICACHE_RAM_ATTR void checkTicks() {
  button.tick(); // just call tick() to check the state.
}

void singleClick() {
  Serial.println("singleClick() detected.");
}

void doubleClick() {
  Serial.println("doubleClick() detected.");

  ledState = !ledState;
  digitalWrite(PIN_LED, ledState);
}

void setup() {
  Serial.begin(115200);
  Serial.println("One Button Example with interrupts.");

  pinMode(PIN_LED, OUTPUT);
  digitalWrite(PIN_LED, ledState);

  attachInterrupt(digitalPinToInterrupt(PIN_INPUT), checkTicks, CHANGE);
  button.attachClick(singleClick);
  button.attachDoubleClick(doubleClick);
}

void loop() {
  button.tick();
  delay(60 * 60 * 1000);
}

delay(60 60 1000); use esp8266, when delay one hour, the singleClick and doubleClick will not execute in real time

mathertel commented 2 years ago

This is by intend:

Don't forget to tick()! In order for OneButton to work correctly, you must call tick() on each button instance within your main loop(). If you're not getting any button events, this is probably why.

The reason for this is that the code executed during an interrupt usually has not the full stack available and on some processors is also restricted in the code that can be executed.

A good practice for interrupt routines is to