Arduino Continuity Tester

Hello Everyone,
I need your help with something.
I'm trying to get arduino mega to do a continuity test on a cable, and i have 2 led diodes and a zebra GK420t printer connected. I used a code from this forum, and modified it a bit, but one thing I can not fix on my own so I'd appreciate it if you could help me. The loop should be like this: Test a cable, get an overall pass result, turn on the diodes, print a label.

#include <SoftwareSerial.h>

// 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, 4};
const byte pinsCableEnd[] = {3, 5};
const byte ledPins[] = {53, 51}; // Pins for LED indicators
const byte NUMCABLES = sizeof(pinsCableBegin);

// Define software serial RX and TX pins
const byte RX_PIN = 18; // RX pin of RS232 to TTL converter
const byte TX_PIN = 19; // TX pin of RS232 to TTL converter

SoftwareSerial printerSerial(RX_PIN, TX_PIN);

void setup() {
  Serial.begin(9600);
  printerSerial.begin(9600); // Initialize software serial for printer communication

  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();

  // Initialize LED pins as outputs
  for (byte i = 0; i < NUMCABLES; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

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;
  byte overallResult = PASS; // Track overall test 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);
      overallResult = FAIL_NOTCONNECTED; // Update overall result
    }
    // then check for continuity and OUTPUT/LOW
    digitalWrite(pinsCableBegin[i], LOW);
    if (digitalRead(pinsCableEnd[i]) != LOW) {
      bitSet(result, FAIL_NOTCONNECTED);
      overallResult = FAIL_NOTCONNECTED; // Update overall result
    }

    // 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);
        overallResult = FAIL_WRONGCONNECTED; // Update overall result
      }
    }
    // 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);
        overallResult = FAIL_SHORTENED; // Update overall result
      }
    }
    // Set LED according to test result
    digitalWrite(ledPins[i], (result == PASS) ? HIGH : LOW);

    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();
  }
  // If all lines pass, send print command to Zebra printer
  if (overallResult == PASS) {
    // Send ZPL command to the printer to print a label
    printerSerial.print("^XA"); // Start of label format
    printerSerial.print("^FO100,100^A0N,50,50^FDTest Passed^FS"); // Example ZPL command to print text
    printerSerial.print("^XZ"); // End of label format
    Serial.println("Printing label...");
  }
  else {
    Serial.println("Test did not pass on all lines, label not printed.");
  }
  Serial.println("Test finished.");
  Serial.println();
}

void loop() {
  DoOneTest();
  delay(1000); // Delay to allow observation of results before running the test again
}

My issue is that after it test the cable, if it's good, it keeps on running the test and printing me countless labels, when in reality I only need 1 label for 1 cable.
My requirement is something like this, test until it gets an overall pass result, then print one label, then test until i get a FAIL_BREAK overall result(Which means that the cable is detached) and then reset it to the first test and after pass get a label again.

Could you be kind enough to help me?

Make an array called something better than

bool knownGood[NUMCABLES];

which will be automatically initialised as all false.

When a cable comes up for testing, see if it is knownGood already, and skip testing and reporting.
If it is not knownGood, test it; if it is good, set that point in the array to true.

You can figure out when and whether to set the entire array elements all to false if you reset or rinse and repeat.

HTH

a7

This also means "test while I get a PASS" which then prints a label... that you do not want.

You need a state/flag to test for "test a cable" and "print a label"

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.