Functionning program, lowest risk of error on encoders, 50ms response

This commit is contained in:
Edgar P. Burkhart 2021-04-10 11:11:38 +02:00
parent a09124d7fa
commit b2bc717b12
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -1,15 +1,21 @@
#include <Joystick.h>
Joystick_ Joystick;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
21, 0,
false, false, false,
false, false, false,
false, false,
false, false, false);
const byte BR[3] = {1,2,3}; // Button array rows
const byte BC[3] = {4,5,6}; // Button array columns
const byte BK[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; // Buttons controller keys
const byte BR[3] = {9,5,13}; // Button array rows
const byte BC[3] = {10,11,12}; // Button array columns
const byte BK[3][3] = {{0,1,2},{3,4,5},{6,7,8}}; // Buttons controller keys
bool Bstate[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; // Buttons state
const byte RE[6][2] = {{7,8},{9,10},{11,12},{13,14},{15,16},{17,18}}; // Rotary encoders pins {A,B}
const byte REK[6][2] = {{10,11},{12,13},{14,15},{16,17},{18,19},{20,21}}; // Rotary encoders keys {-,+}
bool REstate[6][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}; // Rotary encoders state
const byte RE[6][2] = {{8,2},{7,3},{6,4},{19,18},{21,20},{22,23}}; // Rotary encoders pins {A,B}
const byte REK[6][2] = {{9,10},{11,12},{13,14},{15,16},{17,18},{19,20}}; // Rotary encoders keys {-,+}
unsigned long REchange[6] = {0,0,0,0,0,0}; // Last RE change time
bool REstate[6] = {0,0,0,0,0,0};
void setup() {
for(int i=0; i<12; i++) {
@ -20,6 +26,11 @@ void setup() {
pinMode(BC[i], INPUT_PULLUP);
}
for(int i=0; i<6; i++) {
pinMode(RE[i][0], INPUT_PULLUP);
pinMode(RE[i][1], INPUT_PULLUP);
}
Joystick.begin();
}
@ -32,8 +43,12 @@ void readMatrix() {
pinMode(BR[j], INPUT_PULLUP);
int state = digitalRead(BR[j]);
if (state != Bstate[j][i]) {
Joystick.setButton(BK[j][i], state);
Bstate[j][i] = state;
if (i==0 && j==0) {
Joystick.setButton(BK[j][i], state);
} else {
Joystick.setButton(BK[j][i], !state);
}
Bstate[j][i] = state;
}
pinMode(BR[j], INPUT);
}
@ -43,9 +58,30 @@ void readMatrix() {
}
void readEncoders() {
for (int i=0; i<6; i++) {
if (millis() - REchange[i] > 50) {
Joystick.releaseButton(REK[i][0]);
Joystick.releaseButton(REK[i][1]);
REchange[i] = millis();
}
}
for (int i=0; i<6; i++) {
bool Astate = digitalRead(RE[i][0]);
if (Astate != REstate[i]) {
if(digitalRead(RE[i][1]) == Astate) {
Joystick.pressButton(REK[i][0]);
} else {
Joystick.pressButton(REK[i][1]);
}
REchange[i] = millis();
REstate[i] = Astate;
}
}
}
void loop() {
readMatrix();
readEncoders();
}