Selectively Storing Elements into an Array

I've been working on a small project and I'm trying to implement code that will essentially 'grab' a value when its going through an if statement when true

For example:

I have an array of char letters{A, B, C, D, E, F, G, H} respectively I have two arrays of pins {1,2,3,4,5,6,7,8} and {11,12,13,14,15,16,17,18}

where each pin array corresponds to the location of the letter array 1 = 11 (index 0 where A is located) 2 = 12 (index 1 where B is located) . . . 8 = 18 (index 7 where H is located)

Now for random case that the pins don't correspond to eachother, say, "2 = 15 and 5=13" I want to flag the location and store it into a buffered area to be used later after the check runs through the arrays.

outputting the letters corresponding to the "wrong" locations

wrongLetters = B, E

Can someone point me in the right direction?

Example code

char letters[] = {'A','B','C','D','E','F','G','H'};
const byte inputPins[]= {1,2,3,4,5,6,7,8}; //Input Pins
const byte outputPins[]  = {11,12,13,14,15,16,17,18}; //Output Pins

char wrongLetters[];

const byte allLetters=sizeof(inputPins);
void setup()
{
    for(byte i=0;i

may be this ...

#define LETTER_ARRAY 8 //not sure why you chose your array size as 12 but anyway...

char letters[LETTER_ARRAY] = {'A','B','C','D','E','F','G','H'};
const byte inputPins[]= {1,2,3,4,5,6,7,8}; //Input Pins
const byte outputPins[]  = {11,12,13,14,15,16,17,18}; //Output Pins

char badLetters[LETTER_ARRAY];

const byte allLetters=sizeof(inputPins);

void setup()
{
       byte j=0;

        Serial.Begin(9600);

    for(byte i=0;i

bear in mind that since this code in in "setup" it will run only once

I have suggestions.

char letters[12] = {'A','B','C','D','E','F','G','H'};

const byte inputPins[]= {1,2,3,4,5,6,7,8}; //Input Pins
const byte outputPins[]  = {11,12,13,14,15,16,17,18}; //Output Pins
The letters array is filled with 65,66,67,68,69,70,71,72.
There's a pattern there where element [0] is 'A'+0, [1] is 'A'+1, etc. 
The ASCII value of 'A' is 65.

The inputPins[] array, I would keep since it allows you to change the 
actual pin numbers but....

inputPins[0] corresponds to 'A'+0, [1] to 'A'+1,...[n] to 'A'+n.

You don't need a letters array to do that unless you want to the letters associated with the pins to be out of order regardless of the order of pin numbers in the array. Two levels of indirection would take two arrays, with your A-H in order and pins in order you actually can get away with NO arrays but I would leave the pin numbers in arrays just for flexibility.

inputPins[0] corresponds to outputPins[0], no? 
The index [0] is how you tie the two together and the ASCII text 
to print is 'A' + index of the pin that reads ON, in this case. 
The index makes the relation.

Also don't use pins 0 or 1 if you also use Serial. Those are serial port transmit TX and receive RX pins. Pin 13 is the led pin, you might want to substitute another pin just to reserve the led for an indicator light.

PS - sorry about all the edits but those braces played hell with the post formatting.

Yeah, I copied from my original code to try to simplify understanding the logic of storing/obtaining varying indices that's why the '12' was there :grinning:

Thanks! I was close on the badLetters[j]=letters*; but did not have the ++j (which I don't think i understand the logic of it. Is it just another variable to have to be able to store so it looks like a 'new badLetter'?*

GoForSmoke: I have suggestions.

char letters[12] = {'A','B','C','D','E','F','G','H'};

const byte inputPins[]= {1,2,3,4,5,6,7,8}; //Input Pins const byte outputPins[]  = {11,12,13,14,15,16,17,18}; //Output Pins






The letters array is filled with 65,66,67,68,69,70,71,72. There's a pattern there where element [0] is 'A'+0, [1] is 'A'+1, etc. The ASCII value of 'A' is 65.

The inputPins[] array, I would keep since it allows you to change the actual pin numbers but....

inputPins[0] corresponds to 'A'+0, [1] to 'A'+1,...[n] to 'A'+n.



You don't need a letters array to do that unless you want to the letters associated with the pins to be out of order regardless of the order of pin numbers in the array. Two levels of indirection would take two arrays, with your A-H in order and pins in order you actually can get away with NO arrays but I would leave the pin numbers in arrays just for flexibility.



inputPins[0] corresponds to outputPins[0], no? The index [0] is how you tie the two together and the ASCII text to print is 'A' + index of the pin that reads ON, in this case. The index makes the relation. ```

Also don't use pins 0 or 1 if you also use Serial. Those are serial port transmit TX and receive RX pins. Pin 13 is the led pin, you might want to substitute another pin just to reserve the led for an indicator light.

PS - sorry about all the edits but those braces played hell with the post formatting.

No problem! Thanks for your input as well! I'm very new to arduino and coding in general (did not know that about the serial pins on arduino). Luckily I only used those values and the code for the sake of simplicity.

My overall goal is to start fresh with an ATmega 2560 when I'm satisfied logically with my current Mega2560 build (yes I have a VERY long way haha). All have to start somewhere right?

dm41nes: Now for random case that the pins don't correspond to eachother, say, "2 = 15 and 5=13" I want to flag the location and store it into a buffered area to be used later after the check runs through the arrays.

outputting the letters corresponding to the "wrong" locations

wrongLetters = B, E

No... no. The number of the pin is not the array index but rather the content of the element at that index.

1st pin number is stored in the 1st element, address is start of array (the compiler sees array name as the base address of the array. All array elements are at base address + ( index * variable length in bytes ).

Input pin 5 will relate to output pin 5, the actual pin numbers are in the 5th element of each array. The index times them together. The code should never have enough information to get them wrong!

dm41nes: All have to start somewhere right?

Know the feeling. I started a while back myself.

When you learn pointers well, all of this will make better sense. Don't put pointers off for long.

It appears that you are making some kind of cable tester. Is that correct?

Kind of bizarre that you're setting the mode of the pins in your 'inputPins[]' array as OUTPUTS and then trying to read from the pins of your 'outputPins[]' array that you haven't set to either mode.

When code gets to be this convoluted it might indicate that you need to rethink your approach. Perhaps, start by describing (in English) the big picture of what you're trying to accomplish.

For a cable tester it would be best if all the pins start as electrically neutral INPUTs and then 1 at a time make an input pin INPUT_PULLUP (safe to ground) and one at a time the outputs set to OUTPUT (will be low from being INPUT before) and read the INPUT_LOW pin that will go LOW if the path between those two pins in continuous. Then turn the output pin back to INPUT and test the next. Any time the test shows a path between pins not of the same index it will be wrong.

AVR INPUT_PULLUP pin has a weak current supplied to the pin through the chip itself. The pin will read HIGH until it is grounded, then LOW. So you have 7 paths that by error may lead to wrong pins and 1 path that is right, you check each one in turn to see what is really connected and close the other paths to ground by making them INPUT so only one path at a time is grounded. You can switch pin modes so quickly, an 8x8 check can be done in less than 1 millisecond -- the reporting will take longer to print!

Yes, you can use led13 as good or bad indicator using ON/OFF/Blink Fast or Slow. I would be very tempted to wire 2 sets of 8 leds to show connected pins at slow human speed with a button for operator use, though that would take a bit of circuitry.

char letters[12] = {'A','B','C','D','E','F','G','H'};

const byte inputPins[]= {1,2,3,4,5,6,7,8}; //Input Pins
const byte outputPins[]  = {11,12,13,14,15,16,17,18}; //Output Pins

Whenever you see this, it usually means that it’s time for a struct:

struct PinGroup {
  char letter;
  const byte inputPin;
  const byte output pin;
};

PinGroup pinGroup[] = {
  {'A', 1, 11},
  {'B', 2, 12},
  {'C', 3, 13},
  {'D', 4, 14},
  {'E', 5, 15},
  {'F', 6, 16},
  {'G', 7, 17},
  {'H', 8, 18},
};