# Stepper motor with HCSR-04 sensor

I want to make a stepper motor increase and decrease speed working with a HCSR04 sensor. The idea is that if the sensors detects an object the stepper motor is going to decrease, and stop as close as possile to the object.

I have made some code, and it is sorta working..

My current code looks like this:

``````if (distance > 30)
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(500); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(500); // Wait
}

if (distance > 20 && distance < 30)
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(2000); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(2000); // Wait
}

if (distance > 10 && distance < 20)
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(10000); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(10000); // Wait
}

if (distance > 1 && distance < 10)
{
//digitalWrite(13,HIGH); // Output high
//delayMicroseconds(1000); // Wait
digitalWrite(13,LOW); // Output low
}
``````

But I thought it could be easier, So I made a function:

``````void velocity()
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(500); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(500); // Wait
}
``````

Now my question is: Can I manipulate the delayMicroseconds to change if the distance changes? Is there a formula to do so?

Entire code:

``````int trigPin = 11;
int echoPin = 10;

void setup()
{
pinMode(trigPin, OUTPUT); //Sensor
pinMode(echoPin, INPUT); //Sensor
pinMode(6,OUTPUT); // Enable
pinMode(13,OUTPUT); // Step
pinMode(12,OUTPUT); // Dir
digitalWrite(6,LOW); // Set Enable low
digitalWrite(5,LOW); // Set Step low
Serial.begin(9600);
}

void loop()
{
//rotateleft();
//Serial.println("ik draai nu exact een rotatie linksom");
//delay(1000);
//rotateright();
//Serial.println("ik draai nu exact een rotatie rechtsom");
//delay(1000);
//velocity();

//sensor
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print ("cm ");
Serial.print (distance);

//eigen creatie

if (distance > 30)
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(500); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(500); // Wait
}

if (distance > 20 && distance < 30)
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(2000); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(2000); // Wait
}

if (distance > 10 && distance < 20)
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(10000); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(10000); // Wait
}

if (distance > 1 && distance < 10)
{
//digitalWrite(13,HIGH); // Output high
//delayMicroseconds(1000); // Wait
digitalWrite(13,LOW); // Output low
}

//Own functions
}

void velocity()
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(500); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(500); // Wait
}

void rotateleft()
{
digitalWrite(12,HIGH); // Set Dir high
for(int x = 0; x < 200; x++) // Loop 200 times
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(1000); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(1000); // Wait
}
}
void rotateright()
{
digitalWrite(12,LOW); // Set Dir high
for(int x = 0; x < 200; x++) // Loop 200 times
{
digitalWrite(13,HIGH); // Output high
delayMicroseconds(1000); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(1000); // Wait
}
}
``````
• You might look into using the `constrain()` function to establish a distance beyond which you will run at full speed, and then use the `map()` function to turn that into an inverse relationship between distance and delay. Beware of possible hesitations while measuring, and the fact that ultrasonic sensors have a minimum distance, so make sure you don't go to full speed when something is closer than the minimum! Oct 22, 2016 at 22:10

Assuming you have a distance variable containing the distance measured from your sonar sensor in cm. You can use that value to adjust the delay times in between your steps.

EDIT Modified code to increase speed when distance increases

``````int time = 0;
int timeWait = 1000; //interval in milliseconds between each distance reading
int maxDelay = 1000;

void velocity(int speed){
int trim = 10;
digitalWrite(13,HIGH); // Output high
delayMicroseconds(maxDelay - speed*trim); // Wait
digitalWrite(13,LOW); // Output low
delayMicroseconds(maxDelay - speed*trim); // Wait
}

return distance;
}

void setup(){
time = millis();
}

void loop(){
if(millis()-time > timeWait){ //only read distance if 1000ms has elapsed since the last reading
time = millis();