//continuous rotation function, the parameter steps specifies the rotation cycles, every
four steps is a cycle
void moveSteps(int dir, int ms, int steps){
int i;
for(i=0;i<steps;i++){
moveOnePeriod(dir,ms);
}
}
void motorStop(){ //function used to stop rotating
int i;
for(i=0;i<4;i++){
digitalWrite(motorPins[i],LOW);
}
}
int main(void){
int i;
if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen
printf("setup wiringPi failed !");
return 1;
}
for(i=0;i<4;i++){
pinMode(motorPins[i],OUTPUT);
}
while(1){
moveSteps(1,3,512); //rotating 360° clockwise, a total of 2048 steps in a
circle, namely, 512 cycles.
delay(500);
moveSteps(0,3,512); //rotating 360° anticlockwise
delay(500);
}
return 0;
}
const int motorPins[]={1,4,5,6}; //define pins connected to four phase ABCD of stepper
motor
const int CCWStep[]={0x01,0x02,0x04,0x08}; //define power supply order for coil for
rotating anticlockwise
const int CWStep[]={0x08,0x04,0x02,0x01}; //define power supply order for coil for
rotating clockwise
Subfunction moveOnePeriod ((int dir,int ms) will drive the stepping motor rotating four step clockwise or
anticlockwise, four step as a cycle. Where parameter "dir" indicates the rotation direction, if "dir" is 1, the servo
will rotate forward, otherwise it rotates to reverse direction. Parameter "ms" indicates the time between each