HCSR04 Sensor without delay I've made a code to make a stepper work together with a HCSR04 sensor. The problem here is that the sensor has to use a delay in order to work. This delay messes with the smootness of the stepper. 

If I remove the code for the sensor the stepper runs verry smooth, adding the sensor code causes the stepper to run 'shaky'. 

The 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); //**problem**
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5); //**problem**
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 Serial.print ("cm:");
 Serial.print (distance);

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

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

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

else
{
 velocity(); 
}

}

void velocity()
{
 digitalWrite(13,HIGH); // Output high
 delayMicroseconds(500); // Wait
 digitalWrite(13,LOW); // Output low
 delayMicroseconds(500); // Wait
}
 }
Have you searched for other libraries or example code for this particular sensor?

I found this NewPing library that claims to have been developed to fix poor performance of such modules due to poor timing methods.