#include <NewPing.h>
#define TRIGGER_PIN 11 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
int length_of_the_track = 51;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, length_of_the_track); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
//distance
delay(1000);// Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("ditance between the cars & track: ");
Serial.println(sonar.ping_cm());// Send ping, get distance in cm and print result (0 = outside set distance range)
float dis = sonar.ping_cm();
//distance between the cars and the track
Serial.print("distance of the line: ");
Serial.println(length_of_the_track-dis);
// no of cars
float avg_length_of_a_car = 7.125;
float cars = ceil((length_of_the_track-dis)/avg_length_of_a_car);
Serial.print("number of cars: ");
Serial.println(cars);
//Time taken by the cars to take a right turn
int avg_time = 4;// Actually the time for one car is 5s, for two 6 second, That's why the equation becames n+4 s, where n = the no of cars
int time_taken = cars+4;
Serial.print("Time taken for a car: ");
Serial.println(time_taken);
Serial.println("----------------------------------------");
}