I played around with the code, I finally got it working with button press analog pin A0 (A1,A2.e.g)
My working code with button press, pretty straight forward. However, I'd like to set it automatic scan instead of press the button, is there a way to change?
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
const uint8_t i2cAddr = 0x27;
const int rs=0, rw=1, en=2, db4=4, db5=5, db6=6, db7=7, bl=3;
typeof(POSITIVE) blpol=POSITIVE; // backlight polarity level
LiquidCrystal_I2C lcd(i2cAddr, en, rw, rs, db4, db5, db6, db7, bl, blpol);
//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(16, 2);
//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");
lcd.setCursor(0, 1);
lcd.print("Press to Start");
}
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("Testing 4 Pin");
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;
}
else if (test[i] == 1 && j == i && result[i] < 8 ) { //Good, closed circuit
result[i] = 0;
}
digitalWrite(endA[j], LOW);
//debugging
/*
Serial.print("test 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("DEFECT");
lcd.print("DEFECT");
}
else {
Serial.println("CABLE OK");
lcd.print("CABLE OK");
}
Serial.println();
lcd.setCursor(0, 3);
lcd.print("Press to Start");
}