Array and string manipulation

Hi everybody
i need a little help about some array and string manipulation in Arduino.

This is my situation:
I'm writing an array that refers to the eight pin of a 4021 shift register.
These pin are connected to BUTTONS or to ROTARY ENCODERS
The button press or rotary movement send some MIDI out.
The main part of the code is made, but i still miss something to make it smaller and easier to read.

This is my idea:
I write an array of the 8 pins: when it is connected to a button i simply write the MIDI Note number that has to be triggered.
When it's connected to a Rotary Encoder i need to write a sigla that tells me something more: that the pin is connected to an encoder, the pin of the encoder (A-B-C), something more i still have to understand (probably the note that has to be triggered by the encoder movement).

Something like this

array muxPins[] = {1, 2, 3, 4, 5, e01A70, e01B71}; // What kind of array must i declare?

// and then to retrieve the data, example for pin 5

char pinType = muxPins[5].substring(0,1); // substring from character 0, length 1
int EncoderNum = muxPins[5].substring(1,2); // substring from character 1, length 2
char EncoderPin = muxPins[5].substring(3,1); // substring from character 3, length 1
num EncoderNote = muxPins[5].substring(4,*); // substring from character 4, till the end of the string

This is now written in a not-existing-code
I don't know how to write it down correctly
Any help will be really appreciated
thanks in advance
g

hey,
use multiple arrays:

#define NPINS 8
#define BUTTON 1
#define ENCODER 2

char type[NPINS] = {BUTTON, BUTTON, ENCODER, ...};
char pin[NPINS] = {...};
int note[NPINS] = {...};

Hi mekon!
Thanks for you help!

The point is that I have alot of arrays since i have 24 4021 shift reg.
I would like to "compress" a bit the code, if possible.

I already have 4 array for each 4021, that makes 24 x 4.

The beginning of the program looks like this:

// Define variables to hold the data for each shift register.
byte muxButtons1 = 0;
byte muxButtons2 = 0;
byte muxButtons3 = 0;
byte muxButtons4 = 0;
byte muxButtons5 = 0;
byte muxButtons6 = 0;
byte muxButtons7 = 0;
byte muxButtons8 = 0;
byte muxButtons9 = 0;
byte muxButtons10 = 0;
byte muxButtons11 = 0;
byte muxButtons12 = 0;
byte muxButtons13 = 0;
byte muxButtons14 = 0;
byte muxButtons15 = 0;
byte muxButtons16 = 0;
byte muxButtons17 = 0;
byte muxButtons18 = 0;
byte muxButtons19 = 0;
byte muxButtons20 = 0;
byte muxButtons21 = 0;
byte muxButtons22 = 0;
byte muxButtons23 = 0;
byte muxButtons24 = 0;

// General midi notes
char buttonsMuxNotes1[] = {1, 1, 1, 3, 4, 5, 6, 7};
char buttonsMuxNotes2[] = {8, 9, 10, 11, 12, 13, 14, 15};
char buttonsMuxNotes3[] = {16, 17, 18, 19, 20, 21, 22, 23};
char buttonsMuxNotes4[] = {24, 25, 26, 27, 28, 29, 30, 31};
char buttonsMuxNotes5[] = {32, 33, 34, 35, 36, 37, 38, 39};
char buttonsMuxNotes6[] = {40, 41, 42, 43, 44, 45, 46, 47};
char buttonsMuxNotes7[] = {48, 49, 50, 51, 52, 53, 54, 55};
char buttonsMuxNotes8[] = {56, 57, 58, 59, 60, 61, 62, 63};
char buttonsMuxNotes9[] = {64, 65, 66, 67, 68, 69, 70, 71};
char buttonsMuxNotes10[] = {72, 73, 74, 75, 76, 77, 78, 79};
char buttonsMuxNotes11[] = {80, 81, 82, 83, 84, 85, 86, 87};
char buttonsMuxNotes12[] = {88, 89, 90, 91, 92, 93, 94, 95};
char buttonsMuxNotes13[] = {0, 1, 2, 3, 4, 5, 6, 7};
char buttonsMuxNotes14[] = {8, 9, 10, 11, 12, 13, 14, 15};
char buttonsMuxNotes15[] = {16, 17, 18, 19, 20, 21, 22, 23};
char buttonsMuxNotes16[] = {24, 25, 26, 27, 28, 29, 30, 31};
char buttonsMuxNotes17[] = {32, 33, 34, 35, 36, 37, 38, 39};
char buttonsMuxNotes18[] = {40, 41, 42, 43, 44, 45, 46, 47};
char buttonsMuxNotes19[] = {48, 49, 50, 51, 52, 53, 54, 55};
char buttonsMuxNotes20[] = {56, 57, 58, 59, 60, 61, 62, 63};
char buttonsMuxNotes21[] = {64, 65, 66, 67, 68, 69, 70, 71};
char buttonsMuxNotes22[] = {72, 73, 74, 75, 76, 77, 78, 79};
char buttonsMuxNotes23[] = {80, 81, 82, 83, 84, 85, 86, 87};
char buttonsMuxNotes24[] = {88, 89, 90, 91, 92, 93, 94, 95};

