Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keyboard.h>
- #include <Mouse.h>
- #define DELAY 5 // Delay per loop in ms
- #define HITBTN 700
- enum PinAssignments
- {
- encoderPinA = 0,
- encoderPinB = 1,
- encoderPinC = 3,
- encoderPinD = 2,
- buttonWhiteL = A1,
- buttonWhiteR = A0,
- buttonYellowL = A3,
- buttonYellowR = A2,
- buttonGreenL = A5,
- buttonGreenR = A4,
- buttonBlueL = 8,
- buttonBlueR = 9,
- buttonRed = 10,
- };
- //This is up to your pin wiring
- int encoderPos[] = {0, 0};
- static boolean rotating[] = {false, false};
- boolean A_set = false;
- boolean B_set = false;
- boolean C_set = false;
- boolean D_set = false;
- void setup()
- {
- Keyboard.begin();
- Mouse.begin();
- pinMode(buttonWhiteL, INPUT_PULLUP);
- pinMode(buttonWhiteR, INPUT_PULLUP);
- pinMode(buttonYellowL, INPUT_PULLUP);
- pinMode(buttonYellowR, INPUT_PULLUP);
- pinMode(buttonGreenL, INPUT_PULLUP);
- pinMode(buttonGreenR, INPUT_PULLUP);
- pinMode(buttonBlueL, INPUT_PULLUP);
- pinMode(buttonBlueR, INPUT_PULLUP);
- pinMode(buttonRed, INPUT_PULLUP);
- delay(1000);
- pinMode(encoderPinA, INPUT_PULLUP);
- pinMode(encoderPinB, INPUT_PULLUP);
- pinMode(encoderPinC, INPUT_PULLUP);
- pinMode(encoderPinD, INPUT_PULLUP);
- attachInterrupt(0, doEncoderC, CHANGE);
- attachInterrupt(1, doEncoderD, CHANGE);
- attachInterrupt(2, doEncoderA, CHANGE);
- attachInterrupt(3, doEncoderB, CHANGE);
- delay(2000);
- }
- void loop() {
- //Encoder Reset
- for (int i = 0; i <= 1; i++)
- {
- rotating[i] = true;
- if (encoderPos[i] != 0)
- {
- if (i == 0) Mouse.move(encoderPos[i], 0, 0);
- if (i == 1) Mouse.move(0, encoderPos[i], 0);
- encoderPos[i] = 0;
- }
- }
- if (analogRead(buttonWhiteL) <= HITBTN) {
- Keyboard.press('z');
- } else {
- Keyboard.release('z');
- }
- if (analogRead(buttonYellowL) <= HITBTN) {
- Keyboard.press('s');
- } else {
- Keyboard.release('s');
- }
- if (analogRead(buttonGreenL) <= HITBTN) {
- Keyboard.press('x');
- } else {
- Keyboard.release('x');
- }
- if (analogRead(buttonBlueL) <= HITBTN) {
- Keyboard.press('d');
- } else {
- Keyboard.release('d');
- }
- if (analogRead(buttonRed) <= HITBTN) {
- Keyboard.press('c');
- } else {
- Keyboard.release('c');
- }
- if (analogRead(buttonBlueR) <= HITBTN) {
- Keyboard.press('f');
- } else {
- Keyboard.release('f');
- }
- if (analogRead(buttonGreenR) <= HITBTN) {
- Keyboard.press('v');
- } else {
- Keyboard.release('v');
- }
- if (analogRead(buttonYellowR) <= HITBTN) {
- Keyboard.press('g');
- } else {
- Keyboard.release('g');
- }
- if (analogRead(buttonWhiteR) <= HITBTN) {
- Keyboard.press('b');
- } else {
- Keyboard.release('b');
- }
- delay(DELAY);
- }
- void doEncoderA()
- {
- if( digitalRead(encoderPinA) != A_set )
- {
- A_set = !A_set;
- if ( A_set && !B_set )
- encoderPos[0] += 1;
- rotating[0] = false;
- }
- }
- void doEncoderB()
- {
- if( digitalRead(encoderPinB) != B_set ) {
- B_set = !B_set;
- if( B_set && !A_set )
- encoderPos[0] -= 1;
- rotating[0] = false;
- }
- }
- void doEncoderC()
- {
- if( digitalRead(encoderPinC) != C_set )
- {
- C_set = !C_set;
- if ( C_set && !D_set )
- encoderPos[1] -= 1;
- rotating[1] = false;
- }
- }
- void doEncoderD()
- {
- if( digitalRead(encoderPinD) != D_set ) {
- D_set = !D_set;
- if( D_set && !C_set )
- encoderPos[1] += 1;
- rotating[1] = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement