Moving to using Arrays

I'm fairly new to the Arduino platform and want to start looking at how is best to streamline my code for various applications.

An example of this is my MIDI relay switch, at the moment there are lines upon lines of repeated code which is fair from ideal and also space consuming.

An example is this, each program change has its own preset, this is just a snippet, there are 128 pc variables in total!

//Variables to store presets
unsigned int pc0 = 0;
unsigned int pc1 = 0;
unsigned int pc2 = 0;
unsigned int pc3 = 0;
unsigned int pc4 = 0;
unsigned int pc5 = 0;
unsigned int pc6 = 0;
unsigned int pc7 = 0;

I plan to replace all of these with this:

int pc[128];

Obviously when i change this the rest of the code needs to be altered.

In the Setup function, values are read from EEPROM, as follows:

  pc0 = EEPROM.read(0);
  pc1 = EEPROM.read(1);
  pc2 = EEPROM.read(2);
  pc3 = EEPROM.read(3);
  pc4 = EEPROM.read(4);
  pc5 = EEPROM.read(5);

I've ammended that to this:

for (int i = 0; i < 128; ++i )
  pc [ i ] = EEPROM.read ( i );

The main bulk of the code is in the save state and load state functions, both of these used to use the switch and case functions, as follows:

void saveampState ()
{
  switch (proChange)
  {
    case 0:
      pc0 = ((0 << 7) + (0 << 6) + (0 << 5) + (0 << 4) + (amp4State << 3) + (amp3State << 2) + (amp2State << 1) + amp1State << 0);
      EEPROM.write(0, pc0);
      break;
    case 1:
      pc1 = ((0 << 7) + (0 << 6) + (0 << 5) + (0 << 4) + (amp4State << 3) + (amp3State << 2) + (amp2State << 1) + amp1State << 0);
      EEPROM.write(1, pc1);
      break;
    case 2:
      pc2 = ((0 << 7) + (0 << 6) + (0 << 5) + (0 << 4) + (amp4State << 3) + (amp3State << 2) + (amp2State << 1) + amp1State << 0);
      EEPROM.write(2, pc2);
      break;
    case 3:
      pc3 = ((0 << 7) + (0 << 6) + (0 << 5) + (0 << 4) + (amp4State << 3) + (amp3State << 2) + (amp2State << 1) + amp1State << 0);
      EEPROM.write(3, pc3);
      break;  }
}
void loadampState()
{
  switch (proChange)
  {
    case 0:
      amp1State = bitRead(pc0, 0);
      amp2State = bitRead(pc0, 1);
      amp3State = bitRead(pc0, 2);
      amp4State = bitRead(pc0, 3);
      break;
    case 1:
      amp1State = bitRead(pc1, 0);
      amp2State = bitRead(pc1, 1);
      amp3State = bitRead(pc1, 2);
      amp4State = bitRead(pc1, 3);
      break;
    case 2:
      amp1State = bitRead(pc2, 0);
      amp2State = bitRead(pc2, 1);
      amp3State = bitRead(pc2, 2);
      amp4State = bitRead(pc2, 3);
      break;
    case 3:
      amp1State = bitRead(pc3, 0);
      amp2State = bitRead(pc3, 1);
      amp3State = bitRead(pc3, 2);
      amp4State = bitRead(pc3, 3);
      break;  }
}

As you can see, each of those repeated 128 times is far form ideal!

I've replaced the save state with this:

void saveampState ()
{
  int i= proChange;
  {
      pc[i] = ((0 << 7) + (0 << 6) + (0 << 5) + (0 << 4) + (amp4State << 3) + (amp3State << 2) + (amp2State << 1) + amp1State << 0);
      EEPROM.write(i, pc[i]);      
  }
}

And the load state with this:

void loadampState()
{
  int i= proChange;
  {
      amp1State = bitRead(pc[i], 0);
      amp2State = bitRead(pc[i], 1);
      amp3State = bitRead(pc[i], 2);
      amp4State = bitRead(pc[i], 3);
}
}

As i'm away from my Arduino to do any real testing does this look like the most efficient way to perform these functions? I've struggled to understand some of the array functionality and how best to replace the switch and case function :confused:

does this look like the most efficient way to perform these functions?

Not quite. The very first statement you made is (proven) wrong.

I plan to replace all of these with this:

   int pc[128];

But, then, you have:

for (int i = 0; i < 128; ++i )
  pc [ i ] = EEPROM.read ( i );

So, you are reading bytes from EEPROM, and then storing the bytes in ints, wasting a lot of space.

Other than that, yes, what you are doing with arrays, etc. makes a lot of sense (far more than what you were doing).

Many thanks for the reply, I've change it to byte, I guess it's a bad habit to define pins etc as int by default.

Glad to know I'm on the right track :slight_smile:

I guess it's a bad habit to define pins etc as int by default.

I always type my pin number variables intbyte. 8)