1

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);
}
1
  • Which library are you using?
    – Majenko
    May 31, 2017 at 21:11

2 Answers 2

3

Use a variable ( iterator) and count from 0 to 8 (excluding):

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

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

void loop() 
{
    for (uint8 pin = 0; pin < 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 < 8; pin++)
    {
        mcp .digitalWrite(pin, LOW); 
    }

    for (pin = 0; pin < 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.

2
  • apt. I would also throw the routine in a function so I don't even have to repeat the looping code to apply it...
    – dandavis
    May 31, 2017 at 22:05
  • @dandavis ... it can be done, personally, I wouldn't do it because it barely reduces the number of lines and makes it less clear, but it's true it reduces copied code. Normally I start refactoring when it is 3 times used OR (2 times and on different locations). Jun 1, 2017 at 8:11
1

If you are using the Adafruit MCP23017 library there is a shortcut you can use for setting all the GPIO pins in one go:

mcp.writeGPIOAB(val);

val is a 16-bit value which defines the IO level of all 16 pins at once.

There is a similar function for reading:

val = mcp.readGPIOAB();

You can also read just one bank of GPIO pins (why they didn't have a similar write function I have no idea):

valA = mcp.readGPIO(0);
valB = mcp.readGPIO(1);

Unfortunately there aren't any shortcut functions for setting the pin modes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.