Last active
August 29, 2015 14:16
-
-
Save erinzm/d675e630d34a34824165 to your computer and use it in GitHub Desktop.
sdvx controller
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> | |
// -- some #defines to save typing | |
#define pinToKeyWD(pin, key) if (digitalRead(pin)==LOW) { Keyboard.press(key); } else if (digitalRead(pin)==HIGH) { Keyboard.release(key); } | |
#define btn(pin) pinMode(pin, INPUT_PULLUP); | |
#define light(pin) pinMode(pin, OUTPUT); | |
// -- set up the encoders | |
Encoder wheel1(0, 1); | |
Encoder wheel2(2, 3); | |
// -- arrays to hold the encoder positions | |
int encoderPos[] = {0,0}; | |
int oldEncoderPos[] = {0,0}; | |
void setup() { | |
// -- set up the keyboard and mouse | |
Keyboard.begin(); | |
Mouse.begin(); | |
// -- set up the lights | |
light(10); | |
light(16); | |
// -- turn on the lights that are always on | |
digitalWrite(10, HIGH); | |
digitalWrite(16, HIGH); | |
btn(4); | |
btn(5); | |
btn(6); | |
btn(7); | |
btn(8); | |
btn(9); | |
btn(A0); | |
} | |
void loop() { | |
// -- "bind" all the used button pins to keys | |
pinToKeyWD(4, 'a'); | |
pinToKeyWD(5, 'b'); | |
pinToKeyWD(6, 'c'); | |
pinToKeyWD(7, 'd'); | |
pinToKeyWD(8, 'i'); | |
pinToKeyWD(9, 'f'); | |
pinToKeyWD(A0, 'g'); | |
// -- encoders | |
encoderPos[0] = wheel1.read(); | |
encoderPos[1] = wheel2.read(); | |
for (int i = 0; i <= 1; i++) { | |
if (i == 0) { | |
// move the mouse the difference between the previous and current | |
// positions of the encoder, on the X axis | |
Mouse.move(-(oldEncoderPos[i] - encoderPos[i]), 0, 0); | |
} else if (i == 1) { | |
// same thing but on the Y axis. | |
Mouse.move(0, oldEncoderPos[i] - encoderPos[i], 0); | |
} | |
// the "current" value is now old | |
oldEncoderPos[i] = encoderPos[i]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment