# Use Ultrasonic Sensor and accelstepper library to move Step Motor Forward

I'm moving the step motor through the ultrasonic sensor distance.
But the step motor moves a little and stops. And repeat in the loop.
I want to move the step motor at the right speed.

this is the code

#include <AccelStepper.h>

int trigPin1 = 24;
int echoPin1 = 25;
long duration1, distance1;

#define STEPPER_y_DIR_PIN 7
#define STEPPER_y_STEP_PIN 4

AccelStepper stepper_y(AccelStepper::DRIVER, STEPPER_y_STEP_PIN, STEPPER_y_DIR_PIN);

void setup()
{
pinMode(trigPin1, OUTPUT); //Ultrasonic Sensor Pin
pinMode(echoPin1, INPUT); //Ultrasonic Sensor Pin
Serial.begin(115200);
stepper_y.setMaxSpeed(3000);
stepper_y.setSpeed(1000);
}

void loop()
{
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1= duration1*0.340/2; //Distance value,

if (distance1 > 200)
{
stepper_y.setSpeed(1000);
stepper_y.run();
}

else
{
stepper_y.setSpeed(0);
stepper_y.run();
}
}

I don't know what the problem is and how to solve it.

Thank you for your help.

#include <AccelStepper.h> is in your code, but you never use it. That is likely the reason for the problem.
Paul

Try using the `stepper.runSpeed();` function instead of ` stepper.run();`

You set the speed to 0 for both states, why?

if (거리 1> 200)
{
stepper_y.setSpeed ​​(1000);
stepper_y.run ();
}

else
{
stepper_y.setSpeed ​​(0);
stepper_y.run ();
}

I wrote it wrong. It moves at a speed of 1000 when the distance is more than 200.
The same phenomenon occurred even if runSpeed was used.

Try this code. I tested it with my Uno connected to a CNC shield so had to change pin numbers accordingly. The stepper spins steady when the distance is greater than 200, stops otherwise. Using the runSpeed() function. Put the runSpeed() (or run()) outside any functions other than loop() so that it will run every time through loop(). I also take ranges every 60 milliseconds to allow echos to die out so that there are fewer false echos. You may need to look for 0 ranges as that means that there was an out of range which could be an error.

``````#include <AccelStepper.h>

const byte trigPin1 = 9;
const byte echoPin1 = 10;
const byte STEPPER_y_DIR_PIN = 5; // CNC shield pin
const byte STEPPER_y_STEP_PIN = 2; // CNC shield pin
const byte enablePin = 8;  // added for my CNC shield

long duration1, distance1;

AccelStepper stepper_y(AccelStepper::DRIVER, STEPPER_y_STEP_PIN, STEPPER_y_DIR_PIN);

void setup()
{
pinMode(trigPin1, OUTPUT); //Ultrasonic Sensor Pin
pinMode(echoPin1, INPUT); //Ultrasonic Sensor Pin
pinMode(enablePin, OUTPUT); // added for my CNC shield
digitalWrite(enablePin, LOW); // added for my CNC shield
Serial.begin(115200);
stepper_y.setMaxSpeed(3000);
stepper_y.setSpeed(1000);
}

void loop()
{
// slow down the ranging to avoid unwanted echoes
static unsigned long timer = 0;
unsigned long interval = 60;
if (millis() - timer >= interval)
{
timer = millis(); digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1 * 0.340 / 2; //Distance value,
Serial.println(distance1);

if (distance1 > 200)
{
stepper_y.setSpeed(1000);
}

else
{
stepper_y.setSpeed(0);
}
}
stepper_y.runSpeed();
}
``````

Here the code is properly posted in code tags and indented for readability with the IDE autoformat tool (ctrl-t or Tools, Auto Format).

``````stepper_y.setMaxSpeed(3000);
``````

I left it in but the setMaxSpeed() function is used with the functions that use acceleration.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.