title: Speed measurement with arduino and ultrasonic hc-sr04 sensor?
body: <p>i want made speed detection "device" using with Arduino and two ultrasonic hc-sr04 <a href="http://kumuya.blogspot.com/2012/02/speed-measurement-with-arduino.html" rel="nofollow">like this link</a>. but I want to make it with ultrasonic instead by LDR.</p>
  
  <p>from that link. how lasers and ldr work, like this </p>
  
  <blockquote>
    <p>The resistors are used as pull-down resistors and I wired the sensors and put them in a case, to avoid them detecting surrounding light. For each case, a hole was drilled so that the laser beam can light the sensor while the ambient light does not affect the sensor. 
    The working principle is easy: an object that passes by will "cut" the laser beams, this means the LDR sensor will detect this sudden drop of light intensity. First I defined a threshold value under which the sensor is considered triggered, once the value is under threshold for the first sensor then Arduino waits for the second one to be triggered. During this waiting time it counts the elapsed time between the two events. When the second beam is interrupted, the timer stops and now is just simple math. The distance between the 2 sensors is known, the time between the two events is known, and speed can be computed as speed = distance/time. </p>
  </blockquote>
  
  <p>Below the Arduino code: </p>
  
  <pre><code>/* 
  by Claudiu Cristian 
  */ 
  
  unsigned long time1; 
  int photocellPin_1 = 0; // 1st sensor is connected to a0 
  int photocellReading_1; // the analog reading from the analog port 
  int photocellPin_2 = 1; // 2nd sensor is connected to a1 
  int photocellReading_2; // the analog reading from the analog port 
  int threshold = 700; //value below sensors are trigerd 
  float Speed; // declaration of Speed variable 
  float timing; 
  unsigned long int calcTimeout = 0; // initialisation of timeout variable 
  
  void setup(void) { 
  // We'll send debugging information via the Serial monitor 
  Serial.begin(9600); 
  } 
  
  void loop(void) { 
  photocellReading_1 = analogRead(photocellPin_1); //read out values for sensor 1 
  photocellReading_2 = analogRead(photocellPin_2); //read out values for sensor 2 
  // if reading of first sensor is smaller than threshold starts time count and moves to             calculation function 
  if (photocellReading_1 &lt; threshold) { 
  time1 = millis(); 
  startCalculation(); 
  } 
  } 
  
  // calculation function 
  void startCalculation() { 
  calcTimeout = millis(); // asign time to timeout variable 
  //we wait for trigger of sensor 2 to start calculation - otherwise timeout 
  while (!(photocellReading_2 &lt; threshold)) { 
  photocellReading_2 = analogRead(photocellPin_2); 
  if (millis() - calcTimeout &gt; 5000) return; 
  } 
  timing = ((float) millis() - (float) time1) / 1000.0; //computes time in seconds 
  Speed = 0.115 / timing; //speed in m/s given a separation distance of 11.5 cm 
  delay(100); 
  Serial.print(Speed); 
  Serial.print("\n"); 
  } 
  </code></pre>
  
  <p>how to implement the code with ultrasonic HC-SR04 sensors? 
  the coding is problem for me. hopefully someone can help me...... :(
  Please excuse my poor English !</p>