Encoder.H code
#ifndef Encoder_h
#define Encoder_h
#include "Arduino.h"
class Encoder {
private:
int DTPin;
int CLKPin;
int SWPin;
bool currentCLKState;
bool lastCLKState;
public:
Encoder(int CLK, int DT);
Encoder(int CLK, int DT, int SW);
void setDTPin(int DT);
void setCLKPin(int CLK);
void setSWPin(int SW);
bool getDT();
bool getCurrentCLKState();
bool getLastCLKState();
void setLastCLKState(int state);
bool getSwitchState();
};
#endif
Leonardo Code
#include <Joystick.h>
#include <Encoder.h>
#define ROWS 6
#define COLUMNS 5
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
40, 0, // Button Count, Hat Switch Count
false, false, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
Encoder encoder(0,3,18);
Encoder encoder1(1,4,20);
Encoder encoder2(2,5,21);
void setup() {
//Serial.begin(9600);
Joystick.begin();
for(int i = 6; i<=14; i++){
if(i==11){i=14;}
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
for(int o = 15; o<=21; o++){
if(o==17){o=18;}
pinMode(o, INPUT);
}
for(int e = 0; e<=5; e++){
pinMode(e, INPUT_PULLUP);
}
attachInterrupt(digitalPinToInterrupt(1), readRotary,RISING);
attachInterrupt(digitalPinToInterrupt(0), readRotary,RISING);
attachInterrupt(digitalPinToInterrupt(2), readRotary,RISING);
encoder.setLastCLKState(encoder.getCurrentCLKState());
encoder1.setLastCLKState(encoder1.getCurrentCLKState());
encoder2.setLastCLKState(encoder2.getCurrentCLKState());
}
void loop() {
int k = 0;
for(int r = 6; r<=14; r++)
{
if(r==11){r=14;}
digitalWrite(r, HIGH);
for(int c = 15; c<=20; c++){
if(c==17){c=18;}
if(r==6 && c<19){
if(digitalRead(c)){
Joystick.releaseButton(k+1);
Joystick.pressButton(k);
}
if(!digitalRead(c)){
Joystick.releaseButton(k);
Joystick.pressButton(k+1);
}
k+=2;
}
else if((r==6 && c>18) || (r==7 && c<20)){
if(digitalRead(c)){
Joystick.releaseButton(k+1);
Joystick.releaseButton(k+2);
Joystick.pressButton(k);
}
else if(digitalRead(c+1)){
Joystick.releaseButton(k);
Joystick.releaseButton(k+1);
Joystick.pressButton(k+2);
}
else{
Joystick.releaseButton(k);
Joystick.releaseButton(k+2);
Joystick.pressButton(k+1);
}
k+=3;
c++;
}
else if(r==14 && c>18){}
else{
if(digitalRead(c)){
Joystick.pressButton(k);
}
if(!digitalRead(c)){
Joystick.releaseButton(k);
}
k++;
}
}
digitalWrite(r, LOW);
}
for(int i = 0; i<6; i++){
delay(50);
Joystick.releaseButton(i+34);
}
}
void readRotary(){
int btn = 34;
volatile bool currentState = encoder.getCurrentCLKState();
volatile bool currentState1 = encoder1.getCurrentCLKState();
volatile bool currentState2 = encoder2.getCurrentCLKState();
if(currentState != encoder.getLastCLKState() && currentState == 0)
{
if(encoder.getDT() == 1){
Joystick.pressButton(btn);
}else{
Joystick.pressButton(btn+1);
}
}
if(currentState1 != encoder1.getLastCLKState() && currentState1 == 0)
{
if(encoder1.getDT() == 1){
Joystick.pressButton(btn+2);
}
else{
Joystick.pressButton(btn+3);
}
}
if(currentState2 != encoder2.getLastCLKState() && currentState2 == 0)
{
if(encoder2.getDT() == 1){
Joystick.pressButton(btn+4);
}
else{
Joystick.pressButton(btn+5);
}
}
encoder.setLastCLKState(currentState);
encoder1.setLastCLKState(currentState1);
encoder2.setLastCLKState(currentState2);
}