Arduino-Scheduler
Portable Collaborative Multi-Tasking Scheduler for Arduino
Debounce.h
Go to the documentation of this file.
1 
22 #ifndef DEBOUNCE_H
23 #define DEBOUNCE_H
24 
25 template<int PIN, void (*CALLBACK)()>
26 class Debounce {
27 public:
28  static void setup()
29  {
30  pinMode(PIN, INPUT_PULLUP);
31  }
32  static void loop()
33  {
34  const unsigned int DEBOUNCE = 50;
35  int state = digitalRead(PIN);
36  while (1) {
37  await(digitalRead(PIN) != state);
38  unsigned int start = millis();
39  if (state) CALLBACK();
40  unsigned int ms = millis() - start;
41  if (ms < DEBOUNCE) delay(DEBOUNCE - ms);
42  state = !state;
43  }
44  }
45 };
46 
47 #endif
#define await(cond)
Definition: Scheduler.h:162
volatile int state
static void loop()
Definition: Debounce.h:32
static void setup()
Definition: Debounce.h:28