Help with arrays and stepper motor control

I have this stepper motor and stepper motor driver. I want to control the stepper motor through an arduino (obviously with an external power supply). I want to have it switch between the different steps at a speed you can change. So instead of defining each of the 4 motor pins and for each step telling it "pin 1: high" "pin two: low" etc., how do I make a array and just say "step 1: 1000" "step 2: 0100" etc.?

Without seeing your program I'm not sure that I understand your question. However I suspect you are approaching the problem the hard way. Have a look at these links

also look up the AccelStepper library

...R

Here is my code. It is pretty incomplete. “t” is going to be changed with a potentiometer. So how do I write those 4 pins low/high with a byte array? I am want to do something like “case 1: digitalWrite(stepPins, 1000)”. I do not want to have to write “digitalWrite(stepPins[0], HIGH);” “digitalWrite(stepPins[1], LOW);” etc. It is not that I am to lazy, but rather that it is something that could be useful with larger arrays (like maybe an LED matrix).

``````int stepPins[4] = {2,3,4,5}:
int t;
unsigned long cMil;
unsigned long pMil;
void setup() {
for(int h = 0; h<4; h++){
pinMode(stepPins[h], OUTPUT);
}

}

void loop() {
cMil = millis();

switch (stepp) {
case 1:
break;

case 2:
break;

case 3:
break;

case 4:
break;

default:
stepp = 1;
break;
}

}

void rotate(t){
for(int i = 0; i < 1; i++) {
pMil = cMil;
}
for (int z = 0; z < 4; z++){
if (cMil - pMil >= t){
stepp++;
pMil = cMil;
}
}

}
``````

Please don't use single letter names for variables other than in FOR loops. It is impossible, for example to see all the instances of a variable named 't' without also highlighting all the other uses of 't' in words such as "int"

Using meaningful names for variables makes it much easier to spot errors in a program.

...R

With a 4-wire stepper driver, you should use the Stepper library.

I think AccelStepper can also use that format even though it is not the most common method.

There are several ways to do this.

An inefficient but instuctive way to do it is with a two-dimensional array of boolean. Acually, we’ll use byte, because pin outputs are HIGH and LOW, not true and false (even though they are usually the same thing).

``````const byte stepSequence[8][4] = {
{HIGH, LOW, LOW, LOW},
{HIGH, HIGH, LOW, LOW},
{LOW, HIGH, LOW, LOW},
{LOW, HIGH, HIGH, LOW},
{LOW, LOW, HIGH, LOW},
{LOW, LOW, HIGH, HIGH},
{LOW, LOW, LOW, HIGH},
{HIGH, LOW, LOW, HIGH}
};
``````

You’d also have a corresponding array with your pin numbers:

``````const byte stepperPin[4] = {6,7,8,9};
``````

The code to output to the pins looks something like:

``````for(int step = 0; step<8; step++) {
delay(4); // 4ms between steps
for(int pin = 0; pin <4; pin++) {
digitalWrite(stepperPin[pin], stepSequence[step][pin]);
}
}
``````

As I said: not the best or fastest or coolest way, but it explains something about using arrays.