user26371: 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 > 20 && distance < 30)
{
  digitalWrite(13,HIGH); // Output high   
  delayMicroseconds(1000); // Wait   
  digitalWrite(13,LOW); // Output low   
  delayMicroseconds(1000); // Wait
}

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

if (distance > 1 && distance < 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
}
 }


user10394: You only have a single thread of execution of an Arduino.  So you have limited options:

Live with it.
Use two Arduinos.
Use a microcontroller with 2 cores.
Interrupts

2 Is possible, you could have a slave mcu to drive the motor and control it over I2C.  You will still have contention when you are driving the I2C bus, but you can time that so it is not when you are scanning.
3 Is a very similar solution to 2, but is all in one chip.

You MIGHT be able to use interrupts which will let processing continue whilst you are waiting for something to occur.  I can't remember if the Arduino has timer interrupts, if it doesn't an external source could generate the interrupts and changes the state of the lines.,


user27653: 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.

26371: <p>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. </p>

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

<p><strong>The code:</strong></p>

<pre><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
}
 }
</code></pre>
 10394: <p>You only have a single thread of execution of an Arduino.  So you have limited options:</p>

<ol>
<li>Live with it.</li>
<li>Use two Arduinos.</li>
<li>Use a microcontroller with 2 cores.</li>
<li>Interrupts</li>
</ol>

<p>2 Is possible, you could have a slave mcu to drive the motor and control it over I2C.  You will still have contention when you are driving the I2C bus, but you can time that so it is not when you are scanning.</p>

<p>3 Is a very similar solution to 2, but is all in one chip.</p>

<ol start="4">
<li>You MIGHT be able to use interrupts which will let processing continue whilst you are waiting for something to occur.  I can't remember if the Arduino has timer interrupts, if it doesn't an external source could generate the interrupts and changes the state of the lines.,</li>
</ol>
 27653: <p>Have you searched for other libraries or example code for this particular sensor?</p>

<p>I found this <a href="http://playground.arduino.cc/Code/NewPing" rel="nofollow noreferrer">NewPing</a> library that claims to have been developed to fix poor performance of such modules due to poor timing methods.</p>
