Array's values to pinouts

Hey people.

I place 10 values in an array. These values go from 1 to 25. So, each one of the 25 possible values corresponds to a pinout.

I could do this:

Switch(array[x]) {
  case 1:
  digitalWrite(pinout[20], HIGH);
  case 2:
  digitalWrite(pinout[22], HIGH);
  ...and so on...

I think it would work, but is there a simpler(and shorter) solution?

Thanks! :slight_smile:

What about

digitalWrite(pinout[array[x]], HIGH);

The pin formula seems to be more complex as I see
1 → 20
2 → 22
better do some boundary testing for robustness

if ((array[x] >=1) && (array[x] <= 25))
{
  byte pin = 18 + 2 * array[x];
  digitalWrite(pinout[pin], HIGH);  
}
else
{
  // handle the default of the switch 
}

Maybe you need a mapping array that maps the numbers 1…25 on the output pins.
byte m[26] = {-1, 20, 22, … };

Aeturnalus, I didnt' know that it could manage something like that... what a shame. That was obvious. Thanks! :wink:

robtillaart, that first code u wrote would work only if the array followed a pattern. But I think would be harder to find pattern in that than do the switch/case, u'll see below.

I did the mapping, like robtillaart said. Thank you! Its something like that:

  int noteoutput[25] = {
  20, 21, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 41, 42, 43, 44, 
  45, 46, 47, 48, 49, 50, 51, 52, 53};

And now I did this, like Aeturnalus said. Thank you too! :

digitalWrite(note[notepinout[x]], HIGH);

The note has 10 positions. It can repeat the same pinout. Like {1, 5, 1, 5, 10, 15, 10, 15, 2, 5}, and the pins that will go high, in this case, are these {20, 26, 20, 26, 36, 44, 36, 44, 21, 26}.

I'm finishing my project now. It is a Mini Musical Keyboard (25 notes) that is played by the action of solenoid valves. There a manual and an automatic mode. In the manual you choose 10 notes to play sequentially, with 2 possible beat rates. In the automatic mode it plays some pre-programmed musics following a Score (sheet music). That will be fun. Its for college.

Thank you all. :slight_smile:

If you want to you can post your final project in the “exhibition gallery” section (with picture or tube link) to inspire other people …

I'll do that, for sure.

Thanks. 8)

For more on arrays used like this see:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html