// Status of each button of the mux
boolean buttonsMuxStatus1[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus2[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus3[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus4[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus5[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus6[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus7[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus8[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus9[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus10[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus11[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus12[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus13[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus14[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus15[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus16[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus17[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus18[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus19[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus20[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus21[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus22[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus23[] = {0,0,0,0,0,0,0,0};
boolean buttonsMuxStatus24[] = {0,0,0,0,0,0,0,0};

// Midi channel where to send notes for each mux
#define buttonsMuxMidiChannel1 1
#define buttonsMuxMidiChannel2 1
#define buttonsMuxMidiChannel3 2
#define buttonsMuxMidiChannel4 2
#define buttonsMuxMidiChannel5 8
#define buttonsMuxMidiChannel6 8
#define buttonsMuxMidiChannel7 4
#define buttonsMuxMidiChannel8 1
#define buttonsMuxMidiChannel9 1
#define buttonsMuxMidiChannel10 2
#define buttonsMuxMidiChannel11 2
#define buttonsMuxMidiChannel12 8
#define buttonsMuxMidiChannel13 8
#define buttonsMuxMidiChannel14 4
#define buttonsMuxMidiChannel15 1
#define buttonsMuxMidiChannel16 1
#define buttonsMuxMidiChannel17 2
#define buttonsMuxMidiChannel18 2
#define buttonsMuxMidiChannel19 8
#define buttonsMuxMidiChannel20 8
#define buttonsMuxMidiChannel21 4
#define buttonsMuxMidiChannel22 1
#define buttonsMuxMidiChannel23 1
#define buttonsMuxMidiChannel24 2

you can always use 2 dimensional arrays:

char buttonMuxNotes[NCHANNELS][INPUTSPERCHANNEL] = {{...},..};

or make it one long array for all channels

char buttonMuxNotes[NCHANNELS * INPUTSPERCHANNEL] = {...};

and then access the values as

buttonMuxNotes[channel * INPUTSPERCHANNEL + input]

Does the values inside the char buttonsMuxNotes1...24[] change?

If not it does look like you could easily write a function like:

byte getMidiNote(byte muxNum,byte index){
  return (muxNum*8)+index;
}

And later instead of doing:

byte val = buttonsMuxNotes3[4]; //val == 20

You could do:

byte val = getMidiNote(2,4); //val == 20

Are there any particular reason you need to store button states? You could (maybe) just read them and act upon results when needed.

@AlphaBeta:
the midi notes or rotary data for each mux pin are not changing, but they are not sequential as i wrote in the example (1, 2, 3, 4, 5....). And the the point would be to have not only notes numbers (1, 2, 3..) but also more complex data realtive to Rotary Encoders (e1A070, e1B071.... where e=encoder, 1=number of encoder, A= encoder's pin, 070=note triggered by that pin...).
The state is needed to undertsand when there's a change
if it changes from 0 to 1 i fire a noteOn midi
if it changes from 1 to 0 i fire a noteOff midi

@mekon83
i'll try to understand if 3 dimensional arrays can be helpfull

Thanks for the help guys!

I would recommend using structures instead of 3d arrays =) 3d arrays will be a mess too.

mmmm...
sorry but my (small) knowledge it's coming to the end :frowning:
what do you mean with structures?
any link to read and get some inspiration?

thanks!

I see. That's more advanced C language, see http://www.itee.uq.edu.au/~comp2303/Leslie_C_ref/C/SYNTAX/struct.html (that's the best I found in 20 secs, I bet you can find better)
Or you can use classes, that is quite the same for you, see http://arduino.cc/en/Hacking/LibraryTutorial

But if you don't know about class or struct statements, don't worry about that, you can do well with arrays

Mekon
thanks for these input
i'm considering all these possibilities.. and decide which way to go!