I’m trying to make a conveyer belt that uses and encoder to travel a distance, stop, and then continue. The robot currently goes until the encoder reaches 300 and then it resets to 0 and stops. Can anybody help solve the problem?
I’ve formatted your code for readability. I encourage you to use proper indentation; it make your code much easier to understand. Additionally I encourage you to use the code tags which can be done by placing a ``` before and after your code.
At first glance it looks like the issue is that you reset the encoder to 0 before the motor has finished turning. This means that the motor will continue to coast for a short time after you have reset the encoder, leading to its value being somewhat greater than 0. Since you are checking only for the encoder value to be exactly 0 or exactly 300, it does not do what you want it to do. I suggest you utilize inequalities like so:
I’ve also removed some unnecessary brackets inside the second if statement, and have added a delay to your loop to prevent the main task from hogging the CPU.