GPIO Array not working properly.

Hi all, i’m using several GPIO’s in Arduino mega due to large number of IO i’m using array to declare pins and enable it’s output using for-loop, code for same mentioned below.

//.......Declaring Relays...............//
const int Part1Relays[] ={0, 1, 2, 3, 4, 5, 6, 7};
const int Part2Relays[] ={14, 15, 16, 17, 18, 19, 20, 21};
const int Part3Relays[] ={23, 25, 27, 29, 31, 33, 35, 37};

//.............Command Functions.............//

void Part1Cmd(){
    for (int RelayPin = 0; RelayPin < PinCount; RelayPin++){
    digitalWrite(Part1Relays[RelayPin], LOW);
    }
  }

  void Part2Cmd(){
    for (int RelayPin = 0; RelayPin < PinCount; RelayPin++){
    digitalWrite(Part2Relays[RelayPin], LOW);
    }
  }

  void Part3Cmd(){
    for (int RelayPin = 0; RelayPin < PinCount; RelayPin++){
    digitalWrite(Part3Relays[RelayPin], LOW);
    }
  }

But something unpredictable is happening whenever i change declared pin no. in array either it does not enable all the declared pin or enable all pins irrespective of defined one. I don’t understand why this happens at first it was working perfectly but as soon i modify pin numbers inside array it start working improperly. Please help what could be the issue whether i have to define size of array or there is something else??

This is not valid in the C++ language:

    digitalWrite(Part2Relays[11, 12, 13, 14, 15, 16, 17, 18], LOW);

It may compile, but it doesn't do what you think it does.

You had the first one "Part1Cmd()" right, do the other two the same, correct way. :0

Follow this same format for part2 and part3

    for (int RelayPin = 0; RelayPin < PinCount; RelayPin++){
    digitalWrite(Part1Relays[RelayPin], LOW);
    }

Those could all be byte as the values are <= 255, no need for them to be int (-32768 to 32767), or unsigned int (0 to 65535). Save a byte of memory on each one, might come in handy one day.

One thing more i noted when i delete all array’s and rewrite them it again start working properly and when again i make changes to pins it start behaving abnormally!!! I don’t understand why this is happening…

You haven't posted enough to say.

Are you suggesting that this does not work?

digitalWrite(Part1Relays[1], LOW); // where 0 to 7 can be used

:
:
digitalWrite(Part1Relays[1], HIGH);

CrossRoads:
You haven't posted enough to say.

Are you suggesting that this does not work?

digitalWrite(Part1Relays[1], LOW); // where 0 to 7 can be used

:
:
digitalWrite(Part1Relays[1], HIGH);




As mentioned earlier i'm using Arduino pins using an array and calling several function based on required operations of pins but as whenever i edit or make changes in array declaration like changing pin nos. it start behaving improperly. what could be the reason?


//.......Declaring Relays...............//
const int Part1Relays ={0, 1, 2, 3, 4, 5, 6, 7};
const int Part2Relays ={14, 15, 16, 17, 18, 19, 20, 21};
const int Part3Relays ={23, 25, 27, 29, 31, 33, 35, 37};

Nobody can ever tell you the reason unless you post the entire sketch.