Vehical speed detection code:
const int sensor1Pin = 2; // IR Sensor 1 connected to digital pin 2
const int sensor2Pin = 3; // IR Sensor 2 connected to digital pin 3
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long timeDiff;
float distanceBetweenSensors = 20.0; // in cm
float speedOfLight = 343.0; // Speed of sound in m/s
(approximation)
boolean measure = false;
void setup() {
[Link](9600);
pinMode(sensor1Pin, INPUT_PULLUP);
pinMode(sensor2Pin, INPUT_PULLUP);
[Link]();
// Print a message to the LCD.
[Link]();
[Link](0, 0);
[Link](" Vehical Speed ");
[Link](0, 1);
[Link](" Measure System ");
}
void loop() {
if (digitalRead(sensor1Pin) == LOW) {
measure = true;
}
if (measure)
{
unsigned long time1 = micros(); // Record the time when Sensor
1 is triggered
while (digitalRead(sensor2Pin) == HIGH) {} // Wait for Sensor 2 to
be triggered
unsigned long time2 = micros(); // Record the time when Sensor
2 is triggered
timeDiff = time2 - time1; // Calculate the time difference in
microseconds
// Calculate the speed of the vehicle
float timeInSeconds = timeDiff / 1000000.0; // Convert time
difference to seconds
float speed = distanceBetweenSensors / timeInSeconds; //
Calculate speed in meters per second
// speed = speed / 100; // Convert meters per second to
kilometers per hour
[Link]("Time: ");
[Link](timeInSeconds);
[Link](", Speed: ");
[Link](speed);
[Link](" cm/s");
[Link]();
[Link](0, 0);
[Link](" Vehical Speed ");
[Link](3, 1);
[Link](speed);
[Link]("cm/s");
measure = false;
delay(1000);
}