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 🙂
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.
Other questions, why do you use 4 output ?? because my motor have 3 cabos..
If you were referring to this schematic: http://elabz.com/brushless-dc-bldc-motor-with-arduino-part-3-the-stroboscope-project/ , that 4th output is simply for an LED. It was designed for a stroboscope, so it needed a light source in addition to the motor
will you please provide code to drive BLDC motor in arduino ?
Well, sure: http://elabz.com/brushless-dc-bldc-motor-with-arduino-part-3-the-stroboscope-project/ you will find the schematic and the code in there
thankyou very much, i knew pattern matching concept by using for loops but in right time u made me remember it