With posts spanning days, it is always best to assume the code has changed, especially when photos of the application do not match the source.
The program failure to retest a cable is due to variables not being re-initialized. The while loop is totally unnecessary. Here is a modified version that:
- Properly re-initializes the required variables
- Eliminates the while loop
- Eliminates the use of the String class
- Prepares the program for future expansion with the number of pins tested. This was more to remove the "magic number" "4" in every loop and array size declaration.
//Circuit tester sketch for Arduino Mega (Uno)
//(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
#define sizeTest 4
int endA[sizeTest] = {4, 5, 6, 7}; //pins end A
int endB[sizeTest] = {8, 9, 10, 11}; //pins endB
int pSwitch = A0;
int pEncA = A1;
int pEncB = A2;
//results
int result[sizeTest];
int test[sizeTest];
int counter[sizeTest];
bool fail;
void setup() {
Serial.begin(115200); //serial used for debugging only
lcd.begin(20, 4);
//setup pins
for (int i = 0; i < sizeTest; i++) {
pinMode(endA[i], OUTPUT);//set output pins (end A)
pinMode(endB[i], INPUT_PULLUP);//set input pins (end B)
}
pinMode(pSwitch, INPUT_PULLUP);
lcd.clear();
lcd.print("Cable Tester 4 Pinos");
lcd.setCursor(0, 1);
lcd.print("Press Enc p/ Inicio");
}
void loop() {
//run the test
if (!digitalRead(pSwitch)) {
runTest_4x1();
delay(200);
}
}
void runTest_4x1() {
// add space for null terminator
char s[sizeTest+1] = "0000";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Testando: 4 pinos");
lcd.setCursor(0, 1);
// re-initialize reqired variables
fail = false;
for (int i = 0; i < sizeTest; i++){
result[i] = -1;
test[i] = -1;
counter[i] = -1;
}
for (int i = 0; i < sizeTest; 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 < sizeTest; 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");
s[i] = '1';
}
else if (counter[i] == 3) {
Serial.println(" O");
s[i] = '0';
fail = true;
}
else {
Serial.println(" X");
s[i] = 'X';
fail = true;
}
}
lcd.print(s);
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");
}