CLOSED - SOLVED - Need help with a simple comparison problem - SOLVED - CLOSED

Hello all,

My name is Trent and I am having some trouble. I am not new to electronics, I am however, very new to coding.

I am trying to use an Arduino make several checks on some parts I am making in bulk.

I need to check and ensure the following on my part is true for the part to pass inspection.

Pins 1 and 4 are shorted
Pins 2 and 5 are shorted
Pins 1 and 2 are open
Pins 1 and 3 are open
Pins 1 and 5 are open*
Pins 2 and 3 are open
Pins 2 and 4 are open*
Pins 3 and 4 are open*
Pins 3 and 5 are open*
Pins 4 and 5 are open
*I believe these are redundant if all the other checks pass.

As is my led lights up regardless if it passes or not, what am I doing wrong?

//Program to check large volume of parts.
//A good part will have the following conditions met:
//Pins 1 and 4 shorted
//Pins 2 and 5 shorted
//Pins 1 and 2 open
//Pins 1 and 3 open
//Pins 2 and 3 open
//Pins 4 and 5 open

//The parts will fit into a fixture wired to mcu as follows:
//Pin 1 is wired to Arduino pin 13 with a 1Mohm resistor to ground as a pull down resistor.
//Pin 2 is wired to Arduino pin 12 with a 1Mohm resistor to ground as a pull down resistor.
//Pin 3 is wired to Arduino pin 11 with a 1Mohm resistor to ground as a pull down resistor.
//Pin 4 is wired to Arduino pin 10 with a 1Mohm resistor to ground as a pull down resistor.
//Pin 5 is wired to Arduino pin  9 with a 1Mohm resistor to ground as a pull down resistor.

//The following are supporting connections.
//LED anode to Arduino pin 3 and cathode to ground.
//momentary pushbutton switch connected between 5v and Arduino pin 2, with a 1Mohm pulldown resistor between arduino pin 2 and ground.
//----------------------------------------------------------------------------------------------------------------------------------------------------  
  
  void setup(){
  
pinMode(13,INPUT); // Pin 1
pinMode(12,INPUT); //Pin 2
pinMode(11,INPUT); //Pin 3
pinMode(10,INPUT); //Pin 4 coax center
pinMode(9,INPUT); //Pin 5 coax shield

}

void loop(){

  while (digitalRead(2) == LOW) { //This it to wait until button is pressed.
    digitalWrite(3, LOW); //Make sure pass LED is off
  }
  while (digitalRead(2) == HIGH) { //This occurs while button is held.
      //int pin1 = digitalRead(13);
      //int pin2 = digitalRead(12);
      //int pin3 = digitalRead(11);
      //int pin4 = digitalRead(10);
      //int pin5 = digitalRead(9);
      pinMode(3, OUTPUT); //LED
      digitalWrite(3, LOW); //LED Off
      pinMode(13, OUTPUT); //Pin 1
      digitalWrite(13, HIGH); //Pin 1
      pinMode(12, OUTPUT); //Pin 2
      digitalWrite(12, HIGH); //Pin 2
      if ( (digitalRead(13) == digitalRead(10)) && (digitalRead(12) == digitalRead(9)) ) 
        digitalWrite(12, LOW); //Pin 2
        pinMode(12,INPUT);//Pin 2
        if ( (digitalRead(13) != digitalRead(12)) && (digitalRead(13) != digitalRead(11)) )
          digitalWrite(13, LOW); //Pin 1
          pinMode(13,INPUT); //Pin 1
          pinMode(12, OUTPUT); //Pin 2
          digitalWrite(12, HIGH); //Pin 2
          if (digitalRead(12) != digitalRead(11))
            digitalWrite(3, HIGH);
            else digitalWrite(3, LOW);
            delay(1500);            
//              loop();
          
          
  }
//  else
//    loop();

}

