|
#include <AceButton.h> |
|
using namespace ace_button; |
|
|
|
const int BUTTON_PIN = 10; |
|
const int GREEN_LED_PIN = 11; |
|
// LED states. Some microcontrollers wire their built-in LED the reverse. |
|
const int LED_ON = HIGH; |
|
const int LED_OFF = LOW; |
|
|
|
AceButton button(BUTTON_PIN); |
|
|
|
void setup() { |
|
delay(1000); // some microcontrollers reboot twice |
|
Serial.begin(115200); |
|
while (! Serial); // Wait until Serial is ready - Leonardo/Micro |
|
Serial.println(F("setup(): begin")); |
|
|
|
// initialize built-in LED as an output |
|
pinMode(LED_BUILTIN, OUTPUT); |
|
pinMode( GREEN_LED_PIN, OUTPUT); |
|
// Button uses the built-in pull up register. |
|
pinMode(BUTTON_PIN, INPUT_PULLUP); |
|
|
|
// Configure the ButtonConfig with the event handler, and enable all higher |
|
// level events. |
|
ButtonConfig* buttonConfig = button.getButtonConfig(); |
|
buttonConfig->setEventHandler(handleEvent); |
|
buttonConfig->setFeature(ButtonConfig::kFeatureClick); |
|
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick); |
|
buttonConfig->setFeature(ButtonConfig::kFeatureLongPress); |
|
buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress); |
|
|
|
|
|
#if ENABLE_SERIAL == 1 |
|
Serial.println(F("setup(): ready")); |
|
#endif |
|
} |
|
|
|
void loop() { |
|
// Should be called every 20ms or faster for the default debouncing time |
|
// of ~50ms. |
|
button.check(); |
|
} |
|
|
|
void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) { |
|
Serial.print(F("handleEvent(): eventType: ")); |
|
Serial.print(eventType); |
|
Serial.print(F("; button: ")); |
|
Serial.println(buttonState); |
|
|
|
if (eventType == 5) { |
|
digitalWrite(GREEN_LED_PIN, LED_ON); |
|
} else { |
|
digitalWrite(GREEN_LED_PIN, LED_OFF); |
|
} |
|
|
|
|
|
switch (eventType) { |
|
case AceButton::kEventPressed: |
|
digitalWrite(LED_BUILTIN, LED_ON); |
|
break; |
|
case AceButton::kEventReleased: |
|
digitalWrite(LED_BUILTIN, LED_OFF); |
|
break; |
|
} |
|
} |