Hi
I made a system that measure speed of cars based on infrared switches (sen 0019 Adjustable_Infrared_Sensor_Switch__SKU_SEN0019_-DFRobot ) , i'm using two of this sensors that detect when the car enter's and exit a predefined distance (2 mts) , the sensor triggers the millis function to capture the time and then calculate a total travel time which is used to calculate speed and then converted to Km/h.
My problem is that when the cars pass at a relative slow speed 10 to 30 Km/h everything is fine but for speeds > 30 like 40 70 or 100 Km/h it always give a 40 or around 40 Km/h (39.82 41.23)
hope you can help me with this i dont know why is not detecting fast speeds .
thanks
PD: the sensors are aimed at the body of the cars
i'm using 2 mts (2000 mm) intersensor distance
the variable "factor" is the 2000 mm * (a factor to convert mm and millis to Km/h)
here is my code
#include <Wire.h>
#include <UTFT.h>
#include <UTouch.h>
const int gate1Pin = 8;
const int gate2Pin = 9;
int previous ;
int previous2 ;
long startTime;
long endTime;
long travelTime;
float factor = 7183.8;
float travelSpeed = 0;
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
UTFT myGLCD(ITDB32S,38,39,40,41);
UTouch myTouch(6,5,4,3,2);
void setup () {
pinMode(gate1Pin, INPUT);
pinMode(gate2Pin, INPUT);
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, 319, 13);
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 226, 319, 239);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.print("Medicion de Velocidad", CENTER, 1);
Serial.begin(57600);
}
void loop () {
if(digitalRead(gate2Pin) == LOW && digitalRead(gate1Pin) == HIGH && startTime == 0 && endTime == 0 && previous == HIGH) //
{
startTime = millis();
}
if (digitalRead(gate2Pin) == HIGH && digitalRead(gate1Pin) == LOW && endTime == 0 && startTime != 0 && previous2 == HIGH)//
{
endTime = millis();
}
if (digitalRead(gate2Pin) == LOW && digitalRead(gate1Pin) == LOW && endTime == 0 && startTime != 0 && previous2 == HIGH)
{
endTime = millis();
}
if (digitalRead(gate2Pin) == HIGH && digitalRead(gate1Pin) == HIGH)
{
}
previous = digitalRead(gate2Pin);
previous2 = digitalRead(gate1Pin);
if (startTime != 0 && endTime != 0)
{
Serial.print(startTime);
Serial.print(" === ");
Serial.print(endTime);
Serial.print(" === ");
travelTime = endTime - startTime;
travelSpeed = factor / travelTime ;
Serial.print(travelSpeed,2);
Serial.print(" ");
startTime = 0;
endTime = 0;
}
drawText();
}
void drawText()
{
//myGLCD.clrScr();
myGLCD.setBackColor(0, 0, 0);
//myGLCD.setFont(SevenSegNumFont);
myGLCD.setFont(BigFont);
myGLCD.printNumF(travelSpeed,2,LEFT,60);
/*myGLCD.print("00" , LEFT, 60);
myGLCD.setFont(BigFont);
myGLCD.print("o", 65, 95);
myGLCD.setFont(SevenSegNumFont);
myGLCD.print("00" , 82, 60);*/
myGLCD.setFont(SmallFont);
myGLCD.print("dV:0.01 Km/h" ,LEFT, 120);
myGLCD.setFont(BigFont);
myGLCD.print("Km/h", 160, 95);
myGLCD.setColor(64, 64, 128);
myGLCD.fillRoundRect(10, 180, 70, 220);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect(10, 180, 70, 220);
myGLCD.setBackColor(64, 64, 128);
myGLCD.setFont(SmallFont);
myGLCD.print("Save", 20, 190);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setColor(64, 64, 128);
myGLCD.fillRoundRect(80, 180, 140, 220);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect(80, 180, 140, 220);
myGLCD.setBackColor(64, 64, 128);
myGLCD.setFont(SmallFont);
myGLCD.print("Clear", 85, 190);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setColor(64, 64, 128);
myGLCD.fillRoundRect(150, 180, 210, 220);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect(150, 180, 210, 220);
myGLCD.setBackColor(64, 64, 128);
myGLCD.setFont(SmallFont);
myGLCD.print("Log", 155, 190);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setColor(64, 64, 128);
myGLCD.fillRoundRect(220, 180, 280, 220);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect(220, 180, 280, 220);
myGLCD.setBackColor(64, 64, 128);
myGLCD.setFont(SmallFont);
myGLCD.print("Hist", 225, 190);
myGLCD.setBackColor(0, 0, 0);
}