Arduino mega sensing an objects direction of travel.

I would like some help and advice on coding two inductive sensors that are spaced apart to see the start and finish of an object passing under them.
The sensors are required to identify the direction of the object travel, either to the left or to the right. Only one object is active at any one time.
What is the best method to detect the sequence below? as most examples i have found refer to a single button press or a key pad scan which is not really what I require
.
The sensor code below shows the sequence for an object moving from left to right.
0= no object.detected.

Left Sensor/ Right Sensor/ Action.
0/0/ No action
1/0/ Direction sensed left to right.Turn on LED.
1/1/ No action.
0/1/ Start motor slow speed drive.
0/0/ motor full speed drive.

The sensor code below shows the sequence for an object moving from Right to Left. 0= no object detected.

Left Sensor/ Right Sensor/ Action.
0/0/ No action
0/1/ Direction sensed Right to Left. Turn on LED.
1/1/ No action.
1/0/ Start motor slow speed drive.
0/0/ motor full speed drive.

I started using IF statements to test the sequence:

For Left to Right travel.

checkValue = 0;


IF ( sensorLeft == 0) && (sensorRight == 0) && (checkValue == 0)
{
CheckValue = 1;
}
ELSE IF ( sensorLeft == 1) && ( sensorRight == 0) && (checkValue == 1)
{
checkValue = 2;
directionLed();
}
ELSE IF ( sensorLeft == 1) && (sensorRight == 1) && (checkValue == 2)
{
checkValue = 3;
}
ELSE IF ( sensorLeft == 0) && (sensorRight == 1) && (checkValue == 3)
{
checkValue = 4;
motorSlow();
}
ELSE IF ( sensorLeft == 0) && (sensorRight == 0) && (checkValue == 4)
{
checkValue = 0;
motorFast();
}

ELSE
{
checkvalue = 0;
}

Is this the best method or should I use a multiple array with the sequence stored in the array and just compare the inputs to the array.
Any advice on comparing two inputs would be appreciated.
Thank you.

Have you noticed that these are the same?

0/0/ No action
0/0/ motor full speed drive.

You can't do both at the same time, right?

The ONLY way to determine direction using two sensors is to know WHEN each one is toggled. Nothing in your code knows when anything happened.

Hi,
What is the application?
Can you draw a diagram of the project or a picture of a hand drawn diagram in jpg, png?

Thanks, Tom... :slight_smile:

Thank you all for your input. It is difficult to describe sometimes what I am trying to achieve but
basically the project is an object on rails that can move either from the left or from the right. Depending on the direction of travel different actions/processes are performed.
Without an object under either of the detectors for the sake of simplicity I have referred to them as sensor output as logic zero.
So with the attached sketch as the object moves on the rails towards the right it will break the left sensor first changing this to a logic 1. The right sensor will still be un-triggered and indicate logic zero.
As the object keeps moving to the right each sensor will change their state as per my logic table.

STATE 1:0/0/ no action
STATE 2:1/0/ direction detected.
STATE 3:1/1/ no action/
STATE 4:0/1/ turn on a motor for object cleaning at slow speed.
STATE 5:0/0/ turns on a motor for object washing at full speed.

there are two 0/0 states but they are different and the second (STATE 5) doesn't happen unless the logic sequence is followed.
At STATE 2: I was suggesting reading the pins the left and right sensor are connected to, to determine if the object is moving to the right. If STATE 1: was 0/0 when the pins are read at STATE 2: they would be different to the last state. The checkValue was to keep check of where we are between STATE 1: and STATE 5:
What I am trying to work out is the best way to read and code each sensor at the same time and move through the STATE 1 to STATE 5.
hope that makes sense!

object detection.JPG

What I am trying to work out is the best way to read and code each sensor at the same time

You can't. You can read them one after the other, very quickly, but you can't read them "at the same time".

If the object can move in either direction, what happens if it triggers the right sensor first? Is that what you mean by state 4?

If so, then it seems that the object can not actually enter the scene from either direction, and your program is really quite simple. You have something to do when the first sensor becomes triggered. You have something to do when the second sensor becomes triggered. You have something to do when the first sensor is triggered again.

I suggested an answer to a very similar question earlier today

The relevant part is as follows

You need to figure out an interval of time which is longer than the longest time for a vehicle to traverse the sensors.

After a long interval when a detector is triggered record the time (millis() ) and note which detector was triggered. Then record the time when the 2nd detector is triggered. As long as the difference between the times is shorter than the long interval you can assume it is a legitimate detection of a vehicle. The direction will obviously be identified by the detector that was triggered first.

It will probably make more sense if you read the other Thread to see if it relates to your problem.

...R

I have tried to keep this simple so not to confuse everyone, but an object on the rail can move past the detectors from the left or right.
STATE 1 would be the same for the object entering from the left or right.
STATE 2 for an object entering from the left would be 1/0
STATE 2 for an object entering from the right would be 0/1
In the actual program a decision to call a function would be made at this point to run a function for a left moving or right moving object and complete its remaining logic states.
I was really asking how I could read two input pins at the same time but this is not possible so I would have to read each pin then do some logic to determine if the object is moving left or right and then continue reading the input pins from the sensors as I want them to complete the states from 3 to 5.

