19:1 Metal Gearmotor 37Dx68L mm with 64 CPR Encoder Arduino Use


I am currently building an autonomous robot and need some help getting it to drive exactly where I want it to go using rotary encoders. Can anyone please help me with Arduino code to make two of the gear motors (described above) use the encoder values to travel - for example - 200 counts each and then stop. I am using two Pololu high power motor drivers and an Arduino Mega. I can get it to read values but do not known how to tell the motors to essentially be ‘on’ until they reach a certain encoder value. This is the only way we can get our device to go straight.

Any help is greatly appreciated!

Thank you.


The code will depend on how you have everything connected in your system. Do you have code that moves your motors right now? In general, you would add some logic to your code that checks the encoder readings periodically and turns the motor drivers off when the desired values are reached. A more advanced control mechanism would be a PID algorithm that checks the encoder values and slows the motors as the encoders approach the desired values. If you search, you should be able to find a number of code examples for the Arduino that use PID.

If you post your existing code here and diagrams or pictures that show how everything is connected, I might be able to offer more specific advice.