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

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

Arduino Code - simple yet sometimes so challenging!

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 🙂

8 Responses to “Arduino Code Tidbits – #1 – Declaring an Array with Pin Values”

  • 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&lt;=3; output++){
        pinMode(Contact[output], OUTPUT);
        Serial.print(&quot;  Initialized Digital Output &quot;);
        Serial.print(Contact[output]);
        Serial.print(&quot;  as Contact  C&quot;);
        Serial.println(output);
       }
    }
    
    void loop() {
    //  for(int x=0;x=0; i7) Step=0;
      for(output=0; output&lt;=3; output++) {
        digitalWrite(Contact[output], motorPinSteps[output][Step]);
        Serial.print(motorPinSteps[output][Step]);
      }
      Serial.print(&quot;  Step: &quot;);
      Serial.println(Step);
      delay(3);
      Step++;
    }
    
  • Welington:

    Good morning, I am with a problem, I make a project with PIC to control motor bldc, I used the same your idea, but I don’t know what is happening because the motor is turning slow, do you have some program or idea for me help ??

    Thanks

    • If it’s turning at all, and not just twitching or oscillating back and forth, then you are almost there, you just need to increase the frequency with which the windings are commutated. It takes 36 sequential changes in winding commutation for the rotor to make one revolution (if you are using the same type of BLDC motor), so it requires a lot of calculations and make the MCU quite busy. I don’t have a PIC program for you, but, like I said, if you already see rotary movement, your own program is almost there, you just have to fine tune it.

      I also wanted to mention that you don’t want the motor to pick up the speed too fast – BLDC motors have issues with spinning up. The speed has to be ramped up at the rate that the motor can follow. Otherwise it will start losing cycles and will either stop or go in a wrong direction or just not at the speed that you want it to. First try to increase the speed slowly so you know how your motor behaves.

  • Welington:

    Other questions, why do you use 4 output ?? because my motor have 3 cabos..

  • gauri:

    will you please provide code to drive BLDC motor in arduino ?

  • Subhash G Gadiyar:

    thankyou very much, i knew pattern matching concept by using for loops but in right time u made me remember it

Leave a Reply

Or use the Forums ! If your comment is a question, please consider posting it to a matching section of our Electronics Forums. The forums allow for a more natural conversation flow, especially if multiple replies are required. Additionally, you'll be able to style your writing (bold font, italics etc.) and post images which can help with a good answer.

Latest Forum Topics 
Related Posts
Tools

Coming soon ...