Created
March 27, 2021 20:23
-
-
Save aaronbieber/57c40668c15580e8405d5fe8251c0e25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Encoder.h> | |
#include <Bounce.h> | |
#include <Keyboard.h> | |
const int pinClk = 6; | |
const int pinDt = 7; | |
const int prevButton = 8; | |
const int nextButton = 9; | |
const int pinButton = 10; | |
Bounce bouncer = Bounce(pinButton, 5); | |
Bounce prevButtonBouncer = Bounce(prevButton, 5); | |
Bounce nextButtonBouncer = Bounce(nextButton, 5); | |
Encoder myEnc(pinClk, pinDt); | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Basic Encoder Test:"); | |
pinMode(pinButton, INPUT_PULLUP); | |
pinMode(prevButton, INPUT_PULLUP); | |
pinMode(nextButton, INPUT_PULLUP); | |
} | |
long oldPosition = -999; | |
void loop() { | |
long newPosition = myEnc.read(); | |
Serial.println(newPosition); | |
int diff = newPosition - oldPosition; | |
if (newPosition != oldPosition) { | |
//Serial.println(diff); | |
if (newPosition - oldPosition >= 4) { | |
oldPosition = newPosition; | |
Keyboard.press(KEY_MEDIA_VOLUME_INC); | |
Keyboard.release(KEY_MEDIA_VOLUME_INC); | |
} else if (abs(oldPosition - newPosition) >= 4) { | |
oldPosition = newPosition; | |
Keyboard.press(KEY_MEDIA_VOLUME_DEC); | |
Keyboard.release(KEY_MEDIA_VOLUME_DEC); | |
} | |
} | |
bouncer.update(); | |
if (bouncer.fallingEdge()) {\ | |
Keyboard.press(KEY_MEDIA_PLAY_PAUSE); | |
Keyboard.release(KEY_MEDIA_PLAY_PAUSE); | |
} | |
prevButtonBouncer.update(); | |
nextButtonBouncer.update(); | |
if (prevButtonBouncer.fallingEdge()) { | |
Keyboard.press(KEY_MEDIA_PREV_TRACK); | |
Keyboard.release(KEY_MEDIA_PREV_TRACK); | |
} | |
if (nextButtonBouncer.fallingEdge()) { | |
Keyboard.press(KEY_MEDIA_NEXT_TRACK); | |
Keyboard.release(KEY_MEDIA_NEXT_TRACK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment