Skip to content

Instantly share code, notes, and snippets.

@yakutozcan
Created June 4, 2018 19:46
Show Gist options
  • Save yakutozcan/8ace906ee09d58467696311158e3b6f8 to your computer and use it in GitHub Desktop.
Save yakutozcan/8ace906ee09d58467696311158e3b6f8 to your computer and use it in GitHub Desktop.
#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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment