So I'm trying to do a speed trap for a motorcycle track. I got two sharp ir sensors and thought it would be easy using the attachInterrupt. The triggers would keep the time and then it would be a simple calculation to get the speed.
What's happening is that the second trigger keeps firing so the second wheel of the motorcycle is triggering it again ![]()
Here is a copy of the code (which I am sure is wrong). If anyone can help, I would greatly appreciate.
int Delay = 2000;
int led = 13;
int led2 = 7;
int ledReady = 9;
int FirstInterval = 0;
int SecondInterval = 0;
//volatile int state = LOW;
float Distance = 0.00030;//Distance In Kilometers 30cm
float KMperHour = 0;
void setup()
{
// Serial.begin(9600);
// analogWrite(led,255);
// analogWrite(led2,255);
// analogWrite(ledReady,255);
pinMode(2, INPUT);
pinMode(3, INPUT);
attachInterrupt(0, first, RISING);
attachInterrupt(1, second, RISING);
lcd.begin(16, 2);
lcd.print(" ABC Speed Trap ");
lcd.setCursor(0, 1);
lcd.print(" Welcome :)");
}
void loop()
{
CalculateSpeed()
}
void first()
{
FirstInterval = micros();
}
void second()
{
SecondInterval = micros();
}
void CalculateSpeed()
{
KMperHour = 0 ;
float timeDifference = SecondInterval - FirstInterval ; //in MilliSeconds
timeDifference = timeDifference /3600000 ; //in Hours
KMperHour = Distance / timeDifference ;
if (KMperHour < 0)
{
KMperHour = KMperHour *-1;
}
}