Speed Calculator Project Help

I am working on a project with two PIR sensors trying to record the speed of a passing vehicle. So far I have been making good progress however I need some help on the code.

Here is the datasheet for the sensors. I am using one analog PIR sensors and one digital PIR sensor.

Right now I am trying to simply output the time it takes in between sensor triggers. And it works for the first 5 or 6 times then jumps to a high number and continues on, obviously not what I want. I am not sure why its doing that and I can't seem to get any answer debugging with the serial port.

Here is the code

#include <LiquidCrystal.h>

String message1, message2, message3;
int digitalVal, analogVal;
int digitalPin = 2;
int analogPin = A0;
float t0, t1, time, time1, speed_0;

LiquidCrystal lcd(0, 13, 9, 4, 5, 6, 7);


void setup() {
 delay(2000);
 
 lcd.begin(16,2);
 lcd.clear();
 lcd.setCursor(0,0);
 
 Serial.begin(9600);
 while(!Serial) {
 ;
 }
  
  pinMode(A0, INPUT); // ch0 (PIR Sensor)
  pinMode(digitalPin, INPUT); // ch1 (PIR Sensor)   
}

void computeData_0() // calculates speed, direction and concentration 
{
   int t0 = 0;
   t0 = millis();
  
   while(digitalVal == LOW){
       digitalVal = digitalRead(digitalPin);
   }
  
   time = ((float) millis() - (float) t0) / 1000; // time between sensor dections (secs)
   speed_0 = (0.5 / time) * 0.681818; // calculates speed (mph)
   
   Serial.print("Time = ");
   Serial.print(time);
   Serial.println(" sec");
   lcd.print("Time = ");
   lcd.println(time);
}

void computeData_1() // calculates speed, direction and concentration 
{ 
   int t1 = 0;
   t1 = millis();
  
   while(!(analogVal <= 450 || analogVal >= 560)){
       analogVal = analogRead(analogPin);
   }
  
   time1 = ((float) millis() - (float) t1) / 1000; // time between sensor dections (secs)
   speed_0 = (0.5 / time) * 0.681818; // calculates speed (mph)
   
   Serial.print("Time = ");
   Serial.print(time1);
   Serial.println(" sec");
   lcd.print("Time = ");
   lcd.println(time1);
}

void loop() {

  analogVal = analogRead(A0);
  delay(10);
  digitalVal = digitalRead(digitalPin);
  delay(10);
  
  if (analogVal <= 450 || analogVal >= 560) {
    
        String message1 = "Analog Detected";
        Serial.println(message1);
        computeData_0();
  }
 
    
  else if (digitalVal == HIGH) {
        
        String message2 = "Digital Detected";
        Serial.println(message2);
        computeData_1();
  }
  
  
  else if (analogVal <= 400 || analogVal >= 550 && digitalVal == HIGH) {
        
        String message3 = "Both Sensors are being Triggered";
        Serial.println(message3);
  }
  
    
}

Any help would be appreciated.

I am using the Arudino Leonardo and again I'm trying to write some code to simply capture time between sensor seeing detected motion.

Are you reffing to something like this?

Edit: Added more comments.

#define FPS_to_MPH 0.68182 // scale factor
#define DISTANCE 35.5625 // distance between the two sensors.

const byte SensorOnePin = 2;
const byte SensorTwoPin = 3;

byte SensorOneState;
byte SensorTwoState;
float Speed = 0, MPH = 0, tmp = 0;
boolean GotSecondSensor = false;
unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;

void setup() 
{
  Serial.begin(115200);
  pinMode(SensorOnePin, INPUT);
  pinMode(SensorTwoPin, INPUT);
}

void loop() 
{
  // This is using normal buttons to detect car passing through one gate to another,
  // this can be changed to use US sensors to replace the buttons. That part you will  
  // need to write yourself.
  
  SensorOneState = digitalRead(SensorOnePin);
  SensorTwoState = digitalRead(SensorTwoPin);

  if(SensorOneState && GotSecondSensor == false)  // car drives through first gate "sensor"
  {
    SensorOne_timer = millis(); // record time
    GotSecondSensor = true; // lockout this IF statement and unlock the next IF statement
  }
  
  if(SensorTwoState && GotSecondSensor == true) 
  {
    SensorTwo_timer = millis(); //record the time the car reaches the second gate
    Speed = GetSpeed(SensorOne_timer, SensorTwo_timer, DISTANCE); // send the times and the distance into the function.
    Serial.print("MPH: ");
    Serial.println(Speed);
    GotSecondSensor = false; // unlock first IF statement and lockout this IF statement.
  }
  
}

float GetSpeed(unsigned long T1, unsigned long T2, float distance)
{
  MPH = distance * (FPS_to_MPH); // "(FPS_to_MPH)" -> conversion factor, feet per second to miles per hour
  tmp = (T2 - T1)/1000.00; // since the time we are using is in milliseconds, we need to convert milliseconds to seconds
  Serial.print("Time (seconds): ");
  Serial.println(tmp);
  return (MPH / tmp); //return the speed of the car in MPH
}
  pinMode(A0, INPUT); // ch0 (PIR Sensor)
  pinMode(digitalPin, INPUT); // ch1 (PIR Sensor)

Why such disparate names for nearly identical meanings?

Why is one PIR sensor connected to a digital pin and one connected to an analog pin?

Why are you using the wrong kind of sensors?

Probably should comment better in my code, but as I stated above I am using one analog PIR sensor and one digital PIR sensor. That's the reason I have pinMode(A0, INPUT) and pinMode(digitalPin, INPUT). Two different sensors.

I solved my initial problem for now however my digital sensor is not detecting motion fast enough as it takes several hand waves to get anything to show up in my original code, which is concerning when trying to create a speed calculator.

I am thinking the sensor goes HIGH initial then bounces back and forth between HIGH and LOW for some milliseconds causing some trouble. I am going to try to create some code to ignore any switches from HIGH to LOW less than a certain amount of time.

Any rhyme or reason why my digital sensor isn't picking up that motion sooner? The sensor itself is OPEN when not detecting so I have a 10K pulldown resistor from output to GND to make sure it sees GND. I am using only the arduino's 5V supply to power and GND in my circuit and sensors.

Thanks