Code to identify which button is pressed and load corresponding values

edit: I figured it out... using the method I mentioned at the end of this post, Arrays within Arrays.

I am trying to simplify some code and i'm not sure how to go about it.

I basically have 4 buttons, and I currently have some code to continuously check if any of the 4 buttons are pressed. When any button is pressed, I want to execute a set of commands, but with different arguments depending on which button was pressed.

More specifically, I am trying to make buttons that send 3 MIDI messages using the following commands:

noteOn(0, x, 100)
noteOn(0, y, 100)
noteOn(0, z, 100)

So each button will send a different x/y/z value. My initial thought was to create arrays that contain the chord values:

chord1[] = {48, 55, 68}
chord2[] = {57, 68, 79}

then change the commands to something like

noteOn(0, chord1[0], 100)
noteOn(0, chord1[1], 100)
noteOn(0, chord1[2], 100)

but that would still require me to write an addition if/elseif statement for querying which button is pressed in the first place so we know which chord array to use.

I need some kind of trick to identify which button is pressed and pick the appropriate values without making repetitive code. I'm SURE there is an answer to this, I just can't think of it!

Thanks for the help

edit: additional thought just came to mind. What if I make an array of arrays?

allChords[4][3] = { {48, 49 50}, //chord 1
                                      {51, 52, 53}, //chord 2
                                      {54, 55, 56}, //chord 3
                                      {57, 58, 59}}; //chord 4

then run the variable in noteOn as 

noteOn{0, allChords[i][0], 100}
noteOn{0, allChords[i][1], 100}
noteOn{0, allChords[i][2], 100}

So when i = 0, which is button 1, it will pull the array from the allChords array that contains the proper 3 note values for button 1, then it should split the 3 values into the 3 midi commands... I think that makes sense. And I believe you can make an array of arrays... I hope! :slight_smile:

2D (or more dimensions) arrays are fine indeed but need to provide a type when declaring it.

you could also have a small struct with your 3 values for a given button and make an array of struct. Technically will come to the same result but you would be able to write code that is self documenting

struct t_chordSet {
  uint8_t chord1;
  uint8_t chord2;
  uint8_t chord3;
};

t_chordSet allChords[] = {
  {48, 49, 50},  // chord set 1
  {51, 52, 53},  // chord set 2
  {54, 55, 56},  // chord set 3
  {57, 58, 59}   // chord set 4
};

then call noteOn (with parenthesis not {}) and whatever this function does as

noteOn(0, allChords[i].chord1, 100);
noteOn(0, allChords[i].chord2, 100);
noteOn(0, allChords[i].chord3, 100);

Thanks for the reply. Yes, I meant parenthesis instead of brackets, I was retyping the whole thing and went too quickly!

I'm not very familiar with this "struct" type. Is it assigning variable names to the array values?

Also not familiar with the uint8_t, what is that used for?

Here is some more info on structures

uint8_t is a type for unsigned integer on 8 bits (so basically a byte)

2D arrays will work just fine like you mentioned. I'm not sure what your question is? seems you know how to set up your arrays. where are you stuck? Looking at your approach the only thing i would do different is send the index to the function.... not the entire array.

taterking:
2D arrays will work just fine like you mentioned. I'm not sure what your question is? seems you know how to set up your arrays. where are you stuck? Looking at your approach the only thing i would do different is send the index to the function.... not the entire array.

I figured it out after I wrote it :slight_smile:

Can you explain what you mean by send the index to the function?

I think the question is about noteOn()

If that’s your function and not one from a library you could pass just the reference or pointer to the sub array or structure

noteOn(0, allChords[i], 100);

And in that function you would split the 3 calls to the midi library. That would make the main code look simpler

J-M-L:
I think the question is about noteOn()

If that’s your function and not one from a library you could pass just the reference or pointer to the sub array or structure

noteOn(0, allChords[i], 100);

And in that function you would split the 3 calls to the midi library. That would make the main code look simpler

Oh ok. Yes those functions are from the library.

Which library do you use?

To simplify reading the code if you always have 3 chords playing you could encapsulate that into a function, but that’s just personal preference