 # Controlling multible outputs with 2D arrays

Hi

Some time agao I saw a clip on the www.nexusorg.org/url] and I was thinking … hey how would I control multible outputs at the same time.

It should be math independend so no fancy “Knight rider” counting up and down So here is the first option:

``````// Array 1 program
// Multible output control using 2D arrays
// I don't like it - too "complex"

const int outputNumbers = 4;                       // Total output numbers
const int stepSpeed = 500;                         // Time between step chage, millisec
const int outputArray[outputNumbers] = {5,6,7,8};  // Array with the I/O on the board
const byte maxStep = 25;                            // Max 255
int stepNumber = 0;
unsigned long now;
int locationArray[outputNumbers] = {};             // Keeping track of where to look of match
const byte sequence[outputNumbers][maxStep] = {    // The sequence
{1,2,6,9,11,13,14,15,17,18,20,22,23,25},         // Not realy intutiv!!
{1,3,7,10,11,12,14,15,16,18,19,22,24,26},
{1,4,8,9,10,11,12,13,15,16,17,22,24,26},
{1,5,6,7,8,9,10,11,12,13,14,22,23,25}
};

void setup(){
for (int i=0; i<outputNumbers; i=i+1){
locationArray[i] = 0;
pinMode(outputArray[i], OUTPUT);
}
now = millis();
}

void loop(){
if (millis() - now >= stepSpeed){                      // Check if it's time to chage to next step
now = millis();
stepNumber = stepNumber + 1;
for (int i=0; i<outputNumbers; i=i+1){
if (sequence[i][locationArray[i]] == stepNumber){  // Do we have a match?
locationArray[i] = locationArray[i]+1;           // Yep - update location for next pass
digitalWrite(outputArray[i],HIGH);               // Flip the output HIGH
}
else {
digitalWrite(outputArray[i],LOW);                // No match - flip the output LOW
}
}
if (stepNumber > maxStep){                           // End of the line
stepNumber = 0;                                     // Reset stepnumber
for (int i=0; i<outputNumbers; i=i+1) locationArray[i] = 0;  // Reset location array
}
}
}
``````

Each output is assigned a line in the sequence array. Each number represent the stepnumer that the output has to be HIGH in.

It works but I don’t like it >:( The sequence is verry har to “read”. Memory is easyly overloaded since the array matrix hold outputNumbers * maxStep of byte even if it’s empty.

Ok, 2’th solution. Use of boolean in the sequence matrix:

``````// Array 2 program
// Multible output control using 2D arrays
// Using boolean

const int outputNumbers = 4;                       // Total output numbers
const int stepSpeed = 500;                         // Time between step chage, millisec
const int outputArray[outputNumbers] = {5,6,7,8};  // Array with the I/O on the board
const int maxStep = 27;
int stepNumber = 0;
unsigned long now;
const boolean sequence[outputNumbers][maxStep] = {      // The sequence
{1,0,1,0,0,0,1,0,0,1,0,1,0,1,1,1,0,1,1,0,1,0,1,1,0,1,0},   // Lots of '0'
{1,0,0,1,0,0,0,1,0,0,1,1,1,0,1,1,1,0,1,1,0,0,1,0,1,0,1},
{1,0,0,0,1,0,0,0,1,1,1,1,1,1,0,1,1,1,0,0,0,0,1,0,1,0,1},
{1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,0,1,0}
};

void setup(){
for (int i=0; i<outputNumbers; i=i+1)pinMode(outputArray[i], OUTPUT);
now = millis();
}

void loop(){
if (millis() - now >= stepSpeed){              // Check if it's time to chage to next step
now = millis();
for (int i=0; i<outputNumbers; i=i+1){
if (sequence[i][stepNumber] == true)       // Do we have a match?
digitalWrite(outputArray[i],HIGH);       // Yep, flip the output HIGH
else
digitalWrite(outputArray[i],LOW);        // Nope, flip the output LOW
}
stepNumber = stepNumber+1;
if (stepNumber >= maxStep) stepNumber = 0;    // Reset stepnumber

}
}
``````

It works but I don’t like it >:( I realy had to write alot of ‘0’ in that matrix. Code is a bit more simple comparede to no. 1 though.

Ok, 3’th solution. Writing the sequence in a 1D array and seperate the steps with ‘0’.

``````// Array 3 program
// Multible output control using 1D array
// '0' seperates the steps.

const int outputNumbers = 4;                       // Total output numbers
const int stepSpeed = 150;                         // Time between step chage, millisec
const int outputArray[outputNumbers] = {5,6,7,8};  // Array with the I/O on the board
unsigned long location = 0;
unsigned long now;
const byte sequence[] = {1,2,3,4,0,1,0,2,0,3,0,4,0,1,4,0,2,4,0,3,4,0,1,3,4,0,2,3,4,0,1,2,3,4,0,
2,3,4,0,1,3,4,0,1,2,4,0,1,2,3,0,2,3,0,1,3,0,1,2,0,2,0,1,0,0,1,2,3,4,
0,1,4,0,2,3,0,1,4,0,2,3,0,0};

void setup(){
for (int i=0; i<outputNumbers; i=i+1)pinMode(outputArray[i], OUTPUT);
now = millis();
}

void loop(){
if (sequence[location] == 0){
while (millis() - now <= stepSpeed) {} // timelaps
for (int i=0; i<outputNumbers; i=i+1)digitalWrite(outputArray[i], LOW); // Highspeed flickering
now = millis();
}
digitalWrite(outputArray[sequence[location]-1], HIGH);
location = location + 1;
if (location == sizeof(sequence)) location = 0;
}
``````

It works … and I allmost like it ;D
There will be some high speed flickering on outputs…

Well, if anyone can use this or get’s a “wow - now I see how 2D arrays works since the reference page ain’t that helpfull” freel free to use or steal any of the code.

-Fletcher