Array dilemma...

Hi there :slight_smile:

this may seem stupid but...

I am trying to cycle through some pins with a HIGH in an array called "cycleRow"

Can I declare and fill the array as shown below and then go through it with a for loop pushing the HIGH along the pins or is this going to cause a data mismatch (int/string) in the array declaration before I even start?

const int row1 = 22;
const int row2 = 23;
.
.
const int row16 = 37;

pinMode (row1, OUTPUT);
pinMode (row2, OUTPUT);
.
.
.

int cycleRow[10] = {row1, row2, row...........};

:disappointed_relieved:

I don't entirely understand your example, but I don't see any reason why what you're doing would cause a type mismatch. You've defined rowN as an int, and then you've declared an array of ints, into which you put rowN.

After studying your code for some time, I think I finally understood what you were doing. Commenting your code and naming your variables and constants in a more descriptive way will help others to understand it, and help you to keep it all straight as it gets more and more complicated. It is very easy to paint yourself into a corner with code, where you take shortcuts in the beginning, when you can keep it all in your head, but as it gets more complicated, you can't keep track of it, and if you haven't been commenting and using good variable and function names, it will all fall apart.

I think that your "rowN" constants are defining output pins, perhaps for rows of LEDs. If that's right, I would suggest something like:

// define output pin numbers for LED rows
const int outputPinLEDrow1 = 27;
const int outputPinLEDrow2 = 28;

It should be obvious, when you are using a pinMode call, what the intent of the "rowN" argument is, but for example in your array, it's not obvious at all that it's supposed to hold an array of output pin numbers. If you used more descriptive variable names and commented your code, it would be.

// define array of LED row output pin numbers, for "stepping" a HIGH signal through
int cycleRow[] = {outputPinLEDrow1, outputPinLEDRow2, outputPinLEDRow3...}

Notice also that when defining an array with a pre-defined value, you don't need to specify the size. You can just leave the brackets blank, and the compiler will infer the size from the number of elements you provided.

Any time I see someone creating variables like, "row1, row2, row3, ... row27, row28 ..." I think that's probably bad coding practice, and that an array should be used instead. Arrays are tailor-made for this sort of thing. So what if you got rid of "rowN" entirely and just defined an array, such as:

// Define array of LED row output pin numbers. Array index corresponds to LED row number.
const int ledRowPinNums[] = { 28, 29, 30, 31, 32, 33 ... }

You could then turn on and off a row with:

digitalWrite(ledRowPinNums[x], HIGH)

Where x = 0 for the first row, 1 for the 2nd row, and so forth. The off-by-one counting will take a little getting used to, but that's just a normal part of working with arrays.

That's fine, but byte is all that is needed, not int.
cycleRow[0] will then access 22
[1] for 23,
[2] for 24,
[3] for 25,
[4] for 26,
[5] for 27,
[6] for 28,
[7] for 29,
[8] for 30, and
[9] for 31.
digitalWrite (cycleRow[8], HIGH); is one way to output one of the 10 high.

You'll have to make cycleRow[] bigger if you want to keep adding pins. Accessing cycleRow[10] and up when only [ 0] to [9] are declared with
byte cycleRow[10] = {};
will be past what you defined, and odd/erratic operations will result.

Hey thanks for yur help guys :smiley: It's actually for a midi controller... here is all the stuff in question :wink:

I have been trying to do this job without a shift register as i have the Mega2560. Code Tinker Hack: How to turn Piano toy into MIDI keyboard (using Arduino/Atmega)

void setup() {

const int row1 = 22; // Rows 1-16 will be set as O/P starting at pin 22..37
const int row2 = 23; // Each goes HIGH incrementaly.
const int row3 = 24; // All colums scanned for each increment.
const int row4 = 25;
const int row5 = 26;
const int row6 = 27;
const int row7 = 28;
const int row8 = 29;
const int row9 = 30;
const int row10 = 31;
const int row11 = 32;
const int row12 = 33;
const int row13 = 34;
const int row14 = 35;
const int row15 = 36;
const int row16 = 37;

const int columx1 = 42; // Columns for FIRST contact... set as I/P starting at even pins 42..52
const int columx2 = 44; // Scanned for a HIGH.
const int columx3 = 46;
const int columx4 = 48;
const int columx5 = 50;
const int columx6 = 52;

const int columy1 = 43; // Columns for SECOND Contact... set as I/P starting at odd pins 43..53
const int columy2 = 45; // Scanned for a HIGH.
const int columy3 = 47;
const int columy4 = 49;
const int columy5 = 51;
const int columy6 = 53;

pinMode (row1, OUTPUT); // Set Pin Modes for rows...
pinMode (row2, OUTPUT);
pinMode (row3, OUTPUT);
pinMode (row4, OUTPUT);
pinMode (row5, OUTPUT);
pinMode (row6, OUTPUT);
pinMode (row7, OUTPUT);
pinMode (row8, OUTPUT);
pinMode (row9, OUTPUT);
pinMode (row10,OUTPUT);
pinMode (row11,OUTPUT);
pinMode (row12,OUTPUT);
pinMode (row13,OUTPUT);
pinMode (row14,OUTPUT);
pinMode (row15,OUTPUT);
pinMode (row16,OUTPUT);

pinMode (columx1, INPUT); // Set Pin Modes for 1st contact colums...
pinMode (columx2, INPUT);
pinMode (columx3, INPUT);
pinMode (columx4, INPUT);
pinMode (columx5, INPUT);
pinMode (columx6, INPUT);

pinMode (columy1, INPUT); // Set Pin Modes for 2nd contact columns...
pinMode (columy2, INPUT);
pinMode (columy3, INPUT);
pinMode (columy4, INPUT);
pinMode (columy5, INPUT);
pinMode (columy6, INPUT);

boolean 1detect[16]; // Key press discovered 1st contact... *** POLYPHONY is 16 ***
boolean 2detect[16]; // Key press discovered 2nd contact... for Velocity.

int cycleRow[] = {row1, row2, row3, row4, row5, row6, row7, row8,
row9, row10, row11, row13, row14, row15, row16};

Oh please use a loop for that.

for(int i = 0; i < PinWhatever; i++) { 
   pinMode (i, OUTPUT)
}

thank you dgrat!! :grin:

I shall now replace my many lines of ____ with that fabulous hint.

I guess I just got caught up in txt book and example fog. I shall remove my unnecessary formalities pronto!

Cheers :wink: