Need coding help to scan digital pins

Newbie with coding so I'm horrible to say the least with making/understanding for/while/ i++,etc.. statements

Here is what I need help to code for the arduino mega board.. I'm creating a continuity test fixture that is essentially setting a digital pin high to turn on a relay (and LED for visual indication) and searching to ensure that the other digital pin paired with it (and only that pin) goes high also.

I will be using 12 digital pins (pins 22-44 even numbered pins will be output) that I will set high (1 at a time) and need to check that the corresponding pins (23-45 odd numbered) go high. And I will have two other pins (pins 46 and 47) that will either go high when all pins have been scanned (pin 46=green led=testing good) or turn on another pin (pin 47=red led=test failed)

For example..I want to set pin 22 high and then check that pin 23 goes high also.. but need it to stop and turn on pin 47 if any other pin (25-45 odd numbered) goes high also. Then if no other pin goes high it switches to setting pin 24 high and checks that pin 25 goes high and on and on through each of the 12 output pins. If it makes it through all the pins and no errors were found it will set pin 46 high to turn on the green LED signalling that the test has completed and no miswiring in the product under test have been found.

It would also be excellent that if an error/miswire is found it keeps the digital pin that was being tested and the output pin that corresponds to the pin that went high also and kept them both on. This makes it easy to see what pin in my device is erroring out. So for example I set pin 22 high and 23 and 25 goes high it should keep 22 high and turn on 24 (the corresponding output pin for pin 25) Then I can easily see that the LED on pin 22 and pin 24 is on so I know that there is a short between 22 and 24.

I can change pins if that makes coding easier but always need 12 outputs that are paired to look at 12 other pins to make sure they are set high when the matching pin also goes high.

I could write this now with my limited knowledge but I expect it to be way too complicated/long when a simple while/if/for/i++ statement would do the same.

Thanks in advance for anyone that helps

The trick is to use an array to hold the pin numbers. Then you can address them with the loop variable in a for loop. This means you don’t have to keep writing the same code over and over.

Grumpy_Mike:
The trick is to use an array to hold the pin numbers. Then you can address them with the loop variable in a for loop. This means you don’t have to keep writing the same code over and over.

and that makes sense and was where I was headed but again I’m a big newbie and just can’t grasp it… Is it possible that you give me a small example… say with only 3 pins that I set high and check the 3 corresponding pins. So pretend pins 1,2,3 are my outputs and pins 11,12,13 are the corresponding pins that I’m checking and pins 14 is for the red (fail) led and pin 15 is the green (scan complete) led.
I figured that you guys that do this stuff in your sleep could whip up a quick example…
I’m wierd in that I can look at this type of coding and completely understand it when I see it but for some reason just can’t write it myself yet… (I’ll get there though…just need to see enough good code)

int outPins[] = {1, 2, 3};
int inPins[] = {11, 12, 13};

void loop()
{
  // Loop to set pins HIGH
  for(int i=0; i<sizeof(outPins); i++)
  {
     digitalWrite(outPins[i], HIGH); // Set a pin HIGH

     // See if corresponding pin, and only that pin, is HIGH
     for(int j=0; j<sizeof(inPins); j++)
     {
        int state = digitalRead(inPins[j]);
        if(i == j)
        {
           if(state == HIGH)
              // Good
           else
              // Bad
        }
        else
        {
           if(state == LOW)
              // Good
           else
              // Bad
        }
     }
  }
}

Where the Good and Bad comments are, you would put code to set the good/bad pins high. You might want to stop the loops when a Bad condition occurs and/or write something to the serial monitor that indicates what output pin and input pin created the bad condition.

There are already examples that came with the version 022 code that do exactly these sorts of things.

I am not sure that you are making a reasonable request.

WillR:
There are already examples that came with the version 022 code that do exactly these sorts of things.
I am not sure that you are making a reasonable request.

I’m sorry I wasn’t aware of that… I will take a look.

And Paul… That you very much… I will give it a try later to see if it does what I want…
I just get really confused when I try to write the code to do the //Loop to set pins HIGH stuff.