#include <Keypad.h>
#include <Encoder.h>
int period1 = 10;
int period2 = 35;
unsigned long time_now1 = 0;
unsigned long time_now2 = 0;
unsigned long time_now3 = 0;
unsigned long time_now4 = 0;
unsigned long time_now5 = 0;
unsigned long time_now6 = 0;
unsigned long time_now7 = 0;
unsigned long time_now8 = 0;
unsigned long time_now9 = 0;
unsigned long time_now10 = 0;
unsigned long time_now11 = 0;
unsigned long time_now12 = 0;
unsigned long time_now13 = 0;
const byte ROWS = 5; //five rows
const byte COLS = 4; //four columns
byte rowPins[ROWS] = {15, 16, 17, 18, 19}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {20, 21, 22, 23}; //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}
};
Keypad kpd = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
Encoder volumeEncoder (2, 3);
Encoder fuelmapEncoder (4, 5);
Encoder brakebiasEncoder(6, 7);
Encoder absEncoder (8, 9);
Encoder front_arbEncoder(10, 11);
Encoder rear_arbEncoder (12, 14);
long positionVolume = -999;
long positionFuelmap = -999;
long positionBrakebias = -999;
long positionAbs = -999;
long positionFront_arb = -999;
long positionRear_arb = -999;
void setup() {
// Hat switch buttons
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(24, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
}
void loop() {
if(millis() >= time_now1 + period1){
time_now1 += period1;
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();
}
}
}
long newVolume = volumeEncoder.read();
if (newVolume > positionVolume) {
positionVolume = newVolume;
Joystick.button(21, 1);
if(millis() >= time_now2 + period2){
time_now2 += period2;
Joystick.button(21, 0);
}
}
if (newVolume < positionVolume) {
positionVolume = newVolume;
Joystick.button(22, 1);
if(millis() >= time_now3 + period2){
time_now3 += period2;
Joystick.button(22, 0);
}
}
long newFuelmap = fuelmapEncoder.read();
if (newFuelmap > positionFuelmap) {
positionFuelmap = newFuelmap;
Joystick.button(23, 1);
if(millis() >= time_now4 + period2){
time_now4 += period2;
Joystick.button(23, 0);
}
}
if (newFuelmap < positionFuelmap) {
positionFuelmap = newFuelmap;
Joystick.button(24, 1);
if(millis() >= time_now5 + period2){
time_now5 += period2;
Joystick.button(24, 0);
}
}
long newBrakebias = brakebiasEncoder.read();
if (newBrakebias > positionBrakebias) {
positionBrakebias = newBrakebias;
Joystick.button(25, 1);
if(millis() >= time_now6 + period2){
time_now6 += period2;
Joystick.button(25, 0);
}
}
if (newBrakebias < positionBrakebias) {
positionBrakebias = newBrakebias;
Joystick.button(26, 1);
if(millis() >= time_now7 + period2){
time_now7 += period2;
Joystick.button(26, 0);
}
}
long newAbs = absEncoder.read();
if (newAbs > positionAbs) {
positionAbs = newAbs;
Joystick.button(27, 1);
if(millis() >= time_now8 + period2){
time_now8 += period2;
Joystick.button(27, 0);
}
}
if (newAbs < positionAbs) {
positionAbs = newAbs;
Joystick.button(28, 1);
if(millis() >= time_now9 + period2){
time_now9 += period2;
Joystick.button(28, 0);
}
}
long newFront_arb = front_arbEncoder.read();
if (newFront_arb > positionFront_arb) {
positionFront_arb = newFront_arb;
Joystick.button(29, 1);
if(millis() >= time_now10 + period2){
time_now10 += period2;
Joystick.button(29, 0);
}
}
if (newFront_arb < positionFront_arb) {
positionFront_arb = newFront_arb;
Joystick.button(30, 1);
if(millis() >= time_now11 + period2){
time_now12 += period2;
Joystick.button(30, 0);
}
}
long newRear_arb = rear_arbEncoder.read();
if (newRear_arb > positionRear_arb) {
positionRear_arb = newRear_arb;
Joystick.button(31, 1);
if(millis() >= time_now12 + period2){
time_now12 += period2;
Joystick.button(31, 0);
}
}
if (newRear_arb < positionRear_arb) {
positionRear_arb = newRear_arb;
Joystick.button(32, 1);
if(millis() >= time_now13 + period2){
time_now13 += period2;
Joystick.button(32, 0);
}
}
int angle =-1;//no angle input
if (digitalRead(0)==LOW) angle =0;
if (digitalRead(1)==LOW) angle =90;
if (digitalRead(24)==LOW) angle =180;
if (digitalRead(25)==LOW) angle =270;
Joystick.hat(angle);
}
}