Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Mouse.h>
- #include <Keyboard.h>
- // For Leonardo Arduino
- // reference Homingpuyo's, pancakeio
- // button layout
- // Button -- Pin # -- Keyboard
- // FxR 4 b
- // FxL A0 v
- // BT-A A1 s
- // BT-B A2 d
- // BT-C A3 j
- // BT-D A4 k
- // Start A5 t
- // Test A6 N/A
- // Service A7 N/A
- //
- // ENCODERS DATA 1 DATA 2
- // Encoder Right 0 1
- // Encoder Left 3 2
- //
- #define DELAY 5 // Delay per loop in ms
- enum PinAssignments
- {
- encoderPinA = 0,
- encoderPinB = 1,
- encoderPinC = 3,
- encoderPinD = 2,
- };
- //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()
- {
- pinMode(4, INPUT_PULLUP);
- pinMode(A0, INPUT_PULLUP);
- pinMode(A1, INPUT_PULLUP);
- pinMode(A2, INPUT_PULLUP);
- pinMode(A3, INPUT_PULLUP);
- pinMode(A4, INPUT_PULLUP);
- pinMode(A5, INPUT_PULLUP);
- Keyboard.begin();
- 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);
- Serial.begin(9600);
- }
- void loop() {
- if(digitalRead(4)==LOW){
- Keyboard.press('b');
- }
- if(digitalRead(4)==HIGH){
- Keyboard.release('b');
- }
- if(digitalRead(A0)==LOW){
- Keyboard.press('v');
- }
- if(digitalRead(A0)==HIGH){
- Keyboard.release('v');
- }
- if(digitalRead(A1)==LOW){
- Keyboard.press('s');
- }
- if(digitalRead(A1)==HIGH){
- Keyboard.release('s');
- }
- if(digitalRead(A2)==LOW){
- Keyboard.press('d');
- }
- if(digitalRead(A2)==HIGH){
- Keyboard.release('d');
- }
- if(digitalRead(A3)==LOW){
- Keyboard.press('j');
- }
- if(digitalRead(A3)==HIGH){
- Keyboard.release('j');
- }
- if(digitalRead(A4)==LOW){
- Keyboard.press('k');
- }
- if(digitalRead(A4)==HIGH){
- Keyboard.release('k');
- }
- if(digitalRead(A5)==LOW){
- Keyboard.press('t');
- }
- if(digitalRead(A5)==HIGH){
- Keyboard.release('t');
- }
- //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;
- }
- }
- 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