what is wrong in this program?the servo does not work.

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, 3200
DISTANCE, speed);
break;
case 2: //Y轴正方向
StepperMotor_1(false, false, true, 3200DISTANCE, speed);
break;
case 3: //Y轴负方向
StepperMotor_1(false, true, false, 3200
DISTANCE, speed);
break;
}
}

电机有滴滴声,但是不能正常转动。求大神赐教!

It doesn’t use a servo.

But the stepper code is completely bogus.

  for (int i = 0; i < steps; i++)
  {
     digitalWrite(PULPin_1, HIGH);
     delayMicroseconds(speed);
     digitalWrite(PULPin_1, LOW);
     delayMicroseconds(100-speed);
  }

is doing variable duty-cycle, not variable pulse rate. Step/direction interface requires
the rising edges of the pulse waveform to be timed appropriately, the duty cycle is irrelevant.

I would also suggest using AccelStepper library as this handles speed-ramping which is required
for getting upto all but the slowest speeds.

Note that the code fragment above seems to be assuming a motor can jump from
stationary straight to about 10000 steps/second, which is physically impossible so its
guaranteed never to work.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: