Arduino Obstacle Sensors - Get Direction + Write to Serial

Hello community. As most of the topic starts here - I'm kind of new to Arduino and in programming in general or if not that new at least with basic knowledge. Yet I'm working on very simple project which sometimes looks very complicated to me.

General idea - smart barrier - computer vision software scan and verifies vehicles plate IDs. If it's not blacklisted (MySQL) it lifts the barrier. The application I did on VB.NET and the above works just fine. Now at the final stage I'm adding some additional controls and in particular with my question - 2 IR obstacle sensors. One (s1) under the barrier preventing closing if something is under and the second one (s2) a little after s1 to open the barrier if a vehicle is exiting (no exit control, only entering) and to detect if the direction is 'In' or 'Out'. That last part is where I'm bumping.

Somehow I need to realize the code where: If first (s1==HIGH) and then (s2==HIGH) then the direction is 'In'. Otherway around would be 'Out'.
Being under 'loop' completely messes my thinking... How to ask Arduino which of these 2 events has happened first in timely manner? Is it even possible?

And the second part - I need this "Out" flag to be 'Serial.Write'en so my VB application can read it and set the direction into the MySQL table. As all the rest of the VB-Arduino connection is in the direction VB to Arduino and for every case I use 'Serial.Open()', sent, 'Serial.Close()', do I need such opening and closing for the direction Arduino to VB? With only Serial.Write will it jam the port for eternity? Because VB does if not .Close().

I know I probably write not very understandable but I'd appreciate if anyone has any idea how to execute this. It just sounds so simple and yet it's bugging me so hard.

Thank you in advance.

Psssst - we can't see your code.

Yes, because for that part I still haven't got any. The best idea I achieved in my notes is:

string direction
if s2==high then
open()
delay(5000)
while s2==high
if s1==high then
direction = "out"
serial.write

Probably that would work but only if the vehicle has suitable length and passes the two sensors with suitable speed.

That last part is where I'm bumping.

Why? You have two different sensors to read, independently. When a sensor reports that it is triggered (a vehicle is blocking the light), you note that the event happened and when (millis()). You'll find that knowing when is actually sufficient. If when is not 0, the event happened. If it is 0, the event didn't happen.

Periodically, you check that both events have happened (both whens are not 0). If that is the case, you can determine the direction, based in which event happened first.

Speed has nothing to do with direction.

However, I do not think that your sensor positions will work. You need something BEFORE the barrier, to know that a vehicle wants to leave, so you can open the barrier. Once the barrier is open, you can know whether the vehicle is entering or leaving. But, you want to know whether to open the barrier for an incoming car - maybe yes, maybe no - and whether to open the barrier for an outgoing car - always yes.

PaulS:
Why? You have two different sensors to read, independently. When a sensor reports that it is triggered (a vehicle is blocking the light), you note that the event happened and when (millis()). You'll find that knowing when is actually sufficient. If when is not 0, the event happened. If it is 0, the event didn't happen.

Periodically, you check that both events have happened (both whens are not 0). If that is the case, you can determine the direction, based in which event happened first.

Speed has nothing to do with direction.

Thank you for the suggestion. That was my first clue but I think the (millis()) has something to do with speed. I've seen people slacking around in front of a barriers... :smiley: Probably it would work in 99% though.

PaulS:
However, I do not think that your sensor positions will work. You need something BEFORE the barrier, to know that a vehicle wants to leave, so you can open the barrier. Once the barrier is open, you can know whether the vehicle is entering or leaving. But, you want to know whether to open the barrier for an incoming car - maybe yes, maybe no - and whether to open the barrier for an outgoing car - always yes.

The position is as you're saying just by "after" I mean on the way in. On the way out it's "before".

I started thinking in the direction where I have 4 states - a combination from the states of the 2 sensors - 01,11,10,00 is the one direction and 10,11,01,00 is the other. I gave those to 4 variables:

void position() {
 if (digitalRead(sensorB) == LOW && digitalRead(sensorA) == HIGH) {
   position01 = 1; 
   delay(10);}
 if (position01 == 1 && digitalRead(sensorB) == LOW && digitalRead(sensorA) == LOW) {
   position11 = 1; 
   delay(10);}
 if (position11 == 1 && digitalRead(sensorB) == HIGH && digitalRead(sensorA) == LOW) {
   position10 = 1; 
   delay(10);}
 if (position10 == 1 && digitalRead(sensorB) == HIGH && digitalRead(sensorA) == HIGH){
   position00 = 1; 
   delay(10);}
}

void vehDirection() {
 if (position01 == 0 && position11 == 0 && position10 == 0 && position00 == 0) {
   direct = 0; }
   
 if (position01 == 1 && position11 == 0 && position10 == 0 && position00 == 0) {
   direct = 1; }
     
 if (position01 == 1 && position11 == 1 && position10 == 0 && position00 == 0) {
   direct = 2; }
     
 if (position01 == 1 && position11 == 1 && position10 == 1 && position00 == 0) {
   direct = 3; }
     
 if (position01 == 1 && position11 == 1 && position10 == 1 && position00 == 1) {
   direct = 4; }

 if (position01 == 1 && position11 == 0 && position10 == 0 && position00 == 0 && digitalRead(sensorB) == HIGH && digitalRead(sensorA) == HIGH){
   position11 = 0;
   position10 = 0;
   position00 = 0;
   direct = 0; 
   delay(10);
   position01 = 0; }

 if (direct == 4) {
   Serial.println("Out");
   delay(3000);
   position01 = 0;
   position11 = 0;
   position10 = 0;
   position00 = 0;
   direct = 0; }
}

I know it can be optimized but it works perfectly and I can't lie it no matter how I tried.