IDC Cable tester with 20x4 LCD

Hi there !
I’m making my first Arduino Uno project which to do a simple IDC 4 pin cable tester.
After reading some info on the internet, i found a good base to start and made a few modifications. I have changed the LCD and the number of pins and made some modifications on the code to suit my needs,
The program runs OK until it finds a faulty cable. Then, unfortunately, is not able to test it successfully again, only after a reset is able to test the cable correctly.
I’m comfortable with electronics but I’m not a programmer… so, something is missing here and i believe is when it starts the loop which is not reset the counters.

Many thanks.

Here’s the code:

//Circuit tester sketch for Arduino Mega
//(c) Samuel Silva 
// Many thanks to Tony since this project contains 95% of its code 
//GPLv3

//LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
                          // Address can be changed by soldering A0, A1, or A2
                          // Default is 0x27

// map the pin configuration of LCD backpack for the LiquidCristal class
#define BACKLIGHT_PIN 3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C lcd(I2C_ADDR,
                      En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,
                      BACKLIGHT_PIN, POSITIVE);
//pins
int endA[4] = {4,5,6,7}; //pins end A
int endB[4] = {8,9,10,11}; //pins endB
int pSwitch=A0;
int pEncA= A1;
int pEncB= A2;


//results
int result[4] = {-1,-1,-1,-1};
int test[4] = {-1,-1,-1,-1};
int counter[4] = {-1,-1,-1,-1,};
bool fail =false;

void setup() {
  
   Serial.begin(115200); //serial used for debugging only
  lcd.begin(20, 4);
  
  //setup pins  
  for(int i=0; i<4; i++){
      pinMode(endA[i], OUTPUT);//set output pins (end A)
      pinMode(endB[i], INPUT_PULLUP);//set input pins (end B)
  }
  pinMode(pSwitch,INPUT_PULLUP);

}

void loop() {
  //run the test
  
  runTest_4x1();
 
}

void runTest_4x1(){
  
  String resultS="";
  //user interface
  lcd.clear();
  lcd.print("Cable Tester: 4 Pins"); 
  lcd.setCursor(0,1);
  lcd.print("Press Enc to Start");

while(digitalRead(pSwitch))
  {
    delay(100);
  }
  delay(700); //debounce
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Testing: 4 pins");
  lcd.setCursor(0,1);
  for(int i=0; i<4; i++){
     counter[i]=0;
    for(int j=0; j<4; j++){
        digitalWrite(endA[i], LOW); //set all outputs to LOW
    }
    for(int j=0; j<4; j++){  //check for crossed / open circuits vs closed, good, circuits
        digitalWrite(endA[j], HIGH); //scan across the inputs in turn
        test[i] = digitalRead(endB[i]); //read the output
        if(test[i] == 1 && j!=i){ //crossed or open circuit
          counter[i]++;
          result[i] = 8+j; 
        }
        else if(test[i] == 1 && j==i && result[i] <8 ){ //Good, closed circuit
          result[i] = 0; 
        }
        digitalWrite(endA[j],LOW);
        //debugging
        /*
          Serial.print("test1 input core  ");
          Serial.print(i);
          Serial.print(" with output core  ");
          Serial.print(j);
          Serial.print(" test =");
          Serial.print(test[i]);
          Serial.print(" counter =");
          Serial.println(counter[i]);*/
    }
    Serial.print("Core ");
    Serial.print(i);
    Serial.print(" result = ");
    if(result[i] == 0){
       Serial.println(" 1");
       resultS+="1";
    }
    else if(counter[i] == 3){
       Serial.println(" O");
       resultS+="0";
       fail=true;
    }
    else {
       Serial.println(" X");
       resultS+="X";
       fail=true;
    }
  }
  
  lcd.print(resultS); 
  lcd.setCursor(0,2);
 
 if(fail){
   Serial.println("FAILLED");
   lcd.print("FAILLED");
 }
 else{
   Serial.println("CABLE OK");
   lcd.print("CABLE OK");
 }
 Serial.println();
 lcd.setCursor(0,3);
 lcd.print("Press Enc to Start");
   while(digitalRead(pSwitch));
   
  {
    delay(100);
  }
  delay(700); //debounce

}

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)

