There are several ways to solve this problem.
You could map the contents of the belt in a data table, and use the pulse counter to drive a pointer around the table, populating it when a new item enters and removing the correct ones when items are diverted.
Or, you can keep track of each box in transit, and update the position of all of them every scan, then when the appropriate box's position falls within the range of the diverter, then kick it out.
The first method is slick, efficient, and hard to comprehend.
The second method uses looping, a little more indirect addressing, but is easier to understand.
A third method would be to store a list of gate opening positions for each gate, and populate the lists as soon as an item is identified.
This method would be to create a set of data for each gate such that as soon as a product is detected and assigned, you calculate the pulse count range required for the gate to open.
So, product A goes to lane 2, and the pulse counter is at 200. You know that gate two needs to open at pulse count 200+23. You add those values and store them at the next available position in the list of gate trigger ranges for gate two. You need a list becaue there can be more than one item assigned to gate two in transit at any given time. Since there is a list, then you need a pointer to the next open spot on the list.
Do the same thing for the other gate in the system.
Either method will be very good at controlling the system without the potential for getting out of sequence like some FIFO sorting routines can.
They will also deal quite well with the main conveyor stopping and reversing.
All these methods would use indirect addressing as Doug described above.
So, to go further, you need to know which method to pursue. And, it doesn't have to one of these ideas, because there are still other ways to go about this.
You also need to know how many items can be in transit at any given time.
Hope this helps.
Paul