Hi,
I'm trying to build a cable harness testare. The cable harness is composed of 27 cables. I'm using Arduino Mega2560 and MCP23017 to get enough IOs.
OUT_PIN[] array is to manage Arduino digital outputs
MCP_OUT_PIN[] array to manage the expander digital outputs
IN_PIN[] array is to manage Arduino digital inputs
MCP_IN_PIN[] array is to manage the expander digital inputs
I'm wondering if there is any way to iterate through the two output arrays and the same for the input arrays.
I tired to concatenate the two output/input arrays (OUT_PIN_TOTAL [] and IN_PIN_TOTAL[]) but I dont't think this will work. wheres as the output and input of expander IOs need to managed separately via Adafruit_MCP23X17 class.
#include <Wire.h>
#include <Adafruit_MCP23X17.h>
#include <LiquidCrystal_I2C.h>
#include <avr/wdt.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
Adafruit_MCP23X17 mcp;
uint8_t OUT_PIN[] = {30, 32, 34, 36, 38};
uint8_t IN_PIN[] = {40, 42, 44, 46, 48};
uint8_t MCP_OUT_PIN[] = {5, 6, 7};
uint8_t MCP_IN_PIN[] = {10, 9, 8};
uint8_t OUT_PIN_TOTAL[8];
uint8_t IN_PIN_TOTAL[8];
int NoContinuity;
#define MCP_INPUTPIN 2
#define MCP_LEDTOG1 11
#define MCP_LEDTOG2 3
int continuityResult[20];
int shortCableResult[20];
int wrongWiringResult[20];
int shortCableOnce[20];
int count = 0;
int startButton = 52;
int buttonState = 0;
int lastButtonState = 0;
int checkForFault = 0;
void setup() {
mcp.begin_I2C(); // Default device address 0
/* mcp.pinMode(MCP_LEDTOG1, OUTPUT); // Toggle LED 1
mcp.pinMode(MCP_LEDTOG2, OUTPUT); // Toggle LED 2
/*mcp.pinMode(MCP_LED1, OUTPUT); // LED output
mcp.digitalWrite(MCP_LED1, HIGH);
//mcp.pinMode(MCP_INPUTPIN,INPUT); // Button i/p to GND
mcp.pinMode(MCP_INPUTPIN, INPUT_PULLUP); // Puled high to ~100k
// mcp.pullUp(MCP_INPUTPIN,HIGH); */
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.backlight();
pinMode (startButton, INPUT_PULLUP);
for (uint8_t i = 0; i < sizeof(OUT_PIN); i++)
{
pinMode(OUT_PIN, OUTPUT);
digitalWrite(OUT_PIN, HIGH);
}
for (uint8_t i = 0; i < sizeof(IN_PIN); i++)
{
pinMode(IN_PIN, INPUT_PULLUP);
}
for (uint8_t i = 0; i < sizeof(MCP_OUT_PIN); i++)
{
pinMode(MCP_OUT_PIN, OUTPUT);
digitalWrite(MCP_OUT_PIN, HIGH);
}
for (uint8_t i = 0; i < sizeof(MCP_IN_PIN); i++)
{
pinMode(MCP_IN_PIN, INPUT_PULLUP);
}
memcpy(OUT_PIN_TOTAL, OUT_PIN, sizeof(OUT_PIN));
memcpy(OUT_PIN_TOTAL + sizeof(OUT_PIN), MCP_OUT_PIN, sizeof(MCP_OUT_PIN));
memcpy(IN_PIN_TOTAL, IN_PIN, sizeof(IN_PIN));
memcpy(IN_PIN_TOTAL + sizeof(IN_PIN), MCP_IN_PIN, sizeof(MCP_IN_PIN));
lcd.setCursor(0, 0);
lcd.print("PRESS START BUTTON");
lcd.setCursor(0, 1);
lcd.print("TO TEST NEW CABLE");
Serial.println("Press START button to test new cable");
}
//**********************************************************************************
void loop()
{
delay(300);
/* mcp.digitalWrite(MCP_LEDTOG1, HIGH);
// mcp.digitalWrite(MCP_LEDTOG2, LOW);
delay(300);
mcp.digitalWrite(MCP_LEDTOG1, LOW);
//mcp.digitalWrite(MCP_LEDTOG2, HIGH);
// Transfer input pin state to LED1
if (!mcp.digitalRead(MCP_INPUTPIN)) {
mcp.digitalWrite(MCP_LEDTOG2, HIGH);
} else {
mcp.digitalWrite(MCP_LEDTOG2, LOW);
}*/
buttonState = digitalRead(startButton); // read the pushbutton input pin
if (buttonState != lastButtonState) // compare the buttonState to its previous state
{
if (buttonState == LOW) // if the state has changed, proceed the functions
{
checkForFault = shortCableTest();
if (checkForFault >= 1)
{
fixTheError();
goto repeatTest;
}
checkForFault = wrongWiringTest();
if (checkForFault >= 1)
{
fixTheError();
goto repeatTest;
}
checkForFault= continuityTest();
if (checkForFault >= 1)
{
fixTheError();
goto repeatTest;
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GOOD JOB,CABLE IS OK");
lcd.setCursor(0, 2);
lcd.print("PRESS START BUTTON");
lcd.setCursor(0, 3);
lcd.print("TO TEST NEW CABLE");
Serial.println("GOOD JOB,CABLE IS OK");
Serial.println("Press START button to test new cable");
}
delay(50); // Delay a little bit to avoid bouncing
}
}
repeatTest:
lastButtonState = buttonState;
}
//**********************************************************************************
void fixTheError()
{
lcd.setCursor(0, 2);
lcd.print("FIX THE ERROR THEN");
lcd.setCursor(0, 3);
lcd.print("PRESS START BUTTON");
Serial.println();
Serial.println("Fix the error then press START button");
}
//**********************************************************************************
int shortCableTest()
{
boolean shortCablePrint = true;
int shortCableGroup = 0;
for (uint8_t i = 0; i < sizeof(OUT_PIN_TOTAL); i++)
{
int shortCable = 0;
for (uint8_t j = 0; j < sizeof(IN_PIN_TOTAL); j++)
{
if (!digitalRead(IN_PIN_TOTAL[j]) && shortCableOnce[j] == 0) //check if the combination of short cable has not alreday been taken
{
shortCableResult[j] = 1;
shortCable++;
}
}
if (shortCable < 2) //if no more than 2 outputs = 0 it means no shortcable
{
for (uint8_t j = 0; j < sizeof(IN_PIN_TOTAL); j++)
{
shortCableResult[j] = 0;
}
}
else if (shortCable > 1)
{
shortCableGroup++;
if (shortCableGroup > 1)
{
lcd.print(",");
}
for (uint8_t j = 0; j < sizeof(IN_PIN_TOTAL); j++)
{
if (shortCableResult[j] == 1)
{
shortCableOnce[j] = 1; //store all pins with short cable to an array
if (shortCablePrint)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SHORT CABLE ON PINS: ");
lcd.setCursor(0, 1);
Serial.print("SHORT CABLE ON PINS: ");
shortCablePrint = false;
}
lcd.print(j + 1);
Serial.print(j + 1);
lcd.print(" ");
Serial.print(" ");
shortCableResult[j] = 0;
}
}
}
nextI:
digitalWrite(OUT_PIN_TOTAL[i], HIGH);
for (uint8_t j = 0; j < sizeof(IN_PIN_TOTAL); j++)
{
shortCableOnce[j] = 0;
}
return shortCableGroup;
}
//**********************************************************************************
I've pasted only one rutin shortCableTest()
any help whould be very appriciated