Your posting makes no sense at all. What do you mean pins are shorted? To ground, to +5, or together? Why would you short pins together? IF open, they are floating and can read either 0 or 1. Perhaps a schematic drawing would help.

Paul

Thank you for your reply Paul. I tried to be clear but I guess it got confusing. I mean shorted together and open between each other when I say short and open. It will be more clear if you saw the part I'm testing, so I will upload a photo.

Pins 1, 2, and 3 are on the orange molded piece. Pin 4 is the center pin on the coax F connector and pin 5 is the shielded outside of the coax F connector.

I just need to make sure there is a connection where there should be, and not a connection where there shouldn't be.

It needs to be an easy press a button and get a "pass" light so that I can give this to production workers to use. (I was originally going to make this out of logic gates....starting to wish I had)

If you have any questions, feel free to ask.

Thanks again,

Trent

EDIT:
I thought more pictures might help. Here is a picture of my schematic, test fixture, and preliminary wiring to Arduino.

Hi,
Got a bit lost in your code there...
I'd suggest something like this to make it easier to understand.
For each of your pins, drive one pin and read all of them.

// Define flags in binary progression for all pins
//
#define PINFLAG1  1
#define PINFLAG2  2
#define PINFLAG3  4
#define PINFLAG4  8
#define PINFLAG5  16

// Define all pin numbers
#define PIN1 13
#define PIN2 12
#define PIN3 11
#define PIN4 10
#define PIN5 19

// Function to test pins and return accumulated flags
// passed: pin to drive HIGH to do test
//
int testPins( uint8_t drivePin )
{
  result= 0;

  pinMode( PIN1, INPUT );
  pinMode( PIN2, INPUT );
  pinMode( PIN3, INPUT );
  pinMode( PIN4, INPUT );
  pinMode( PIN5, INPUT );

  pinMode( drivePin, OUTPUT );
  digitalWrite( drivePin, HIGH );

  delay(10);   // allow pins to settle before reading

  if( digitalRead(PIN1) == HIGH )
    result= result+PINFLAG1;
  if( digitalRead(PIN2) == HIGH )
    result= result+PINFLAG2;
  if( digitalRead(PIN3) == HIGH )
    result= result+PINFLAG3;
  if( digitalRead(PIN4) == HIGH )
    result= result+PINFLAG4;
  if( digitalRead(PIN5) == HIGH )
    result= result+PINFLAG5;

  return result;
}


void loop()
{

  // Example tests
  int testPasses= 0;

  // driving PIN1 high, we expect PIN1 and PIN4 to be high, the rest low:
  //
  if( testPins( PIN1 ) == (PINFLAG1 + PINFLAG4) )
    testPasses++;

  if( testPins( PIN2 ) == (PINFLAG2 + PINFLAG5) )
    testPasses++;


  // and so on...
}

Note: I just typed this in, not a complete sketch, not compiled, not run, not tested.

Yours,
TonyWilk

Why not read all the Arduino pins and save their values into variables that reflect the names of the pins on your device. For example

devicePin1State = digitalRead(13);
devicsPin2State = digitalRead(12);
// etc

Then you can use some simple tests to check the combinations of the saved values.

The program would be even easier to follow if you define your Arduino pins like this

byte devicePin1 = 13;
byte devicePin2 = 12;
// etc
// and
devicePin1State = digitalRead(devicePin1);
devicsPin2State = digitalRead(devicePin2);
// etc

...R

The program would be even easier to follow if you define your Arduino pins like this

But, since you have two distinct ends, don't name all 5 pins devicePinN.

PaulS:
But, since you have two distinct ends, don't name all 5 pins devicePinN.

What?

There are only 5 pins that I can see.

Yours,
TonyWilk

TonyWilk:
What?

There are only 5 pins that I can see.

Yours,
TonyWilk

The picture in reply 2 shows a component with 2 ends. One end has three pins. One end has two pins. That's 5, so you don't have to take your shoes off.

