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.
#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
}
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.