unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;
The values stored in these variables are NOT timers. The names should reflect what IS stored in the variables.
I don't see where you deal with the second sensor being triggered first. What I would do is have two booleans, one for each sensor being triggered. I would set them in the ISRs.
I would move all of the code for calculating direction and speed to loop(). I would only execute the code when both booleans were true, at which time I'd reset them to false.
Instead of saving "SensorOne" and "SensorTwo" you should store "FirstSensorTime". If either interrupt happens and FirstSensorTime is zero, just save millis() in FirstSensorTime and you are done. When EITHER interrupt occurs and FirstSensorTime is not zero, subtract FirstSensorTime from millis() to get transit time. Then set FirstSensorTime to zero to prepare for the next cycle.
Use the Rising edge for the interrupt. You don't need interrupts on both edges if you're only acting on the Rising edge.
Unless your switching is very clean you should add debounce timers to ignore interrupts less than a few milliseconds apart.
There are several problems with your current code. First, doing serial printing in an ISR is not a good idea.
Pretend that you are the train. You pass the first sensor. What happens? You pass the second sensor. What happens?
The way I see it, ISR1() gets called, and then ISR2() gets called, which causes the speed to be computed, because all the needed data is present.
Now, pretend that you go the other way. You pass the second sensor. ISR2() is called, but does less because not all the data is present. Then, you pass the first sensor, which notes your presence, and when you arrived.
Now, explain WHY ISR1() does NOT also calculate speed, based on the fact that sensor 2 was triggered first.
By the way, there is nothing magical about ISR in the name. Name the functions something meaningful.
I tried to put calculation part in loop(), but its not working.
We can't see THAT code, nor do we know what you mean by "it isn't working". Of course it works. That it does not do what you expect says more about your expectations than about what actually happens.
Should I write calculation part in both ISR1 and ISR2?
No. You should put the calculation part in a function, and call it from both ISRs. Or from loop().
whenever I try to measure speed from sensor1 to sensor2 direction it works perfect.
But When I tried to compute from Sensor2 to Sensor1 I am getting false reading.
actually I tried different ways to calculate it but cant get correct one.
I just need a hint what shoul I need to do ?
Here is the code for 1 direction.
thank you
#define CMS_to_KMH 0.036// scale factor
#define DISTANCE 15.5 // distance between the two sensors in centimeter.
#define LED1 4 // constants LED Pin numbers
#define LED2 5
#define LED3 6
#define LED4 7
#define LED5 8
int SensorOnePin = 2;
int SensorTwoPin = 3;
volatile byte SensorOneState;
volatile byte SensorTwoState;
float Speed = 0, KMH = 0, tmp = 0;
boolean GotSecondSensor = false;
volatile unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;
void setup()
{
Serial.begin(9600);
pinMode(SensorOnePin, INPUT_PULLUP);
pinMode(SensorTwoPin, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
attachInterrupt(0, Sensor1, RISING);
attachInterrupt(1, Sensor2, RISING);
Serial.println("Start");
}
void loop(){
}
void Sensor1()
{
SensorOneState = digitalRead(SensorOnePin);
SensorTwoState = digitalRead(SensorTwoPin);
if(SensorOneState == true && GotSecondSensor == false) // Train drives through first "sensor"
{
SensorOne_timer = millis(); // record time
GotSecondSensor = true; // lockout this IF statement and unlock the next IF statement
}
}
void Sensor2()
{
SensorOneState = digitalRead(SensorOnePin);
SensorTwoState = digitalRead(SensorTwoPin);
if(SensorTwoState == true && GotSecondSensor == true)
{
SensorTwo_timer = millis(); //record the time the train reaches the second gate
GotSecondSensor = false; // unlock first IF statement and lockout this IF statement.
KMH = DISTANCE * (CMS_to_KMH); // "(FPS_to_MPH)" -> conversion factor, feet per second to miles per hour
tmp = (SensorTwo_timer - SensorOne_timer)/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);
int myNum = KMH/tmp;
byte pins[5] = { LED1, LED2, LED3, LED4, LED5} ;
for (byte i = 0 ; i < sizeof(pins) ; i++)
digitalWrite (pins[i], ( myNum & (1<<i)) != 0 ? HIGH : LOW) ;
Serial.print("Binar Codiert :");
Serial.println(myNum, BIN);
Serial.print("Geschwindigkeit im KM/H: ");
Serial.println(myNum);
}
}
If you record the time that each sensor is triggered, and compute the speed and direction when both are non-zero and then set them back to 0, the order doesn't matter.
But When I tried to compute from Sensor2 to Sensor1 I am getting false reading.