//定义电机控制用常量

//A4988连接Arduino引脚号
const int dirPin = 2;   //方向引脚 接一个正负级信号
const int stepPin = 3;  //步进电机引脚 接pwm信号

// 电机每一圈的步数
const int STEPS_PER_REV = 200;



void setup(){
  // put your setup code here, to run once:
  pinMode(dirPin, OUTPUT); 
  pinMode(stepPin, OUTPUT);
}

void loop(){
// put your main code here, to run repeatedly:
  digitalWrite(dirPin, LOW);

  // 电机慢速旋转
  for(int x=0; x < STEPS_PER_REV; x++){
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }

  delay(1000); //等待一秒

  //设置电机逆时针旋转
  digitalWrite(dirPin, HIGH);

  // 电机快速旋转
  for(int x=0; x < (STEPS_PER_REV * 2); x++){
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  delay(1000); //等待一秒

}


A4988