I am working with two sets of IR beam, they are to be placed in X distance. My goal is to measure speed. I would like to measure time between the first IR beam to the second beam when the beam breaks.
I am not sure what's going on, I am not getting any time of the serial port but the LEDs are turning on and off the way I wanted.
Please help me with getting the time between two IR beam sensors.
This the sensor that I am using - Adafruit IR breakbeam sensors and I am working with a Mega2560.
#define LEDPIN 13
int startPin = 8, finishPin = 5;
unsigned long starttime = 0, finishtime, time_interval;
float distance = 1, Speed; // Speed calculated
int trigger1 = 0; // Sensor 1
int trigger2 = 0; // Sensor 2
int sensor1State; // Sensor 1 status
int sensor2State; // Sensor 2 status
void setup() {
pinMode(startPin, INPUT_PULLUP); // Sensor 1 as input
digitalWrite(startPin, HIGH); // Turn on the pullup
pinMode(finishPin, INPUT_PULLUP); // Sensor 2s input
//digitalWrite(finishPin, HIGH); // Turn on the pullup
pinMode(LEDPIN, OUTPUT);
time_interval = ((finishtime - starttime) / 1000000);
Serial.println(time_interval);
}
void loop() {
sensor1State = digitalRead(startPin); // Read the state of the IR sensor 1:
if (sensor1State == LOW) // See if IR beam of sensor 1 has been broken
{
if (trigger1 == 0 && trigger2 == 0) // Check to make sure both sensors have not triggered
{
starttime = micros(); // Save time when sensor 1 was triggered
digitalWrite(LEDPIN, HIGH);
trigger1 = 1; // Prevent sensor 1 from triggering again
}
}
if (trigger1 == 1) {
sensor2State = digitalRead(finishPin); // See if IR beam of sensor 2 has been broken
if (sensor2State == LOW) {
// Check to make sure sensor 1 has triggered but not sensor2
if (trigger2 == 0) {
// Run speed function
trigger2 = 1;
finishtime = micros(); // Save time when sensor 2 was triggered
digitalWrite(LEDPIN, LOW);
// time takes to travel between 2 IR Beam // divide by 1000000 to convert microsecond to second
Serial.println(time_interval);
}
// Reset both sensors
trigger1 = 0;
trigger2 = 0;
}
}
}
-
Need to post your code correctly. Please Edit and add 4 spaces before each line at a minimum.CrossRoads– CrossRoads2018年11月01日 16:42:26 +00:00Commented Nov 1, 2018 at 16:42
-
You will not get a meaningful result with a 16 MHz processor, where every clock cycle takes 62.5nanoseconds, and the speed of light is basically instant at short distances. time_interval probably needs to be a float also.CrossRoads– CrossRoads2018年11月01日 16:46:43 +00:00Commented Nov 1, 2018 at 16:46
-
2@CrossRoads, how is here speed of light in play?Juraj– Juraj ♦2018年11月01日 17:55:20 +00:00Commented Nov 1, 2018 at 17:55
-
Yeah, never mind that. I think you are close on the triggering and capturing the state kind of logic, just needs a little more thinking I think. For example, in here you never calculate the interval that you print if (trigger2 == 0) { // Run speed function trigger2 = 1; finishtime = micros(); // Save time when sensor 2 was triggered digitalWrite(LEDPIN, LOW); // time takes to travel between 2 IR Beam // divide by 1000000 to convert microsecond to second Serial.println(time_interval); }CrossRoads– CrossRoads2018年11月01日 19:03:28 +00:00Commented Nov 1, 2018 at 19:03
2 Answers 2
You've tried to print an interval at the bottom of the setup() function, using finishtime before you have ever used it. Fortunately, finishtime will have been initialized to zero before the program runs so the the arithmetic result will be zero, though meaningless.
In loop(), you've never completed the interval calculation before you try to print it.
If nothing at all is being printed, check your wiring and be sure your code is getting the triggers. You haven't showed your wiring, so we can't help you debug that, in case there is a problem with that also.
You never calculate the time_interval that you print. Add that after the finishtime line
if (trigger2 == 0) {
// Run speed function
trigger2 = 1;
finishtime = micros(); // Save time when sensor 2 was triggered
digitalWrite(LEDPIN, LOW);
// time takes to travel between 2 IR Beam // divide by 1000000 to convert microsecond to second
Serial.println(time_interval);
}
-
Thank you for the respond, I had it like that before still doesnt work, I am not getting any time, not sure whats going on ??Ish– Ish2018年11月01日 19:25:37 +00:00Commented Nov 1, 2018 at 19:25
-
void loop() { sensor1State = digitalRead(startPin); // Read the state of the IR sensor 1: if (sensor1State == LOW) // See if IR beam of sensor 1 has been broken { if (trigger1 == 0 && trigger2 == 0) // Check to make sure both sensors have not triggered { starttime = micros(); // Save time when sensor 1 was triggered Serial.println(starttime); digitalWrite(LEDPIN, HIGH); trigger1 = 1; // Prevent sensor 1 from triggering again } } if (trigger1 == 1) { sensor2State = digitalRead(finishPin); // See if IR beam of sensor 2 has brokIsh– Ish2018年11月01日 19:29:27 +00:00Commented Nov 1, 2018 at 19:29
-
} if (trigger1 == 1) { sensor2State = digitalRead(finishPin); if (sensor2State == LOW) { if (trigger2 == 0) { trigger2 = 1; finishtime = micros(); // Save time when sensor 2 was triggered Serial.println(finishtime); digitalWrite(LEDPIN, LOW); time_interval = ((finishtime - starttime) / 1000000); Serial.println(time_interval); }Ish– Ish2018年11月01日 19:30:31 +00:00Commented Nov 1, 2018 at 19:30
-
my wire is good, i hard wire to pin, grd and 5 v. My 1st code that i wrote its work with it, when i added the condition its not working anymore - I didnt add any resister do you think thas a reason its not working,Ish– Ish2018年11月01日 19:47:21 +00:00Commented Nov 1, 2018 at 19:47
-
please do not put code into comments .... nobody will read that .... add the code to your question (make sure that it is indented at least 4 spaces so that it shows up as "code")jsotola– jsotola2018年11月01日 23:05:25 +00:00Commented Nov 1, 2018 at 23:05