Hello. I am having an issue with my Arduino registering multiple inputs from a single button press. I am trying to setup a button box for my sim racing rig. Now as I have since discovered after completing my project build that the Mega boards do not support HID from factory and require a work around. I have managed to get this issue sorted and can set it up to be read as a gamepad.
That being said, I am now having an issue with all the buttons/switches/encoders, that one press from any of them reads as multiple inputs on a gamepad tester. This is very strange considering all the buttons/switches/encoders work perfect inside Ardunio IDE and print in the serial monitor one at a time, and only one Pin.
I am wondering if this has something to do with the "UnoJoy fix", something I did wrong, forgot to do, compatability issue? I have no clue at this point, hence why I am here asking for help.
Here is the full sketch for my project:
#include <Encoder.h>
#include <BfButton.h>
#include <BfButtonManager.h>
byte clockpin[] = { 40, 44, 48, 52}; // "clock" pin array
byte datapin[] = { 38, 42, 46, 50}; // "data" pin array
byte pushpin[] = { 39, 43, 47, 51}; // "push" pin array
const byte arraysize = sizeof(clockpin) / sizeof(clockpin[0]); // number of encoders
byte pushstate[arraysize]; // state of each button
byte clockstate[arraysize], lastclockstate[arraysize], datastate[arraysize]; // states
byte encocount[arraysize]; // encoders count 0 to 255
byte pushcount[arraysize]; // pushbuttons count 0 to 255
//PUSH BUTTONS
int numbuts = 32;
int bts[32]; //
boolean btgs[32]; //sends one message, not continuous data
unsigned long pushtimer[arraysize], timeout = 350; // adjust timeout for lowest value
#define BAUD 9600
void setup() {
Serial.begin(BAUD); // initialize Serial Monitor
for (byte i = 0; i < arraysize; i++) { // configure DIO pins
pinMode(clockpin[i], INPUT_PULLUP); //
pinMode(datapin[i], INPUT_PULLUP); //
pinMode(pushpin[i], INPUT_PULLUP); //
lastclockstate[i] = digitalRead(clockpin[i]); // first state reading
//pinMode(bt1, INPUT_PULLUP); //pushbutton #1 pin 6, single button code
//bt1g = false; //single button code
for(int i=0; i<numbuts; i++) bts[i] = i+6;
for(int i=0; i<numbuts; i++) btgs[i] = false;
for(int i=0; i<numbuts; i++) pinMode(bts[i], INPUT_PULLUP);
}
}
//PUSH BUTTONS
int buttonStatus = 0;
void loop() {
for (byte i = 0; i < arraysize; i++) {
// pushbutton ENCODER
pushstate[i] = digitalRead(pushpin[i]); // read each pushbotton
if ((pushstate[i] == LOW) && (millis() - pushtimer[i] > timeout)) { // debounce pushbutton
pushtimer[i] = millis(); // set new timer
++pushcount[i]; // increment pushbutton count
showstates("Button : ", i, " | Presscount: ", pushcount[i]); // send data to formatter
}
// clockstate
clockstate[i] = digitalRead(clockpin[i]); // read clock state for each encoder
if (clockstate[i] != lastclockstate[i]) { // if clock state changed
lastclockstate[i] = clockstate[i]; // store state
// datastate
datastate[i] = digitalRead(datapin[i]); // datastate for each encoder
if (datastate[i] == 1 && clockstate[i] == 0) { // states for CC rotation
encocount[i]++; // increment counter
showstates("Encoder: ", i, " | CW | Count: ", encocount[i]); // send data to formatter
}
if (datastate[i] == 0 && clockstate[i] == 0) { // states for CW rotation
encocount[i]--; // decrement counter
showstates("Encoder: ", i, " | CC | Count: ", encocount[i]); // send data to formatter
}
}
//PUSH BUTTONS
for(int i=0; i<numbuts; i++){
if(!btgs[i]) {
if(digitalRead(bts[i])==LOW){
Serial.print("bt" + String(i) + ":");
Serial.println(1);
btgs[i] = true;
}
}
else {
if(digitalRead(bts[i])==HIGH) {
Serial.print("bt" + String(i) + ":");
Serial.println(0);
btgs[i] = false;
}
}
}
delay(5);
}
}
void showstates(char *name, int number, char *action, int count) { // Serial Monitor formatting
Serial.print(name);
Serial.print(number);
Serial.print(action);
Serial.print(count);
Serial.println();
}
Here is a general example from Wowki, it's missing a few switches as they didn't have what I was looking for. The buttons on my project use pins 9,10,11,12,13 as well as 22-37
Please let me know if I am missing any vital info here. Thanks!