I don't know what your problem is with my post. I simply want the OP to name the pins something like yellowPin1, yellowPin2, yellowPin3, coaxPin1, and coaxPin2, rather than devicePin1, devicePin2, devicePin3, devicePin4, and devicePin5.

PaulS:
I don't know what your problem is with my post. I simply want the OP to name the pins something like yellowPin1, yellowPin2, yellowPin3, coaxPin1, and coaxPin2, rather than devicePin1, devicePin2, devicePin3, devicePin4, and devicePin5.

The problem was the lack of the explanation/suggestion you have now given.

Yeh, makes sense.

Yours,
TonyWilk

I'd suggest making the sketch data driven. Have an array of pins and a two dimensional array that tells you which ones should be connected and which should not. Then just loop through the combinations with two nested for loops to verify that the cable meets spec.

As a bonus, you could set it up to check several different cables with some kind of selector switch by simply adding more data.

Tony, I am trying to follow your suggestion, but how does the program know which pin to make "drivePin"? from what I can tell it isn't specified. Keeping in mind, that I have extremely little coding experience.

wildbill, I really like your suggestion, but don't even have a clue of where to start on that approach.

Thanks again for your suggestions thus far,

Trent

EDIT:
I was trying to upload an edited version of your code (below) and I got the following error:

exit status 1
Error compiling for board Arduino/Genuino Uno.

Any idea what caused that?

// Define flags in binary progression for all pins
//
#define PINFLAG1  1
#define PINFLAG2  2
#define PINFLAG3  4
#define PINFLAG4  8
#define PINFLAG5  16

// Define all pin numbers
#define PIN1 13
#define PIN2 12
#define PIN3 11
#define PIN4 10
#define PIN5 9
#define LED 4

// Function to test pins and return accumulated flags
// passed: pin to drive HIGH to do test
//
int testPins( uint8_t drivePin )
{
  int result = 0;

  pinMode( PIN1, INPUT );
  pinMode( PIN2, INPUT );
  pinMode( PIN3, INPUT );
  pinMode( PIN4, INPUT );
  pinMode( PIN5, INPUT );

  pinMode( drivePin, OUTPUT );
  digitalWrite( drivePin, HIGH );

  delay(10);   // allow pins to settle before reading

  if ( digitalRead(PIN1) == HIGH )
    result = result + PINFLAG1;
  if ( digitalRead(PIN2) == HIGH )
    result = result + PINFLAG2;
  if ( digitalRead(PIN3) == HIGH )
    result = result + PINFLAG3;
  if ( digitalRead(PIN4) == HIGH )
    result = result + PINFLAG4;
  if ( digitalRead(PIN5) == HIGH )
    result = result + PINFLAG5;

  return result;
}


void loop()
{

  // Example tests
  int testPasses = 0;

  // driving PIN1 high, we expect PIN1 and PIN4 to be high, the rest low:
  //
  if ( testPins( PIN1 ) == (PINFLAG1 + PINFLAG4) )
    testPasses++;
  delay(5);

  if ( testPins( PIN2 ) == (PINFLAG2 + PINFLAG5) )
    testPasses++;
  delay(5);

  if ( testPins( PIN3 ) == (PINFLAG3) )
    testPasses++;
  delay(5);

  if ( testPasses == 3 )
  {
    pinMode( LED, OUTPUT );
    digitalWrite( LED, HIGH );
  }
  else
  {
    pinMode( LED, OUTPUT );
    digitalWrite( LED, LOW );
  }

  // and so on...
}

nodusdesign:
Tony, I am trying to follow your suggestion, but how does the program know which pin to make "drivePin"? from what I can tell it isn't specified. Keeping in mind, that I have extremely little coding experience.

The definition of the function:

int testPins( uint8_t drivePin )

just says you are going to pass a value which we are going to call 'drivePin' inside the function.

So, in the same way you use digitalRead( PIN1 ), you call testPins( PIN1 )

Inside the function drivePin is PIN1

