I am having a little problem with measuring the delay between two sensors.
So I have two sensors and about 2cm distance between them. When someone walks past the sensors one is activated a little sooner (probably miliseconds). I need to know which of the sensors was activated before the other. So what can I do? Probably use millis function and get the time of the activation and then compare it? That would probably be the logical step but I have a feeling that this wont work for some reason ::).
mem: Your code doesn't work. It works for first or second sensor - only one direction. If I change pins it works in different direction. And it works for first if it has been triggered first...
Aaa! I figured out what your code does. It prints the delay between sensor activations. It is realy not what I want - i just need to know which of sensors was triggered first. But English is not my primary language so I have little problems telling people what I want. ;D
roli, do you want help getting the code to work? If so, look at the serial output and see if you can see what is not working. Feel free to add more print statements to see how the variables change when the sensors are triggered. If you get stuck, post the serial output along with a description of how the sensors were triggered.
I just added a piece of code that prints which sensor was activated (I put it where the time of sensor trigger is recorded) and found out that the thing records the time many times but prints which was activated first only once or it doesn't print that at all. So for one pass sensor gets activated (and time recorded) many times. So I added delay of 10 and wawed in one direction and in second direction and this is the result:
Sensor 1 active
Sensor 2 active
sensor 1 triggerd first, time is: 72
Sensor 2 active
Sensor 2 active
Something is wrong here. They work normaly with LEDs (I created little code that blinks when sensor is activaed). If I wawe the LEDs blink just once and in right order. But they don't here.
What are you using as sensors, I wonder if you are getting bounce.
There are many articles on dealing with switch bounce including this one:Arduino Playground - SoftwareDebounce
I am using IR sensors (one side has IR emmiter and IR reciever, and second one has the same). IR reciever sees IR diode from one side and the same for the other side. Sensor gets HIGH when the IR diode isn't in range. If I plug them into the analog port I get a value to about 120 when the sensor isn't triggered and about a 500 when it is. So it is possible that the sensor doesn't go well with digital in?
So it is possible that the sensor doesn't go well with digital in?
That is quite possible. A 500 count analog reading converts to about 2.4vdc, which is not a valid digital high voltage value. Can you supply a drawing on how you are wiring the emitters and receivers and can you give the device part numbers to see if data sheets can be located.
I tested the code and the timeout logic may have been a problem. I modified the timeout check and it tests ok for me using switches as sensors (it bounces a little but the logic works)
#define SENSOR1 10
#define SENSOR2 11
#define SENSOR_TRIGGER LOW
#define TIMEOUT 1000 //stop looking for delay after this many ms
unsigned long sensor1Time = 0;
unsigned long sensor2Time = 0; // this should be an unsigned long
unsigned long timeout;
void setup(){
pinMode(SENSOR1,INPUT);
pinMode(SENSOR2,INPUT);
Serial.begin(9600);
}
void loop(){
if( digitalRead(SENSOR1) == SENSOR_TRIGGER ){
sensor1Time = millis();
timeout = sensor1Time+ TIMEOUT;
}
if( digitalRead(SENSOR2) == SENSOR_TRIGGER ){
sensor2Time = millis();
timeout = sensor2Time + TIMEOUT;
}
if( sensor1Time > sensor2Time && sensor2Time > 0){
Serial.print("sensor 2 triggerd first, time is: ");
Serial.println( sensor1Time - sensor2Time);
sensor1Time = sensor2Time = 0;
}
if( sensor2Time > sensor1Time && sensor1Time > 0){
Serial.print("sensor 1 triggerd first, time is: ");
Serial.println( sensor2Time - sensor1Time);
sensor1Time = sensor2Time = 0;
}
if( (millis() > timeout) && ( sensor1Time || sensor2Time) ){
sensor1Time = sensor2Time = 0;
}
}
the output was:
sensor 2 triggerd first, time is: 97
sensor 1 triggerd first, time is: 235
sensor 1 triggerd first, time is: 273
sensor 2 triggerd first, time is: 449
EDIT: It works with fast movement but if you move a little slower it duplicates the result. Problem is that I can't have duplicated results because this is acualy some kind of a counter.
I'm not positive but it seems to me that in your drawing the cathode of the photodiode should be wired to +5vdc instead of wiring to the analog input pin. ?
A little slower - I think still less than 1 second. But I did increase the timeout to 2 seconds just to be sure. But it didn't help.
I'm not positive but it seems to me that in your drawing the cathode of the photodiode should be wired to +5vdc instead of wiring to the analog input pin. ?
That way I get a nice little warning from my mac - "USB overcurrent notice....".
I edited the code, Sensor2Time wants to be an unsigned long, try it with that change.
with the timeout set to 3000 i got these readings when triggering slowly:
sensor 2 triggerd first, time is: 1205
sensor 1 triggerd first, time is: 2393
sensor 2 triggerd first, time is: 1368
sensor 1 triggerd first, time is: 1631
sensor 2 triggerd first, time is: 1364