Hi TomGeorge,

Sorry about the code, I’ve corrected it.

This is a wire continuity check. It will check if the Arduino Pin 4 is connected to 8, if the pin 5 is connected to 9 and so on.
It is also showing if the circuit is Open (not conected) by showing “0”, or if there is a cross wire by showing “X”.
If the cable is OK, it will show “1 1 1 1”. If there is a crosswire between the first and the second pins, it will show “X X 1 1”. If it’s open on the fourth pin it wil show “1 1 1 0”.

The program runs OK when the cable is correct. When the cable is wrong, it will change its status accordingly if its open “0”, or crossed wire"X", but if i correct the cable the program does not shows the “1” and is stuck on the pins that were initially with defect.
If i reset the Arduino, the test is successful and shows the “1” on all pins.

Example IDC.png

|500x195

Please show ALL your code, not just a portion of it.

Hi, Yes, we need to see all your code please.

The program runs OK when the cable is correct. When the cable is wrong, it will change its status accordingly if its open "0", or crossed wire"X", but if i correct the cable the program does not shows the "1" and is stuck on the pins that were initially with defect.

You need to re- initialize ALL your variables before recommencing the test. I cringe when I see a while loop, is it absolutely necessary?

Tom.... :)

Sorry... the code was trimmed... I've corrected it.

I tried to reset the variables already but i don't know if i did it correctly. At the end of the loop, i wrote "int i=0;" and "int j=0"; also, but it didn't worked.

define Rw_pin 1

define Rs_pin 0

You do know pin D0 and D1 are for serial connection on your UNO.

larryd:

define Rw_pin 1

define Rs_pin 0

You do know pin D0 and D1 are for serial connection on you UNO.

I've made the same mistake, they are the pin allocations on the I2C adapter PCB on the LCD display. Tom.. :)

Always nice to see a schematic along with the sketch.

I tried to reset the variables already but i don't know if i did it correctly. At the end of the loop, i wrote "int i=0;" and "int j=0"; also, but it didn't worked.

It’s difficult to explain with certainty since we cannot see what the code described above actually was. But, one that is certain is that you you have not yet encountered the concept of variable scope. Please have a read:

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

What you did in the quote above was to create two new integers, named i and j, and initialed them to zero, you did not reset any variables. It appears the only placed you used i and j are are in the for loops. Variables used in for loops are automatically initialized when the statement is executed.

I agree with TG, that while loop is incorrect, if the switch is pressed, the test should run once and then wait until it is pressed again.

I believe that the while loop is to avoid having a continuous loop. I do not want it to happen. It will only restart after pressing the switch button that i’ve paced at Pin A0.
I have attached a picture describing the problem. Hope it helps.

If you want help, you must post the actual code that was used when pictures were taken. Looking at yesterday’s code while describing today’s problem is a waste of everyone’s time.

Hi, I don't see why you need to stop the process, just let it run continuously and keep the results live as you connect and disconnect the cable.

Also when you display, do you blank out the old results, or just try and overwrite them?

Tom... :)

Hi, In your code in post#1 this;

 while (digitalRead(pSwitch))
  {
    delay(100);
  }

Does nothing but add a 100ms delay in the loop.

When you post the code you are using now, please post it as a new post, please do not go and edit the code in post#1.

Thanks.. Tom... :)

I hate using the ‘while’ loop.

larryd: I hate using the ‘while’ loop.

I avoid it like the plague, it is the least function I think of when writing code.

I prefer to use an If or switch and flags.

Tom... :)

Even lower on my list is the

do { } while() ;

construct. Yes, there are times you should use it but I think must subconsciously avoid it.

This is the only time I use ‘while’.

    while ( currentMillis - millisOne >= delayTime )
    {
      millisOne = millisOne + delayTime;
    }

From discussion: http://forum.arduino.cc/index.php?topic=396591.10;

