How to use a switch to use 2 different styles/ types of code

I’m attempting to use a guitar hero controller as both a MIDI controller and as a generic rhythm game controller (in this specific case, Friday Night Funkin’)

I suppose the first question is if it’s even possible, as the keyboard library (for the rhythm game, just changing the buttons to WASD, enter, space, etc.) And the MIDI library are, simply put, different

If im not mistaken, which I very easily could be, the buttons/ pins have to be defined I’m different ways. If not I would just follow the reccomendations from a different post (Using a push button to select between 2 loops) and specifically use the code stating

void loop()
{
  if(digitalRead(yourPin) == HIGH)
  {
    // your code for the one mode here
  }
  else
  {
    // your code for the other mode here
  }
}

Overall I don’t know a lot about coding, so if it’s a simple answer or I’ve already got it working and just overthinking it, I’m sorry about any inconveniences/ time wasted. Thank you in advance

Hello
Well I guess you will need a debouncer function and eventualy a small FSM, based on SWITCH/CASE instruction, to react on the input

here’s code that handles multiple buttons.

several things

  • the pin is configured as INPUT_PULLUP and the button switch is connected between the pin and ground. the pin will be pulled LOW when the button is active
  • this code recognizes a button press, a single event when the button is pressed. the code recognizing a change in state needs to be removed to simply recognize that the button is pressed
  • this code only recognizes a single button
// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

I’m not sure how the Arduino fits into this project, or what type of Arduino it is. It’s acting as an interpreter between this guitar device and a pc/laptop? How does the guitar device connect to the Arduino?

However it works, sometimes you want it to appear to the pc as a midi device and sometimes as a keyboard/hid device?

Do you need to switch dynamically between the two functions, or is it ok to switch only by unplugging & re-plugging the Arduino, or at least resetting the Arduino?

I’m not sure if the dynamic switching will be possible, and if it is, it will be more complex. I would suggest, to keep things simple, allowing switching between the types only on reset or re-plugging. Then reading the switch would only be done once, at the first line of setup(), and assigned to a global variable. The rest of setup() and loop() would then be conditional on the global variable, similar to the example in your first post.