Can you take a video and/or pictures that actually show how the machine work.
Does every teeth equate to one slot movement of that plate?
When it goes over by two teeth, what is the counter value?
Are you saying there are two sensors, one to count and the other to stop? In the video you showed there's a sensor positioned to count teeth and in the picture you showed a sensor that is activated by a rotating plate.
This is what I understand:
Start command starts the motor, the gear turns and rising edge of teeth increment the counter. When the count is reached the motor is stopped but the counter will not reset until falling edge of that Prox that is activated by that rotating plate. So if the machine coasts the counter will register additional counts and if it continues to coast past that plate then the counter will reset.
If that is the case then remove all that code and put code similar to Network 1 and use the counter to stop the machine when count value is reached, just that one sensor. This will help you determine if the machine coasts or not.
I hope "ES" does not mean Emergency-STOP and I hope it is not wired in a similar way to how it is used in code. There are serious design and safety consideration for any safety device and an emergency stop could mean life or death.
You are troubleshooting too many things and making too many changes at the same time, some of it is helpful but it is always better to work on isolating problem areas.
I would start with cleaning up your code:
- Do not use M-Bits to avoid any possibility of memory overlap with other tags including clock bits.
- If you want global then just put them in a global DB and while you're at it give your existing DB a name that reflects its purpose; "." (dot) is not a name.
- Do not use space in your names.
- Give all your variables names that reflect their purpose so people reading your code do not have to keep going back and forth trying to figure out what "Tag_32" is supposed to do.
- Work with one issue at a time. Small bits of code that can be tested and then add to it incrementally and test every segment you add.