Go Down

Topic: Arduino mega continuity and short tester (Read 336 times) previous topic - next topic

Paul_l

#15
Feb 10, 2018, 01:14 pm Last Edit: Feb 12, 2018, 07:44 am by Paul_l
Tom, if you will can you please help me to convert this code from serial monitor to lcd print and add a button to start the program instead of writing "Test" in serial monitor?

You can remove this
Code: [Select]

  Serial.println();
  Serial.println("################################################");
  Serial.println("#                CABLE TESTER                  #");
  Serial.println("################################################");
  Serial.println();

and add something to fit the 16x2LCD

Code: [Select]

enum {PASS, FAIL_NOTCONNECTED, FAIL_WRONGCONNECTED, FAIL_SHORTENED };

// pin numbers for use at begin and end of cable
const byte pinsCableBegin[]= { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22 };
const byte pinsCableEnd[]  = {23, 24, 25, 26, 27, 28, 29, 30, 31, 31, 33, 34,35, 36, 37, 38, 29, 40, 41 };
const byte NUMCABLES=sizeof(pinsCableBegin);

void setup() {
  Serial.begin(9600);
  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();
}

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;
  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);
    }
    // then check for continuity and OUTPUT/LOW
    digitalWrite(pinsCableBegin[i], LOW);
    if (digitalRead(pinsCableEnd[i])!=LOW)
    {
        bitSet(result,FAIL_NOTCONNECTED);
    }
   
    // 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);
      } 
    }
    // 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);
      } 
    }
    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();
  }
  Serial.println("Test finished.");
  Serial.println();
}

void loop() {
  if (Serial.available())
  {
    DoOneTest();
    delay(20);
    while (Serial.available()) Serial.read(); // clear Serial input buffer
  }
}




TomGeorge

Hi,
Do you have an LCD display?
Is it I2C or parallel interface?
What do you want displayed on the LCD.

You have the advantage of having monitor print scrolling down the screen and being able to see all you printout.
With the LCD you are limited to 2 rows of 16 characters, you can scroll with the LCD, but you will need to store  your printout in character arrays, or some shorthand.

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Paul_l

Yes I have an LCD with I2C, on the LCD i want to display the test result like Test Pass or Test Fail, and if it is possible to be displayed on the second row the issue like this example Open at pin 10 or Short at pin 10.

I also attached 2 photos of my Mega and LCD with I2C adapter.

Thank you,
Paul Lungoci

Paul_l

#19
Feb 13, 2018, 01:16 pm Last Edit: Feb 13, 2018, 02:35 pm by Paul_l
I manage to make it work :)

Here is the final code :
Code: [Select]


#include <avr/wdt.h> //autoreset
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#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

int n = 1;

LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//pins
int ledPinPass = 52;
int ledPinFail = 12;
int endA[10] = {30,31,34,38,40,42,44,46}; //pins end A
int endB[10] = {32,33,35,39,41,43,45,47}; //pins endB
int pSwitch=53; //you can put here what you want pin X to gnd
int pEncA= 59;
int pEncB= 63;

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

void setup() {

  MCUSR = 0;  // clear out any flags of prior resets.
  Serial.begin(115200); //serial used for debugging only
 lcd.begin (16,2); //  <<----- My LCD was 16x2

 
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
 
  //setup pins 
  for(int i=0; i<8; 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_5x2();
}

void runTest_5x2(){
  String resultS="";
  //user interface
  lcd.clear();
  lcd.print(" tester ");
 
  lcd.setCursor(0,1);
  lcd.print(" test1 ");
 
  while(digitalRead(pSwitch))
  {
    delay(100);
   
  }
  delay(500); //debounce
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Cablu sub testare");
  delay(500);
  //lcd.setCursor(0,1);
  for(int i=0; i<8; i++){
     counter[i]=0;
    for(int j=0; j<8; j++){
        digitalWrite(endA[i], LOW); //set all outputs to LOW
    }
    for(int j=0; j<10; 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] = 20+j;
        }
        else if(test[i] == 1 && j==i && result[i] <20 ){ //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]);
      */
   
    //old
    /*
     Serial.print("Core ");
    Serial.print(i);
    Serial.print(" result = ");
    if(result[i] == 0){
       Serial.println(" 1");
       resultS+="1";
    }
    else if(counter[i] == 9){
       Serial.println(" O");
       resultS+="0";
       fail=true;
    }
    else {
       Serial.println(" X");
       resultS+="X";
       fail=true;
    }
     */
         }
    //new
    delay(250); //delay between tests
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Testare grup= "); //group test
    lcd.print(i);
   // lcd.setCursor(0,1);
   // lcd.print("result =");
    if(result[i] == 0){
    lcd.print(" 1");
       resultS+="1";
    }
    else if(counter[i] == 9){
      lcd.setCursor(0,1);
       lcd.print("Open grup "); //open at group X
       lcd.print(i);
       delay(1000);
    //   lcd.setCursor(4,1);
       resultS+="0";
       fail=true;
     
    }
   
    else {
      lcd.setCursor(0,1);
       lcd.print("FireInvers/scurt"); //short or cross wire
       delay(1000);
       resultS+="X";
       fail=true;
       
    }
  }
 
  //lcd.print(resultS);
  lcd.setCursor(0,0);
 delay(1000);
 if(fail){
  lcd.clear();
   Serial.println("     FAILED");
   lcd.print(" Cablul defect");
   pinMode(ledPinFail, OUTPUT);
   digitalWrite(ledPinFail, HIGH);
 }
 else{
 
  lcd.clear();
   Serial.println("     PASSED");
   lcd.print("Cablul este bun");
   pinMode(ledPinPass, OUTPUT);
   digitalWrite(ledPinPass, HIGH);
   
 }
 Serial.println();
 lcd.setCursor(0,2);
 lcd.print(" Reset  automat");
  // while(digitalRead(pSwitch))
  {
    delay(300);
  }
  delay(700); //debounce
  //autoreset
  wdt_enable(WDTO_15MS); // turn on the WatchDog and don't stroke it.
for(;;) {

}
}



If anyone find some bugs or improves please do not be shy and post it here :)

larryd

No technical PMs.
The last thing you did is where you should start looking.

TomGeorge

#21
Feb 13, 2018, 10:25 pm Last Edit: Feb 13, 2018, 10:26 pm by TomGeorge
Hi,
Good stuff mate.. looks good..  :) :) :)


Tom. :)
Everything runs on smoke, let the smoke out, it stops running....

Paul_l

Congrats!

Thank you!

Hi,
Good stuff mate.. looks good..  :) :) :)

Tom. :)
Thank you!

I merge i think 3-4 cable tester codes and after aprox. 8 hours it is working like I wanted to:)

Thank you for your support!

Best regards,
Paul L.

Go Up