Button Box For PC Driving Sim Question

Hey guys and gals. I'm new to the forum and to Arduino in general. I'm interested in building a driving sim button box, like the one AMStudio describe in their YouTube video here: MAKE THIS BUTTON BOX | 32 FUNCTION w ENCODERS - YouTube

In the description of the video, they include a link for a sketch that they created themselves. I have a Pro Micro board, and have uploaded the sketch they provide, and connected a couple of switches to test it. It all works exactly as advertised. No issues at all.

Which brings me to my question. Is it possible to modify the sketch code in order to make a single, specific switch behave differently that all the others? Here's an example of what I mean - Can a toggle switch at button location 7, be made to act more like a momentary push button. I.E. if you switch the toggle from OFF to ON, the output goes HIGH for a specified time (500ms, or so), and then goes back LOW and stays there. When you flip the switch from ON back to OFF, the same action takes place. Again, but ONLY switch 7 will work this way. All others work just as described in the sketch.

Is something like that possible? I know almost nothing about coding, but I have been messing with their sketch a bit. I've been able to modify it to make switches behave like I described, but it causes ALL of the switches to behave that way. And that's not what I want.

So is that possible?

Here's a link directly to the sketch : GitHub - AM-STUDIO/32-FUNCTION-BUTTON-BOX

Thank you so much for any input you can provide!

Is something like that possible? I know almost nothing about coding, but I have been messing with their sketch a bit. I've been able to modify it to make switches behave like I described, but it causes ALL of the switches to behave that way.

Please post the code that you came up with

Here’s what I’ve come up with:

void CheckAllButtons(void) {
      if (buttbx.getKeys())
    {
       for (int i=0; i<LIST_MAX; i++)   
        {
           if ( buttbx.key[i].stateChanged )   
            {
            switch (buttbx.key[i].kstate) {  
                    case PRESSED:
                    case HOLD:
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              break;
                    case RELEASED:
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              delay(50);
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              delay(800);
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;
                    case IDLE:
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;
            }
           }

The only parts that I added are under “RELEASED”. The rest of it is straight from the code they provide. (The code listed is only a small part of the sketch).

And yes, I realize the OFF to ON action doesn’t produce a “pulse”, only the ON to OFF action does. But it works correctly In-Game, and that is my ultimate goal anyway.

it causes ALL of the switches to behave that way. And that's not what I want.

All of the switches behave in the same way because they share the same code in the for loop that iterates through them. The name of the function, ie CheckAllButtons(), is a bit of a giveaway

Which button in the array do you want to behave differently than the others ? Which value of i (the for loop variable) relates to the odd one out ?

The specific button that I want to behave differently would be button 1. It's actually the second button, since they are enumerated starting with 0.

Put a test for the value of i in the RELEASED case and run the original code for any value other than 1 and your modified code for a value of 1

I tried this.

for (int i=0; i<LIST_MAX; i++)   
        {
           if ( buttbx.key[i].stateChanged )   
            {
            switch (buttbx.key[i].kstate) {  
                    case PRESSED:
                    case HOLD:
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              break;
                    case RELEASED:
                              Joystick.setButton(buttbx.key[1-32].kchar, 0);
                              Joystick.setButton(buttbx.key[0].kchar, 0);
                              delay(50);
                              Joystick.setButton(buttbx.key[0].kchar, 1);
                              delay(700);
                              Joystick.setButton(buttbx.key[0].kchar, 0);
                              break;
                    case IDLE:
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;
            }

In this case, “0” would be the button treated differently. But it continued to treat ALL the buttons the same. They ALL have the “pulse” after releasing them.

Joystick.setButton(buttbx.key[1-32].kchar, 0);

Do you have a button with an array index of -31 ?

What I had in mind was

case RELEASED :
if (i == oddOneOut)
{
  //execute this code
}
else
{
  //execute this code
}

Thank you so much for your help so far, Bob. I really appreciate it.

Upon your advise, I tried this:

void CheckAllButtons(void) {
      if (buttbx.getKeys())
    {
       for (int i=0; i<LIST_MAX; i++)   
        {
           if ( buttbx.key[i].stateChanged )   
            {
           switch (buttbx.key[i].kstate) {  
                    case PRESSED:
                    case HOLD:
                    if (i=1)
                    {
                              Joystick.setButton(buttbx.key[1].kchar, 1);
                              delay(700);
                              Joystick.setButton(buttbx.key[1].kchar, 0);
                              break;
                    }
                    else
                    {
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;
                    }
                    case RELEASED:
                    if (i=1)
                    {
                              Joystick.setButton(buttbx.key[1].kchar, 1);
                              delay(700);
                              Joystick.setButton(buttbx.key[1].kchar, 0);
                              break;
                    }
                    else
                    {
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;
                    }
                    case IDLE:
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;

This causes “Button 1” to work EXACTLY as I want it to. However, it also makes ALL the buttons show up as “Button 1”.

Would it just be easier to use two separate Arduino boards. One with the original, unchanged code for Buttons 2-32, and use the other with this edited code for only Button 1?

if (i=1)

That does not test whether i is equal to 1, rather it sets i to 1

Yes. That is correct. My bad. I changed it to read “i == 1” instead. Unfortunately, that also did not work. It treats all buttons the same. As if the “if-else” statement wasn’t even there.

Print some messages to allow you to determine which sections of code are being executed and the value of pertinent variables at those points

I've gotten it to work... Sort-of...

With the code listed below, when I press and hold button 1, it acts like a "press and release", which is what I want. But then, it follows that with another "press and release". So it's executing the action TWICE. When I let go of the button, it acts exactly like I want. It does another "press and release" action. All the other buttons act just as they should. Any idea what's causing this code to perform that action twice when the button is pressed and held?

if ( buttbx.key[i].stateChanged )   
            {
           switch (buttbx.key[i].kstate) {  
                    case PRESSED:
                    case HOLD:
                    if (buttbx.key[i].kchar == 1)
                    {
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              delay(400);
                              Joystick.setButton(buttbx.key[i].kchar, 0);    
                    }
                    else
                    {
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              break;
                    }
                    case RELEASED:
                    if (buttbx.key[i].kchar == 1)
                    {
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              delay(400);
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                    }
                    else
                    {
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;
                    }
                    case IDLE:
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;