How to write a digitalWrite array for MCP23017 expander chip? I'm using multiple expander chips for my project and I was wondering if there is a way to shorten the loop code by using array? So that I don't have a super long code.

void setup() {
  mcp.begin(0);
  mcp1.begin(1);
  mcp.pinMode(0, OUTPUT);
  mcp.pinMode(1, OUTPUT);
  mcp.pinMode(2, OUTPUT);
  mcp.pinMode(3, OUTPUT);
  mcp.pinMode(4, OUTPUT);
  mcp.pinMode(5, OUTPUT);
  mcp.pinMode(6, OUTPUT)
  mcp.pinMode(7, OUTPUT);
  mcp1.pinMode(0, OUTPUT);
  mcp1.pinMode(1, OUTPUT);
  mcp1.pinMode(2, OUTPUT);
  mcp1.pinMode(3, OUTPUT);
  mcp1.pinMode(4, OUTPUT);
  mcp1.pinMode(5, OUTPUT);
  mcp1.pinMode(6, OUTPUT);
  mcp1.pinMode(7, OUTPUT);
}

void loop() {
  mcp.digitalWrite(0, LOW); 
  mcp.digitalWrite(1, LOW); 
  mcp.digitalWrite(2, LOW); 
  mcp.digitalWrite(3, LOW);
  mcp.digitalWrite(4, LOW); 
  mcp.digitalWrite(5, LOW); 
  mcp.digitalWrite(6, LOW); 
  mcp.digitalWrite(7, LOW);
  mcp1.digitalWrite(0, LOW); 
  mcp1.digitalWrite(1, LOW); 
  mcp1.digitalWrite(2, LOW); 
  mcp1.digitalWrite(3, LOW);
  mcp1.digitalWrite(4, LOW);
  mcp1.digitalWrite(5, LOW); 
  mcp1.digitalWrite(6, LOW); 
  mcp1.digitalWrite(7, LOW);
}
Use a variable ( iterator) and count from 0 to 8 (excluding):

void setup() 
{
    mcp.begin(0);
    mcp1.begin(1);

    for (uint8 pin = 0; pin  &lt; 8; pin ++)
    {
        mcp .pinMode(pin, OUTPUT);
        mcp1.pinMode(pin, OUTPUT);
    }
}

void loop() 
{
    for (uint8 pin = 0; pin &lt; 8; pin++)
    {
        mcp .digitalWrite(pin, LOW); 
        mcp1.digitalWrite(pin, LOW); 
    }
}


If the order in which the digital pins are written to LOW matters than split them like in your example, (however, I do not think that is the case).

void loop() 
{
    for (uint8 pin = 0; pin &lt; 8; pin++)
    {
        mcp .digitalWrite(pin, LOW); 
    }

    for (pin = 0; pin &lt; 8; pin++)
    {
        mcp1.digitalWrite(pin, LOW); 
    }
}


Note : for clarity I would call the mcp variables mcp1 and mcp2 instead of mcp resp mcp2, or an even more intuitive name if there is a clear function for both mcp's.

Using uint8 creates an unsigned byte instead of an int which saves also one byte of stack memory.