Conceptual Question - Connecting a Digital Fiber Sensor, Preset Counter, & Servo

Hello Everyone!

I have a conceptual question about the general logic to setting up my code.

My goal is to have a digital Fiber sensor count parts and then relay the count number to a digital preset counter that displays the total amount. From there, my goal is to have a servo motor linked to the sensor/counter to control the pathway for the parts depending on a maximum quantity.

For example, I need the sensor to detect 100 parts that get sent to one box and then activate the servo motor to open up a new lane and detect 100 more to a different box. This process is needs to go back and fourth and, theoretically, should never turn off.

I have been able to connect the sensor, counter, servo, and Arduino Uno using a very simple test code. However, trying to identify the logic/process of connecting these devices have been my troubles.

If someone could point me in the right direction, I think i would be able to take it over from there.

Thanks!