Hi, I'm very new to Arduino so there may seem like an obvious answer to this question but I'm having trouble addressing pins on an IO expander in an array.
I am using multiple MCP23017 -E/SP chips in a cable continuity testing project. I am trying to create an array that lists the pins of these IO expanders however I'm not sure how to call them up.
I have got the chips working and addressed correctly but it seems to be more of a syntax issue I'm having with this particular problem.
For example, if I want to reference GPIO pin 3 on mcp2 in my array, I am unsure how to write that into the software. I have put some code above of what I assumed it would be [mcp2(3)] but this obviously doesn't compile and I wondered if someone knew how I should write this so that the array looks at the correct pin.
This would make writing the pins into an array much easier as they would all be successive but I just wanted to know if this was possible before wasting my time trying to make it happen?
I have attached how the chips are wired with the diagram and I'm obviously just using different addresses for the different chips but I don't really understand what the code is doing.
I do not have a schematic for this I just wish to be able to address the IO expansion pins on the chips in a similar manner to that of the Arduino board pins.
For examples sake you could imagine that I want to run a continuity test of a cable with 2 ends where each connector has 60 pins so I need 120 total IO and I will be using the IO on the expansion boards to do this test.
The cable would have simple end to end connections e.g. pin 1 on connector 1 goes to pin 2 on connector 2 and so on but I just need to be able to call up the IO expansion pins in my array to be able to do this.
Hi, thank you for the previous code given. It works perfectly well but I was just wondering after this function has run, how do I reference the GPIO pins in an array?
I have attached a schematic of one of the continuity tests (the simplest) I will be running using these IO expanders and was wondering how I would declare 2 arrays for this test for the pins at either end of the cable as I'm unsure of the syntax.
I am basing the code of the below which I found on another forum page however I'm simply trying to replace the array elements in "pinsCableBegin" and "pinsCableEnd" with my own pin addresses on the corresponding schematic.
`
// Cable Tester by "jurs" for Arduino forum
enum {PASS, FAIL_NOTCONNECTED, FAIL_WRONGCONNECTED, FAIL_SHORTENED };
// pin numbers for use at begin and end of cable
const byte pinsCableBegin[]= { 2, 3, 4, 5, };
const byte pinsCableEnd[] = {11,12,13,A0 };
const byte NUMCABLES=sizeof(pinsCableBegin);
void setup() {
Serial.begin(9600);
if (sizeof(pinsCableBegin)!=sizeof(pinsCableEnd))
{
Serial.println("Wrong cable pin configuration!");
Serial.println("Fix declaration of pinsCableBegin[] and pinsCableEnd[] arrays!");
while(1); // error stop with endless loop
}
Serial.println();
Serial.println("################################################");
Serial.println("# CABLE TESTER #");
Serial.println("################################################");
Serial.println();
}
void allPinsInputHigh()
{ // set all pins to INPUT_PULLUP in a for-loop
for (byte i=0;i<NUMCABLES;i++)
{
pinMode(pinsCableBegin[i],INPUT_PULLUP);
pinMode(pinsCableEnd[i],INPUT_PULLUP);
}
}
void DoOneTest()
{
byte result;
Serial.println();
Serial.println("### TEST ###");
for (byte i=0;i<NUMCABLES;i++) // test each pin
{
result= PASS; // initially there is no error found, assume PASS
allPinsInputHigh();
// first test is for continuity and OUTPUT/HIGH
pinMode(pinsCableBegin[i], OUTPUT);
if (digitalRead(pinsCableEnd[i])!=HIGH)
{
bitSet(result,FAIL_NOTCONNECTED);
}
// then check for continuity and OUTPUT/LOW
digitalWrite(pinsCableBegin[i], LOW);
if (digitalRead(pinsCableEnd[i])!=LOW)
{
bitSet(result,FAIL_NOTCONNECTED);
}
// next test: check for wrong connections to other pins
for (byte j=0;j<NUMCABLES;j++)
{
if (j!=i && digitalRead(pinsCableEnd[j])==LOW)
{
bitSet(result, FAIL_WRONGCONNECTED);
}
}
// final test for short circuit against other pins
for (byte j=0;j<NUMCABLES;j++)
{
if (j!=i && digitalRead(pinsCableBegin[j])==LOW)
{
bitSet(result, FAIL_SHORTENED);
}
}
Serial.print("Line ");
Serial.print(i+1);
if (result== PASS) Serial.print(" PASS");
else Serial.print(" FAIL");
if (bitRead(result,FAIL_NOTCONNECTED)) Serial.print(" BREAK");
if (bitRead(result,FAIL_WRONGCONNECTED)) Serial.print(" WRONG");
if (bitRead(result,FAIL_SHORTENED)) Serial.print(" SHORT");
Serial.println();
}
Serial.println("Test finished.");
Serial.println();
}
void loop() {
if (Serial.available())
{
DoOneTest();
delay(20);
while (Serial.available()) Serial.read(); // clear Serial input buffer
}
}
`
Thanks once again and any help is massively appreciated!