When you call the function with a different parameter like: testPins( PIN2 ) then drivePin is PIN2

Here's a complete sketch of that code (it does compile and run)

// Define flags in binary progression for all pins
//
#define PINFLAG1  1
#define PINFLAG2  2
#define PINFLAG3  4
#define PINFLAG4  8
#define PINFLAG5  16

// Define all pin numbers
#define TESTPIN1 13
#define TESTPIN2 12
#define TESTPIN3 11
#define TESTPIN4 10
#define TESTPIN5 19

void setup()
{
  Serial.begin( 9600 );
}

// Function to test pins and return accumulated flags
// passed: pin to drive HIGH to do test
//
int testPins( uint8_t drivePin )
{
  int result= 0;

  pinMode( TESTPIN1, INPUT );
  pinMode( TESTPIN2, INPUT );
  pinMode( TESTPIN3, INPUT );
  pinMode( TESTPIN4, INPUT );
  pinMode( TESTPIN5, INPUT );

  pinMode( drivePin, OUTPUT );
  digitalWrite( drivePin, HIGH );

  delay(10);   // allow pins to settle before reading

  if( digitalRead(TESTPIN1) == HIGH )
    result= result+PINFLAG1;
  if( digitalRead(TESTPIN2) == HIGH )
    result= result+PINFLAG2;
  if( digitalRead(TESTPIN3) == HIGH )
    result= result+PINFLAG3;
  if( digitalRead(TESTPIN4) == HIGH )
    result= result+PINFLAG4;
  if( digitalRead(TESTPIN5) == HIGH )
    result= result+PINFLAG5;

  return result;
}


void loop()
{
  Serial.println("Testing...");

  // Example tests
  int testPasses= 0;

  // driving TESTPIN1 high, we expect TESTPIN1 and TESTPIN4 to be high, the rest low:
  //
  if( testPins( TESTPIN1 ) == (PINFLAG1 + PINFLAG4) )
    testPasses++;

  // pin2 shorted to pin5
  //
  if( testPins( TESTPIN2 ) == (PINFLAG2 + PINFLAG5) )
    testPasses++;

  // pin3 not connected to any other pin
  //
  if( testPins( TESTPIN3 ) == (PINFLAG3) )
    testPasses++;

  Serial.print("testPasses= ");
  Serial.println( testPasses );
  
  delay( 2000 );
}

Use the Serial monitor to see the messages.

Yours,
TonyWilk

Tony,
Thank you, this compiles and flashes to my Arduino. Just by reading through your code (after your explanation) it looks like it should work for what I need. However in practice I get some odd results. I will post 3 different scenarios and see what you think. (I am completely bypassing my test fixture for now to reduce variables that could cause these errors)

Scenario 1: Nothing connected at all to Arduino pins:

Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0

as expected

Scenario 2: Arduino pins 13 and 10 have a jumper between them and Arduino pins 12 and 9 have a jumper between them.

Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 1
Testing...
testPasses= 1
Testing...
testPasses= 1
Testing...
testPasses= 1
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 0
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 1

Which seems odd to me.

Scenario 3: There are 1Mohm pull down resistors between each of the pins used (13,12,11,10,9) and ground and (on the pin side of the resistor, not the ground side) I have a jumper between pins 13 and 10, and a jumper between pins 12 and 9. Ill upload a picture to clarify this one.

Testing...
testPasses= 0
Testing...
testPasses= 2
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 2
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 2
Testing...
testPasses= 0
Testing...
testPasses= 2
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 2
Testing...
testPasses= 0
Testing...
testPasses= 2
Testing...
testPasses= 1
Testing...
testPasses= 0
Testing...
testPasses= 2
Testing...
testPasses= 1
Testing...
testPasses= 1
Testing...
testPasses= 1

Which also isn't what I would expect.

EDIT: removed typo

