Ultrasonic sensors & motor

h3rtz:
I is only one motor and the sensors are placed at different positions along the rail

Now we are getting somewhere.

So, the motor can move the payload in a straight line and in doing so it passes 4 sensors. OK so far. What is it that the sensors will detect ? I assume that you want the payload to be moved to the position nearest the sensor that has detected something. Is that correct ?

Yes that is correct. How could this be done?

What is it that is being detected by the sensors ?

The sensors point out from the side of the rail like in the included picture and looks for people

Finally we have a description that almost makes sense.

To summarise, the payload is delivered to the position of the sensor that detects a person

In order to do that the program will need to know where the payload is positioned. Has the system got any form of output to indicate the position of the payload ?

It is tempting to suppose that the motor could be run for a specified time to move to a known position but that will not be accurate.

I thought I would use another ultrasonic sensor to look at the payload from one of the ends of the rail and thereby know the distance it is at from that end, to determine its position.

I was thinking this would work since we know the positions of the sensors and the distance from they end that they are at.

h3rtz:
I thought I would use another ultrasonic sensor to look at the payload from one of the ends of the rail and thereby know the distance it is at from that end, to determine its position

That may not be very reliable but it gives you something to work with.

Here is some pseudo code to think about

start of loop()
  for loop to read the 4 side sensors
    if any of them read close enough to trigger an action
      set a boolean variable to true
      if the reading is lower than the lowest reading so far
        save the for loop index value
      end if
    end if
  end for loop

  if the boolean variable is true //found a nearby person
    use the previously saved loop index variable as an index to an array of distances
    and read the target distance from the array
    
    read the end sensor distance
      if the distance is reasonable
        if the distance is less than the target distance
          motor direction is positive
        else
        if the distance is greater than the target distance
          motor direction is negative
        else set targetReached to true  //already at the right distance
        end if check on direction
      
        while targetReached is false
          run the motor in the required direction
          read the end sensor distance
          if motor direction is positive and sensor distance is greater or equal to target distance
            targetReached = true
          else
          if sensor distance is less than or equal to target distance
            targetReached = true
          end if check on motor direction
        end while
      end if distance is reasonable
  end if found a nearby person      
end of loop()

There is much detail to fill out, the logic may be wrong and using a sensor at the end of the track is not to be recommended

It's some very interesting pseudo code, but I don't quite know which commands to insert in the slots. I do think I understand the principle behind the code though.
I would love an example of the code and what commands to use, with some random variables that I can change to fit my needs. Sorry if I'm asking too much, but I would appreciate the help.

Start with the part that reads the 4 sensors and works out which one is returning the lowest value. Don't even bother with using a for loop to start with. Read sensor 0 and save the value that it returns as the smallest value and zero as the lowest sensor number. Then read sensor 1, compare it with the lowest value read so far and if it is smaller then save the value as the new lowest and 1 as the lowest sensor number. Do the same for sensors 2 and 3 then print the lowest value and the lowest sensor number.

Test the code and the hardware by running it and placing an object in front of the sensors. Does it give consistent and sensible results ? What happens when there is nothing in front of any of the sensors ? My pseudo code mentioned that you need to check whether the end sensor reading is reasonable later in the code but you must do that for the side sensors too. If no object is sensed then you are likely to get a nonsense answer such as zero. Experiment to find what the sensible range is and test the readings as you take them to see that they are in the sensible range before you save a new lowest reading and sensor number. If none of the readings are in the sensible range then print a message saying so.

This may all sound very tedious, but it is necessary to make sure that the sensing is working. Once that works you can move on to positioning the payload and you will have learnt a lot along the way. As you change your program save it often with a new name so that you can go back one or many stages.

I will not write the code for you as it is your project and in any case I don't have the hardware to test it.