Chapter 16 Stepping Motor
two steps. The "ms" of stepping motor used in this project is 3ms (the shortest time), less than 3ms will exceed
the speed limit of stepping motor resulting in that motor can not rotate.
void moveOnePeriod(int dir,int ms){
int i=0,j=0;
for (j=0;j<4;j++){ //cycle according to power supply order
for (i=0;i<4;i++){ //assign to each pin, a total of 4 pins
if(dir == 1) //power supply order clockwise
digitalWrite(motorPins[i],(CCWStep[j] == (1<<i)) ? HIGH : LOW);
else //power supply order anticlockwise
digitalWrite(motorPins[i],(CWStep[j] == (1<<i)) ? HIGH : LOW);
printf("motorPin %d, %d \n",motorPins[i],digitalRead(motorPins[i]));
}
printf("Step cycle!\n");
if(ms<3) //the delay can not be less than 3ms, otherwise it will exceed
speed limit of the motor
ms=3;
delay(ms);
}
}
Subfunction moveSteps (int dir, int ms, int steps) is used to specific cycle number of stepping motor.
void moveSteps(int dir, int ms, int steps){
int i;
for(i=0;i<steps;i++){
moveOnePeriod(dir,ms);
}
}
Subfunction motorStop () is used to stop the stepping motor.
void motorStop(){ //function used to stop rotating
int i;
for(i=0;i<4;i++){
digitalWrite(motorPins[i],LOW);
}
}
Finally, in the while cycle of main function, rotate one circle clockwise, and then one circle anticlockwise.
According to the previous knowledge of the stepping motor, it can be known that the stepping motor rotation
for one circle requires 2048 steps, that is, 2048/4=512 cycle.
while(1){
moveSteps(1,3,512); //rotating 360° clockwise, a total of 2048 steps in a
circle, namely, this function(four steps) will be called 512 times.
delay(500);
moveSteps(0,3,512); //rotating 360° anticlockwise
delay(500);
}