I suspect your pull-down resistors are not connected properly.
1M ohm is rather a large value for a pull-down, touching the leads will easily change the voltage levels. Something like 10k ohm might be a better choice.

Ah... see next post...

Yours,
TonyWilk

Oops, the earlier code did define TESTPIN5 wrong.

This is fixed, tested and has some debug Serial.print...

// Define flags in binary progression for all pins
//
#define PINFLAG1  1
#define PINFLAG2  2
#define PINFLAG3  4
#define PINFLAG4  8
#define PINFLAG5  16

// Define all pin numbers
#define TESTPIN1 13
#define TESTPIN2 12
#define TESTPIN3 11
#define TESTPIN4 10
#define TESTPIN5 9

void setup()
{
  Serial.begin( 9600 );

  pinMode( LED_BUILTIN, OUTPUT );
}

// Function to test pins and return accumulated flags
// passed: pin to drive HIGH to do test
//
int testPins( uint8_t drivePin )
{
  int result= 0;

  pinMode( TESTPIN1, INPUT );
  pinMode( TESTPIN2, INPUT );
  pinMode( TESTPIN3, INPUT );
  pinMode( TESTPIN4, INPUT );
  pinMode( TESTPIN5, INPUT );

  pinMode( drivePin, OUTPUT );
  digitalWrite( drivePin, HIGH );

  delay(10);   // allow pins to settle before reading

  if( digitalRead(TESTPIN1) == HIGH )
    result= result+PINFLAG1;
  if( digitalRead(TESTPIN2) == HIGH )
    result= result+PINFLAG2;
  if( digitalRead(TESTPIN3) == HIGH )
    result= result+PINFLAG3;
  if( digitalRead(TESTPIN4) == HIGH )
    result= result+PINFLAG4;
  if( digitalRead(TESTPIN5) == HIGH )
    result= result+PINFLAG5;

Serial.print("Driving Pin: "); Serial.print(drivePin); Serial.print(" result: ");
Serial.print("1= "); Serial.print( digitalRead(TESTPIN1) ); Serial.print(", ");
Serial.print("2= "); Serial.print( digitalRead(TESTPIN2) ); Serial.print(", ");
Serial.print("3= "); Serial.print( digitalRead(TESTPIN3) ); Serial.print(", ");
Serial.print("4= "); Serial.print( digitalRead(TESTPIN4) ); Serial.print(", ");
Serial.print("5= "); Serial.println( digitalRead(TESTPIN5) ); 


  return result;
}


void loop()
{
  Serial.println("Testing...");

  // Example tests
  int testPasses= 0;

  // driving TESTPIN1 high, we expect TESTPIN1 and TESTPIN4 to be high, the rest low:
  //
  if( testPins( TESTPIN1 ) == (PINFLAG1 + PINFLAG4) )
    testPasses++;

  // pin2 shorted to pin5
  //
  if( testPins( TESTPIN2 ) == (PINFLAG2 + PINFLAG5) )
    testPasses++;

  // pin3 not connected to any other pin
  //
  if( testPins( TESTPIN3 ) == (PINFLAG3) )
    testPasses++;


  Serial.print("testPasses= ");
  Serial.println( testPasses );
  
  delay( 2000 );
}

I've tested this with an Arduino UNO with 1M ohm pull-downs and it prints:

Testing...
Driving Pin: 13 result: 1= 1, 2= 0, 3= 0, 4= 1, 5= 0
Driving Pin: 12 result: 1= 0, 2= 1, 3= 0, 4= 0, 5= 1
Driving Pin: 11 result: 1= 0, 2= 0, 3= 1, 4= 0, 5= 0
testPasses= 3

Yours,
TonyWilk

Hi,
From the picture are you sure they are 1M resistors?
Brown - Black - Green

Tom.. :slight_smile:

Yes, they were 1Mohm.

I ran the new script, worked great. I changed to 10kohm as suggested as a precaution and still works.

Everything is working as it should now.

Thanks for all the help everyone,
Trent