Code:
//Six rows on pins 0, 1, 2, 3, 4, 5
//Seven columns on pins 23, 22, 21, 20, 19, 18, 17,
//Four rotary encoders, pins (33,34) (36,37) (38,39) (40,41)
//Three potentiometers, pins 24, 25, 26
#include <Keypad.h>
#define JOYSTICK_SIZE 64 // 12 = normal, 64 = extreme joystick
#include "EncoderTool.h"
using namespace EncoderTool;
//Button Matrix Section
const byte ROWS = 6; //four rows
const byte COLS = 7; //four columns
byte rowPins[ROWS] = {0, 1, 2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {23, 22, 21, 20, 19, 18, 17,}; //connect to the column pinouts of the keypad
char keyMap[ROWS][COLS] = {
{1,2,3,4,5,6,7},
{8,9,10,11,12,13,14},
{15,16,17,18,19,20,21},
{22,23,24,25,26,27,28},
{29,30,31,32,33,34,35},
{36,37}
};
Keypad kpd = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
//Encoder Section
PolledEncoder encoderA;
PolledEncoder encoderB;
PolledEncoder encoderC;
PolledEncoder encoderD;
void setup()
{
encoderA.begin(33, 34);
encoderB.begin(36, 37);
encoderC.begin(38, 39);
encoderD.begin(40, 41);
Joystick.X(512); // same as Windows default
Joystick.Y(512); // same as Windows default
Joystick.Zrotate(512); // same as Windows default
analogReadAveraging(50);
Joystick.useManualSend(true);
}
void loop()
{
Joystick.hat(1, 361); //For some reason this is Hat #4
Joystick.hat(2, 361); // This is Hat #3
Joystick.hat(3, 361); // This is Hat #2
Joystick.hat(4, 361); // Hat #1
long xValue;
long yValue;
long zValue;
xValue = (map(analogRead(24),16,996,0,1024))*64;
yValue = (map(analogRead(25),16,996,0,1024))*64;
zValue = (map(analogRead(26),16,996,0,1024))*64;
Joystick.X(xValue);
Joystick.Y(yValue);
Joystick.Zrotate(zValue);
encoderA.tick();
encoderB.tick();
encoderC.tick();
encoderD.tick();
Joystick.send_now();
if ( kpd.getKeys() )
{
for (int i=0; i<LIST_MAX; i++)
{
if ( kpd.key[i].stateChanged )
{
if ( kpd.key[i].kstate == PRESSED || kpd.key[i].kstate == HOLD)
{
Joystick.button(kpd.key[i].kchar, 1);
} else if ( kpd.key[i].kstate == RELEASED )
{
Joystick.button(kpd.key[i].kchar, 0);
}
Joystick.send_now();
}
}
}
if (encoderA.valueChanged())
{
if (encoderA.getValue() == 1)
{
Joystick.button(38, 1);
encoderA.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(38, 0);
}
else if (encoderA.getValue() == -1)
{
Joystick.button(39, 1);
encoderA.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(39, 0);
}
else
{
encoderA.setValue(0);
Joystick.button(38, 0);
Joystick.button(39, 0);
Joystick.send_now();
}
}
if (encoderB.valueChanged()) // do we have a new value?
{
if (encoderB.getValue() == 1)
{
Joystick.button(40, 1);
encoderB.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(40, 0);
}
else if (encoderB.getValue() == -1)
{
Joystick.button(41, 1);
encoderB.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(41, 0);
}
else
{
encoderB.setValue(0);
Joystick.button(40, 0);
Joystick.button(41, 0);
Joystick.send_now();
}
}
if (encoderC.valueChanged()) // do we have a new value?
{
if (encoderC.getValue() == 1)
{
Joystick.button(42, 1);
encoderC.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(42, 0);
}
else if (encoderC.getValue() == -1)
{
Joystick.button(43, 1);
encoderC.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(43, 0);
}
else
{
encoderC.setValue(0);
Joystick.button(43, 0);
Joystick.button(44, 0);
Joystick.send_now();
}
}
if (encoderD.valueChanged()) // do we have a new value?
{
if (encoderD.getValue() == 1)
{
Joystick.button(45, 1);
encoderD.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(45, 0);
}
else if (encoderD.getValue() == -1)
{
Joystick.button(46, 1);
encoderD.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(46, 0);
}
else
{
encoderD.setValue(0);
Joystick.button(45, 0);
Joystick.button(46, 0);
Joystick.send_now();
}
}
delay(5);
}