const int ENAPin_1 = 5; //define Enable Pinconst
const int DIRPin_1 = 6; //define Direction pinconst
const int DIRPin_2 = 7; //define Direction pinconst
const int PULPin_1 = 8; //define Pulse pinconst
//const int buttonPin = 2; // the number of the pushbutton pin
// variables will change:
//int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
pinMode (ENAPin_1, OUTPUT);
pinMode (PULPin_1, OUTPUT);
pinMode (DIRPin_1, OUTPUT);
pinMode (DIRPin_2, OUTPUT);
//Serial.begin(115200); //设置串口波特率115200
// Serial.println("配置完毕");
// initialize the pushbutton pin as an input:
//pinMode(buttonPin, INPUT);
}
void loop()
{
XYAXISMOVE(0 ,5);
StepperMotor_1(false, true, false, 3200*5, 50);
delay(1000);
}
void StepperMotor_1(boolean ENA, boolean DIR1, boolean DIR2 , int steps,int speed)
{
digitalWrite(ENAPin_1, ENA);
digitalWrite(DIRPin_1, DIR1);
digitalWrite(DIRPin_2, DIR2);
for (int i = 0; i < steps; i++)
{
digitalWrite(PULPin_1, HIGH);
delayMicroseconds(speed);
digitalWrite(PULPin_1, LOW);
delayMicroseconds(100-speed);
}
}
void XYAXISMOVE(int DIR, int DISTANCE ,int speed=50)
{
switch(DIR)
{
case 0: //X轴正方向,DISTANCE单位cm
StepperMotor_1(false, true, true, 3200DISTANCE, speed);
break;
case 1: //X轴负方向
StepperMotor_1(false, false, false, 3200DISTANCE, speed);
break;
case 2: //Y轴正方向
StepperMotor_1(false, false, true, 3200DISTANCE, speed);
break;
case 3: //Y轴负方向
StepperMotor_1(false, true, false, 3200DISTANCE, speed);
break;
}
}