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 &gt; 30) 
{
  digitalWrite(13,HIGH); // Output high   
  delayMicroseconds(500); // Wait   
  digitalWrite(13,LOW); // Output low   
  delayMicroseconds(500); // Wait 
}

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

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

if (distance &gt; 1 &amp;&amp; distance &lt; 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 &gt; 30) 
{
  digitalWrite(13,HIGH); // Output high   
  delayMicroseconds(500); // Wait   
  digitalWrite(13,LOW); // Output low   
  delayMicroseconds(500); // Wait 
}

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

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

if (distance &gt; 1 &amp;&amp; distance &lt; 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 &lt; 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 &lt; 200; x++) // Loop 200 times
   {     
     digitalWrite(13,HIGH); // Output high
     delayMicroseconds(1000); // Wait
     digitalWrite(13,LOW); // Output low
     delayMicroseconds(1000); // Wait   
   } 
 }
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
}

int readDistance(){
 //code to read distance
 return distance;
}    

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

void loop(){
   if(millis()-time &gt; timeWait){ //only read distance if 1000ms has elapsed since the last reading
     time = millis();
     int distance = readDistance(); //your distance reading code;

   }
   velocity(distance);
}`


EDIT 2 
You'll also need something to handle upper and lower limits for delays (i.e. you don't want delayMicroseconds(0) or any negative numbers and  you don't want to include extraneous distance readings)