WattsThat:
If you want help, you must post the actual code that was used when pictures were taken. Looking at yesterday’s code while describing today’s problem is a waste of everyone’s time.

The code itself, its unchanged. What is printed on the screen does not affect the main code. I did the translation to English so most of the people understand it. But if you prefer in Portuguese, i can do that also.
Here’s the code:

//Circuit tester sketch for Arduino Mega
//(c) Samuel Silva 
// Many thanks to Tony since this project contains 95% of its code 
//GPLv3

//LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
                          // Address can be changed by soldering A0, A1, or A2
                          // Default is 0x27

// map the pin configuration of LCD backpack for the LiquidCristal class
#define BACKLIGHT_PIN 3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C lcd(I2C_ADDR,
                      En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,
                      BACKLIGHT_PIN, POSITIVE);
//pins
int endA[4] = {4,5,6,7}; //pins end A
int endB[4] = {8,9,10,11}; //pins endB
int pSwitch=A0;
int pEncA= A1;
int pEncB= A2;


//results
int result[4] = {-1,-1,-1,-1};
int test[4] = {-1,-1,-1,-1};
int counter[4] = {-1,-1,-1,-1,};
bool fail =false;

void setup() {
  
   Serial.begin(115200); //serial used for debugging only
  lcd.begin(20, 4);
  
  //setup pins  
  for(int i=0; i<4; i++){
      pinMode(endA[i], OUTPUT);//set output pins (end A)
      pinMode(endB[i], INPUT_PULLUP);//set input pins (end B)
  }
  pinMode(pSwitch,INPUT_PULLUP);

}

void loop() {
  //run the test
  
  runTest_4x1();
 
}

void runTest_4x1(){
  
  String resultS="";
  //user interface
  lcd.clear();
  lcd.print("Cable Tester 4 Pinos"); 
  lcd.setCursor(0,1);
  lcd.print("Press Enc p/ Inicio");
while(digitalRead(pSwitch))
  {
    delay(100);
  }
  delay(700); //debounce
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Testando: 4 pinos");
  lcd.setCursor(0,1);
  for(int i=0; i<4; i++){
     counter[i]=0;
    for(int j=0; j<4; j++){
        digitalWrite(endA[i], LOW); //set all outputs to LOW
    }
    for(int j=0; j<4; j++){  //check for crossed / open circuits vs closed, good, circuits
        digitalWrite(endA[j], HIGH); //scan across the inputs in turn
        test[i] = digitalRead(endB[i]); //read the output
        if(test[i] == 1 && j!=i){ //crossed or open circuit
          counter[i]++;
          result[i] = 8+j; //alterei de 8 para 2 e não deu
        }
        else if(test[i] == 1 && j==i && result[i] <8 ){ //Good, closed circuit
          result[i] = 0; 
        }
        digitalWrite(endA[j],LOW);
        //debugging
        /*
          Serial.print("test1 input core  ");
          Serial.print(i);
          Serial.print(" with output core  ");
          Serial.print(j);
          Serial.print(" test =");
          Serial.print(test[i]);
          Serial.print(" counter =");
          Serial.println(counter[i]);*/
    }
    Serial.print("Core ");
    Serial.print(i);
    Serial.print(" result = ");
    if(result[i] == 0){
       Serial.println(" 1");
       resultS+="1";
    }
    else if(counter[i] == 3){
       Serial.println(" O");
       resultS+="0";
       fail=true;
    }
    else {
       Serial.println(" X");
       resultS+="X";
       fail=true;
    }
  }
  
  lcd.print(resultS); 
  lcd.setCursor(0,2);
 
 if(fail){
   Serial.println("DEFEITO");
   lcd.print("DEFEITO");
 }
 else{
   Serial.println("CABO OK");
   lcd.print("CABO OK");
 }
 Serial.println();
 lcd.setCursor(0,3);
 lcd.print("Press Enc P/ Inicio");
   while(digitalRead(pSwitch));
     {
    delay(100);
  }
  delay(700); //debounce

}

I will remove the while function and let it run without any switch to see what happens.