I will begin with my first experience working with Arduino and Arduino IDE. I made a speed detection "device" using 2 laser pointers and 2 LDR sensors connected to an Arduino UNO.

 

Building the schematics is very easy.

 

 

 

 

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.

 

Below you can find the Arduino 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 < 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 < threshold)) {
    photocellReading_2 = analogRead(photocellPin_2);  
    if (millis() - calcTimeout > 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");  
}

 

I think the code is more than well commented and needs no further explanation.