Arduino Code Tidbits – #1 – Declaring an Array with Pin Values

Arduino Code - simple yet sometimes so challenging!
Every once in a while you come to a point in writing a software program where you spend unexpectedly long time trying to write just one line of code correctly. Has it ever happened to you?
I had one of those forehead slapping moments a couple of days ago writing a program controlling a brushless DC motor. I’ll post the full program that came out later as a part of the description of an upcoming project. Just wanted to say that ironically, the Arduino code tidbit I want to describe here did not actually make it into the final version of the sketch! But I was surprised by having to spend so much time researching such a simple issue and finding it mentioned in neither official Arduino references nor elsewhere online.
So, when you create a new variable or an array, before you can use it, you need to decide on their type (integer, boolean, long etc.) and declare its type as well as (optionally) assign their initial value, such as this:
boolean ledState = false; // the current state of the status LED output pin int buttonState; // the current reading from the direction input pin
The digital pin values HIGH and LOW are constants normally used in a context like this:
if((millis() - flashTime) > flashDelayActual)
{
digitalWrite(flashPin, HIGH);
} else {
digitalWrite(flashPin, LOW);
}
so the voltage on the output pin, in this case defined by the variable flashPin is either zero (LOW) or +5V (HIGH). But what if the state of the pin is decided based on the current pointer in an array and there are not enough pins to justify directly outputting a byte-type variable into the entire port, as in the example below?
byte const digit_0=B00111111; DDRD = digit_0;
In that case you need to define an array with the digital pin values (HIGH/LOW). But what type of a variable LOW or HIGH actually are?
It turns out that they are actually integers. In other words, you can declare a multidimentional array with with 3 rows, 9 digital pin values per row either like this:
const int motorPinSteps[3][9]={
{HIGH,HIGH,HIGH,HIGH,LOW,LOW,LOW,LOW,LOW},
{HIGH,LOW,LOW,LOW,LOW,LOW,HIGH,HIGH,HIGH},
{LOW,LOW,LOW,HIGH,HIGH,HIGH,HIGH,LOW,LOW}
};
or like this:
const int motorPinSteps[3][9]={
{1,1,1,1,0,0,0,0,0},
{1,0,0,0,0,0,1,1,1},
{0,0,0,1,1,1,1,0,0}
};
and use them exactly the same way later in your program
for(int x=0;x<=2;x++) {
digitalWrite(motorPins[x],motorPinSteps[x][currentStepA]);
}
So, it’s up to you to decide whether you want to use a much shorter version of that declaration with 0s and 1s or the longer but somewhat more descriptive declaration with HIGHs and LOWs
P.S. The motorPinSteps[3][9] array was used as an attempt to drive a 9-slot/2-pole brushless DC motor. The type you commonly find in CD-ROM drives. The particular code did not make the cut because it’s really hard (from the hardware stand point) to drive a brushless motor using digital values such as HIGH and LOW. When the voltage across the windings changes abruptly from 0 to +5V (and no PWM is used), the motor is either not able to respond at all due to inertia or produces sudden jerky movements that are not compatible with the application. More on the actual application in a couple of days, when I’m done playing with it

Thanks a lot, it helped me to simplify my stepper sketch
byte Contact[]={8, 9, 10, 11}; int Step=0; byte output=0; const int motorPinSteps[][8]={ //Collumns = STEPS 0-7 {1,1,0,0,0,0,0,1}, //Output C0 {0,1,1,1,0,0,0,0}, //Output C1 {0,0,0,1,1,1,0,0}, //Output C2 {0,0,0,0,0,1,1,1} //Output C3 }; /*void stepper(){ //STEP1 digitalWrite(Contact[0], HIGH); digitalWrite(Contact[1], HIGH); digitalWrite(Contact[2], LOW); digitalWrite(Contact[3], LOW); delay(milliseconds); //STEP2 digitalWrite(Contact[0], LOW); digitalWrite(Contact[1], HIGH); digitalWrite(Contact[2], LOW); digitalWrite(Contact[3], LOW); delay(milliseconds); //STEP3 digitalWrite(Contact[0], LOW); digitalWrite(Contact[1], HIGH); digitalWrite(Contact[2], HIGH); digitalWrite(Contact[3], LOW); delay(milliseconds); //STEP4 digitalWrite(Contact[0], LOW); digitalWrite(Contact[1], LOW); digitalWrite(Contact[2], HIGH); digitalWrite(Contact[3], LOW); delay(milliseconds); //STEP5 digitalWrite(Contact[0], LOW); digitalWrite(Contact[1], LOW); digitalWrite(Contact[2], HIGH); digitalWrite(Contact[3], HIGH); delay(milliseconds); //STEP6 digitalWrite(Contact[0], LOW); digitalWrite(Contact[1], LOW); digitalWrite(Contact[2], LOW); digitalWrite(Contact[3], HIGH); delay(milliseconds); //STEP7 digitalWrite(Contact[0], HIGH); digitalWrite(Contact[1], LOW); digitalWrite(Contact[2], LOW); digitalWrite(Contact[3], HIGH); delay(milliseconds); //STEP8 digitalWrite(Contact[0], HIGH); digitalWrite(Contact[1], LOW); digitalWrite(Contact[2], LOW); digitalWrite(Contact[3], LOW); delay(milliseconds); }*/ void setup() { Serial.begin(57600); Serial.println("Intialize Contact Outputs:"); for(int output=0; output<=3; output++){ pinMode(Contact[output], OUTPUT); Serial.print(" Initialized Digital Output "); Serial.print(Contact[output]); Serial.print(" as Contact C"); Serial.println(output); } } void loop() { // for(int x=0;x=0; i7) Step=0; for(output=0; output<=3; output++) { digitalWrite(Contact[output], motorPinSteps[output][Step]); Serial.print(motorPinSteps[output][Step]); } Serial.print(" Step: "); Serial.println(Step); delay(3); Step++; }