Hey,
After receiving a lot of help on this project offer the past few weeks, i would like to start by saying that the guys on this forum have assisted me a lot and i would have not got to the point i am at not. So a huge thank you!
It feels like i am now at the last hurdle and I seem to be staring at the screen, scrolling through the code and not knowing whats wrong...
The code below, checks a cable to see what type it is, sets how many pins it should be checking for and then sees if the cable passes its test. The code works fine if the cable type matches. But if it does not it misses checking Pin 2.
Any help would be much appreciated
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
String Line1;
String Line2;
String Line3;
String Line4;
uint8_t outPins[] = {2, 0, 4, 5, 6};// Output pins
uint8_t inpPins[] = {7, 0, 9, 10, 11};// Input pins
uint8_t outConnectorPins[] = {26, 28, 30, 32, 34 ,36 ,38, 40, 42, 44, 46, 48, 50};// Check what cable is conencted Pins MALE
uint8_t inConnectorPins[] = {27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51};// Check what cable is conencted Pins FEMALE
int pinsToCheck = 0;
int run;
int inputPinAmount = 0;
int outputPinAmount = 0;
const int sensePin = 2; // the number of the pushbutton pin
const int readPin = 7; // the number of the pushbutton pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int buttonState = 0; // variable for reading the pushbutton status
int lastButtonState = 0;
// LCD SCROLLING SETUP //
void lcdPrint(String lcdText)
{
lcd.clear();
Line1 = Line2;
lcd.setCursor(4, 0);
lcd.print(Line1);
Line2 = Line3;
lcd.setCursor(4, 1);
lcd.print(Line2);
Line3 = Line4;
lcd.setCursor(4, 2);
lcd.print(Line3);
Line4 = lcdText;
Line4.replace("\r\n", "");
lcd.setCursor(4, 3);
lcd.print(Line4);
Line1 = "";
lcdText = "";
}
// LCD SCROLLING SETUP //
void setup()
{
Serial.begin(115200);
lcd.begin();
lcd.backlight();
lcd.setCursor(4,2);
lcd.print("Cable Tester");
delay(1000);
pinMode(readPin, INPUT); //set the button pin as INPUT
pinMode(sensePin, OUTPUT); //set the LED pin as OUTPUT
}
void loop()
{
pinMode(sensePin,OUTPUT);
digitalWrite(sensePin, HIGH); // Set Output Sensor Pin to High
pinMode(readPin,INPUT);
int buttonState = digitalRead(readPin); //read the state of Input Sensor Pin
if(lastButtonState != buttonState)
{
if(buttonState == 1) // Cable has been connected
{
lastButtonState = 1;
cableConnected();
}
else
{
lastButtonState = 0; // Cable has been Disconnected
cableDisonnected();
}
}
}
void cableConnected()
{
lcd.clear();
lcd.setCursor(2,1);
lcd.print("Cable Connected");
delay(500);
lcd.clear();
checkWhatCable();
}
void checkWhatCable()
{
lcd.clear();
lcdPrint("");
lcdPrint("");
lcdPrint("");
lcdPrint("");
int femalePin;
int malePin;
for (uint8_t m = 0; m < sizeof(outConnectorPins); m++) // Setup output pins
{
pinMode(outConnectorPins[m],OUTPUT);
digitalWrite(outConnectorPins[m],LOW);
}
for (uint8_t f = 0; f < sizeof(inConnectorPins); f++) // Setup input pins
{
pinMode(inConnectorPins[f],INPUT); // Turn off internap pullup resistor
}
//////////////////////////////////////////////////////////////////////////////////////
for (uint8_t o = 0; o < sizeof(outConnectorPins); o++) // Setup Female input pins
{
digitalWrite(outConnectorPins[o],HIGH); // Set pin high
for (uint8_t i = 0; i < sizeof(inConnectorPins); i++)// Scan input pins
{
if (digitalRead(inConnectorPins[i]) == HIGH) // If pin is high must be connected to output
{
femalePin = inConnectorPins[o]; // input
inpPins[1] = femalePin;
lcd.setCursor(4,0);
if (femalePin == 27)
{
lcd.print("16A 1 Phase");
inputPinAmount = 3;
}
else if (femalePin == 29)
{
lcd.print("32A 1 Phase");
inputPinAmount = 3;
}
/////////////////////////////////////////////////////
malePin = (outConnectorPins[i]); // Input
outPins[1] = malePin;
lcd.setCursor(4,2);
if (malePin == 26)
{
lcd.print("16A 1 Phase");
outputPinAmount = 3;
}
else if (malePin == 28)
{
lcd.print("32A 1 Phase");
outputPinAmount = 3;
}
delay(2000);
}
}
digitalWrite(outConnectorPins[o],LOW); // Set output back to low
}
setPinModes();
}
void setPinModes()
{
for (uint8_t i = 0; i < sizeof(outPins); i++) // Setup output pins
{
pinMode(outPins[i],OUTPUT);
digitalWrite(i,LOW);
}
for (uint8_t i = 0; i < sizeof(inpPins); i++) // Setup input pins
{
pinMode(inpPins[i],INPUT); // Turn off internap pullup resistor
}
Serial.println("Begin Test");
if (testCable() == true)
{
lcdPrint("** Passed **");
Serial.println("** Passed **");
}
else
{
lcdPrint("** Failed **");
Serial.println("** Failed **");
}
}
bool testCable()
{
lcd.clear();
if (inputPinAmount > outputPinAmount)
{
pinsToCheck = inputPinAmount;
}
else
{
pinsToCheck = outputPinAmount;
}
bool testResult = true; // Assume cable is good
uint8_t count = 0; // Count of correct connections
if (sizeof(outPins) != sizeof(inpPins)) // Array sizes match?
{
Serial.println(F("ERROR: Array Sizes Do Not Match."));
return false; // Exit now
}
for (uint8_t o = 0; o < sizeof(outPins); o++)// Loop though output pins
{
Serial.print("Pin \t");
Serial.print(o);
digitalWrite(outPins[o],HIGH); // Set pin high
if (digitalRead(inpPins[o]) == HIGH)// Everything is OKAY that pin works
{
count++; // Increment count
Serial.print("->"); // Display it as first pin
Serial.print(o); // Display it as first pin
int a = (o +1);
String myStr;
myStr=String(a);
lcdPrint(myStr + " -> " + myStr);
delay(500);
}
// If there is issues with the cable test, see what cables are crossed///
for (uint8_t i = 0; i < sizeof(inpPins); i++)// Scan input pins
{
if (digitalRead(inpPins[i]) == HIGH) // If pin is high must be connected to output
{
if (o != i) // If array numbers don't match then must be error <--------------------ERROR
{
Serial.print(" X ");
Serial.print(i +1);
//testResult = false; // Somethings Wrong show Error
int a = (o +1); //Output
String myStr;
int b = (i +1); // Input
String myStr2;
myStr=String(a); //Output
myStr2=String(b); // Input
lcdPrint(myStr + " X " + myStr2); // Show Errors
delay(500);
}
}
}
digitalWrite(outPins[o],LOW); // Set output back to low
Serial.println(); // New line ready for next pin
}
if (count < pinsToCheck) // Have we had the correct number of correct pins?
{
testResult = false; // If not then must be error
}
else
{
testResult = true;
}
return testResult;
}
void cableDisonnected()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Cable Disconnected");
}