Would it be easier to look at this as two buttons on separate pins and it is required that they are pressed and released in my sequence.

0/0
1/0
1/1
0/1
0/0

If they are not pressed in this sequence they do nothing.
If they follow this sequence to the end then they do something.
How would you code that?

Hi,

Thank you all for your input. It is difficult to describe sometimes what I am trying to achieve but
basically the project is an object on rails that can move either from the left or from the right. Depending on the direction of travel different actions/processes are performed.

Thanks, almost there, what is the application in the real world.
If we now exactly what you are trying to do, we can give you more meaningful answers.
How fast is slow and fast, are you controlling speed and direction as well as STUFF?
You may not have enough sensors, you may need different types of sensors.

You have an OBJECT??? it moves left to right, and does stuff, WHAT? When it encounters these sensors.

What does it do?
Wash windows.
Weld metal.
Print stuff.
???
Sorry BUT, you only get the quality answers from the quality of the questions you ask.

Thanks... Tom... :slight_smile:

Tom,

thank you. Sorry for being vague but the project is at an early stage and does have a lot of further processes to be designed.
The object will move past the sensors at a fixed speed either from the left or right.
for the left to right object movement, when direction is detected and the sensors are triggered as I have indicated, at STATE 4:0/1 a scrub cleaner is used on the object. at STATE 5:0/0 a high pressure wash is applied to the object. After this the object leaves this section of rail and goes for x-ray inspection.

That is about it for now but how to read both sensors and decode them to my my sequence is really what I would like best method for.
I have thought to compare left and right sensor to saved values to detect any change.
So if stored values are both zero then both sensors are read, if one of them changes to logic 1 and compared to the stored values it would give direction. That was why I asked if my sequence could be stored in an array, read the sensors and do a compare.

Hi,

STATE 1:0/0/ no action
STATE 2:1/0/ direction detected.
STATE 3:1/1/ no action/
STATE 4:0/1/ turn on a motor for object cleaning at slow speed.
STATE 5:0/0/ turns on a motor for object washing at full speed.

  • Your Object moves in from left. LEFT and RIGHT sensor RESET.
  • Your Object SETS sensor LEFT, RIGHT sensor RESET.
  • Your Object SETS sensor LEFT and RIGHT, (Direction Determined)
  • Your Object RESETS sensor LEFT, SETS sensor RIGHT, (Activate Scub Cleaner)
  • Your Object RESETS sensor LEFT and RIGHT, (ACTIVATE HIGH PRESSURE WASH)

Find below what I think you are trying to do if the object is only moving left to right, if moving right to left your wash and scub will be back to front.
This just off the top of my head during lunch at work, I'll email you the Invoice.

Tom.... :slight_smile:

mgmidget123:
I was really asking how I could read two input pins at the same time but this is not possible

If you use digitalRead() to get the values of two pins there will be an interval of a few microseconds between the two reads. For your application I suspect you can treat that as "at the same time"

If you really need to read two I/O pins at the same time you can do so with PINx - read about port manipulation

...R

Tom,

thanks. Cheque is in the post!!
The process is sensitive to the change from the previous state, that is why I am trying to detect the states in my sequence and step through them. Attached sketch is what is required. The wash is activated but switches itself off after a set time so no other sensor to turn it off is required.
An object entering this area from the right will not be washed or scrubbed. when the object passes under the right then left sensor it will detect the object is moving left and start a different process further to the left that bead blasts the object. The coding will be similar to the left to right movement so if can get my head round the left to right object detection then I will be sorted.
Could I not store my sequence in an array and then use digitalread() of each pin to check against the array and step through. not sure how to compare input to an array? any pointers?
thanks for your time and everyone else who has been offering advice, it is appreciated and must bug the hell out of you experienced programmers but we don't all see through the fog as clearly as you.
Read and more reading is the best way but a kick sometimes gets you back on the track.
thanks again.
M

object detection.JPG

Without reading the preceding posts, I did exactly this about 7-8 years ago... with opto-interrupters on a PIC driving a shuttle valve.

A very simple state-machine is the answer, and you have FIVE possible locations of the carriage along the path of travel.
LEFT - Sensor 1 - MID - Sensor 2 - RIGHT

Based on which sensor is triggered, and the motor direction immediately prior to the sensor being triggered - you can identify all five locations. If a sensor is 'steady on' - of course you know the carriage is not moving, and the carriage is directly above the sensor.

If you know the position is not going to change when power is off, you can save the position to EEPROM before shutdown, then when power is restored, proceed as if nothing happened.

If there is a chance of movement, or you need to re-initialise the state machine - use a learning sequence to make baby-steps in each direction until you 'find' a sensor - then carry on as normal.

Be careful that you don't let the carriage run off the end - or you may need additional end-stops or sensors.

It's also worth timing the motor-run duration against a known maximum period - to ensure the carriage is actually moving - and not jammed in one location (?overheating, burned out or 'just stuck'!).

Using all the above - you can detect a blocked / damaged sensor, drive system and other conditions.