I'm new to Arduino and in need of help for this small project.
I'm trying to build an Arduino code that reads the speed of an object passing through two ultrasonic sensors. The plan now is to ping the first ultrasonic sensor once an object passes. The second sensor will then activate and wait for the object to pass by. The time it takes to reach the second sensor will be given into the equation.
Speed will be calculated by the distance between the two ultrasonic sensors divided by the time for the object to reach the second sensor. I would like to display the speed if possible on my computer screen.
Here is my code so far. Any help will be appreciated:
int trig1 = 3; //Trig 1
int trig2 = 4; //Trig 2
int echo1 = 5; //echo1
int echo2 = 6; //echo2
int ECHO1;
int ECHO2;
double first_time;
double second_time;
double final_time;
double speed;
void setup()
{
pinMode(trig1, OUTPUT); //sets the digital pin 3 as output
pinMode(trig2, OUTPUT); //sets the digital pin 4 as output
pinMode(echo1, INPUT); //sets the digital pin 5 as input
pinMode(echo2, INPUT); //sets the digital pin 6 as input
}
void loop()
{
ECHO1 = digitalRead(echo1);
while (ECHO1 == 0)
{
digitalWrite(trig1,HIGH);
delay(100);
digitalWrite(trig1,LOW);
ECHO1 = digitalRead(echo1);
}
first_time = time();
ECHO2 = digitalRead(echo2);
while (ECHO2 == 0)
{
digitalWrite(trig2,HIGH);
delay(100);
digitalWrite(trig2,LOW);
ECHO2 = digitalRead(echo2);
}
second_time = time();
final_time = second_time - first_time;
}