Hello, I was doing a button box and I modified a source code I found a little to fit what I was doing. However, I run into an issue, and don't know how to fix it.
void setup() {
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
}
const int pinToButtonMap = 12;
int lastButtonState[3] = {0,0,0};
void loop() {
for (int index = 0; index < 3; index++)
{
int currentButtonState = !digitalRead(index + pinToButtonMap);
if (digitalRead(12) == HIGH && digitalRead(13) == LOW)
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
else if (digitalRead(12) == LOW && digitalRead(13) == HIGH)
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
else
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
}
When running this, the behavior I'd expect is 1 being on if 12 is off but 13 is toggled, 2 being on if vice versa, and 3 being on if none of them are toggled. What I get is 3 is always on, 1 and 2 are on when their conditions are met.
Something else that happens is if I remove the else statement and modify the code to do 2 buttons, 1 and 2 work but they stay on after their conditions are dropped. Fixing either behavior would make this work for me, I don't strictly need a 3rd button to work if 1 and 2 are turning off correctly however a 3rd button would be nice. Any ideas?