measuring a delay

Can you clarify "the time is reset back to 1...5"

When it works normaly the output says "sensor 1 triggerd first, time is: 57", and then the time grows. But every time that error happens I get this "sensor 1 triggerd first, time is: 1". That time is from 1 to 5 - if it is bigger it works.

It may be that the trigger pulse edge is not sharp or that there is some kind of bounce. If you can get hold of a scope it would be interesting to have a look. But you could just filter out these short pulses if they are not affecting the valid readings. In your code you can check if the time is 5 or less and simply ignore it.

I mounted the sensors today and tryed them in real world conditions:

sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1
sensor 2 triggerd first, time is: 72
sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1

I walked out of the room!

sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1
sensor 1 triggerd first, time is: 1

Walked back in!

So the sensor 1 is triggered too many times (When you walk out you can trigger the sensor two times or more if you go slowly).

How close together are the sensors?

The sensors ared like this:

E ---------S
.-----------
.-----------
.10cm----
.-----------
S---------E

So the space between Emmiter and sensor is about 10cm or a little less. And the left side is around 70cm from the right side.

If you ignore readings if 1ms for now does it working correctly in both directions ? try it moving slowly and miving quickley. We are testing to see if the problem is in the logic or in the sensors (or both). if it works moving slowly then the logic is probably ok.

I will try it as soon as I can get the sensors to work correctly. For some reason they are giving only LOW values. And they are mounted using duct tape until I can get them to work ;D. But I cant get to the HIGH values. And it was working yesterday. Maybe because I am using 20m of network cable to connect them to arduino? Probably not but it is a piece of information.

Great! Sensors are working again! Sensors are now active when the value gets to 100 (probably because of the cable length).

When I walk out:

Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor2 active!
sensor 1 triggerd first, time is: 18
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor1 active!
sensor 2 triggerd first, time is: 22
Sensor2 active!
Sensor2 active!
Sensor1 active!
sensor 2 triggerd first, time is: 19
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor2 active!
sensor 1 triggerd first, time is: 41
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!

And in:

Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor1 active!
Sensor2 active!
sensor 1 triggerd first, time is: 18
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!

This was moving slowly. But the problem here is that the sensors are not working again. They work and they don't. I think that the cable is too long so I can't get a nice value to compare. Sometimes it is working with 100, sometimes with 200, sometimes with neither.

Can you run your initial tests with short cable lengths- the shorter the better. When you have the software working reliably you can then look at increasing the lengths. You may need a transistor or IC to drive the longer line but leave that issue for later.

These are the tests with half of the original cable length.

Slow:
Walk in:

Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor1 active!
sensor 2 triggerd first, time is: 16
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!

Walk out:

Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor2 active!
sensor 1 triggerd first, time is: 18
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!

Fast:
Walk in:

Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor1 active!
sensor 2 triggerd first, time is: 19
Sensor1 active!
Sensor2 active!
sensor 1 triggerd first, time is: 19
Sensor2 active!
Sensor2 active!
Sensor2 active!
Sensor1 active!
sensor 2 triggerd first, time is: 16
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!
Sensor1 active!

Walk out:

Sensor1 active!
Sensor1 active!
Sensor2 active!
sensor 1 triggerd first, time is: 17
Sensor1 active!
Sensor2 active!
sensor 1 triggerd first, time is: 18
Sensor1 active!
Sensor2 active!
sensor 1 triggerd first, time is: 17
Sensor1 active!

EDIT: This was before I was checking if times is 1. After many tryes that "time is 1" problem came back so I am now trying the code that ignores time if it is 1. Will see how it goes.

