Hi [@TomGeorge,
This thread was 2 years old back you were helping Sam on this sketch, I have a Mega with 16x2 i2c.
I want to try this code to see if it works.
Thanks
(sorry for edit this post)
//Circuit tester sketch for Arduino Mega (Uno)
//(c) Samuel Silva
//LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
// 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");
lcd.setCursor(0, 1);
lcd.print("Press Enc p/ 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 Enc P/ Start");
}