EDIT2: Hell now the time is 2! So I checked if time is less than 10 -> this thing now says that time is 11. Raised the time to 50 -> time is now 51. >:( :o

Try it with switches. or use wires to make and break contact if you don't have switches. You will need pull-ups.

You may want to add some code to ignore the bounce. When you have th sketch logic working you can figure out what's wrong with the sensors or wiring.

You can test your sensors with a sketch that prints the value of milllis when the sensor output changes state.

Switches are too reliable for that kind of test. I think that the best way to test that is in real time with real sensors - only that way I can get rid of all the problems.

One problem is that your code does something that I would try to do myself - and it is not working. Probably I need completly new idea.

EDIT: God I am getting mad! And if I can't figure what to do I will start breaking things (starting with that sensors) >:( :frowning:

#define SENSOR1 3
#define SENSOR2 4


int sensor_last1 = 0;
int sensor_last2 = 0;
int prej1 = 0;
int prej2 = 0;

unsigned long sens1 = 0;
unsigned long sens2 = 0;

void setup(){
 pinMode(SENSOR1,INPUT);
 pinMode(SENSOR2,INPUT);
 Serial.begin(9600);
}

void loop(){
if ((analogRead(SENSOR1) > 120) && (prej1 == 0))
{
  sens1 = millis();
  prej1 = 1;
}

if ((analogRead(SENSOR2) > 120) && (prej2 == 0))
{
  sens2 = millis();
  prej2 = 1;
}

if ((sens1 > sens2) && (sens2 > 0))
{
  Serial.println("PRVI");
  prej1 = 0;
  prej2 = 0;
  sens1 = 0;
  sens2 = 0;
}
else if ((sens2 > sens1) && (sens1 > 0))
{
  Serial.println("DRUGI");
  prej1 = 0;
  prej2 = 0;
  sens1 = 0;
  sens2 = 0;
}
}

This code - works for the second sensor - I don't get any strange activations or anything. For the first sensor it doesn't work as well - I get activation of second and first sensor or I get first sensor activated twice. I am not using any timeouts at this point. And after each try I need to restart arduino for some reason to get it working again.

EDIT: I think that I finaly got it! Combination of your code and mine seems to work. But no one must walk in after the other (there needs to be a 0.5s gap). And there is still a problem with too long cable - this is working using shorter cable but I think that it will stop if I put the long one back.

EDIT2:

#define SENSOR1 3
#define SENSOR2 4

#define SENSOR_TRIGGER LOW

#define TIMEOUT 2000 //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;
int prej1 = 0;
int prej2 = 0;

void setup(){
 pinMode(SENSOR1,INPUT);
 pinMode(SENSOR2,INPUT);
 Serial.begin(9600);
}

void loop(){
  if( (analogRead(SENSOR1) > 120) && (prej1 == 0)){
       sensor1Time = millis();
       timeout = sensor1Time+ TIMEOUT;
       prej1 = 1;
  }
  if(( analogRead(SENSOR2) > 120 ) && (prej2 == 0)){
        sensor2Time = millis();
        timeout = sensor2Time + TIMEOUT;
        prej2 = 1;
  }
  if( sensor1Time > sensor2Time && sensor2Time > 0){
      Serial.print("sensor 2 triggerd first, time is: ");
      Serial.println( sensor1Time -  sensor2Time);
       sensor1Time =  sensor2Time = 0;
prej1 = 0;
prej2 = 0;
delay(500);
  }
  if( sensor2Time > sensor1Time && sensor1Time > 0){
      Serial.print("sensor 1 triggerd first, time is: ");
      Serial.println( sensor2Time -  sensor1Time);
       sensor1Time =  sensor2Time = 0;
prej1 = 0;
prej2 = 0;
delay(500);
  }
  if( (millis() > timeout)  && ( sensor1Time || sensor2Time) ){
    sensor1Time = sensor2Time = 0;
    prej1 = 0;
prej2 = 0;
  }
}

This is the working code. And as far as I can tell it is doing great. Two people one after the other going in are working too. Now the special events (someone wawing at the sensors, someone standing between the sensors...). I know that I can't have everything planned but I would like to get those "special events" to 0. Oh and how to get this working on a long cable?

EDIT: Another problem. Long cable + daylight don't go well together. Sensors aren't activated because of the daylight.