Programming help/sample code for programming motor encoders

Hi I am using the vex integrated motor encoders on my chassis motors and I would like to use them to move an exact distance in my autonomous can I get some sample code and some help on how to do this. Keep in mind I have no experience with these or shaft encoders

We don’t actually have the integrated encoders, but I assume that the code should be similar. they’re fairly simple to program. At the very least, all you have to do is set your motors to drive forward in a while loop that will end when the encoders reach a certain distance. It should look like:

while (quad_encoder_value < distance)
 drive forward
 get quad_encoder_value
stop motors

I use easyC, so sorry about not having the correct syntax, but it should be easy to figure out what to put in. This code (with modifications) can be used to drive forward, backward, and turn. Another thing that the quad encoders are good for is driving straight. If you have problems with one side driving faster than the other, you can use a code like this:

while (quad_encoder_right < distance && quad_encoder_left < distance)
 if (quad_encoder_right > quad_encoder_left)
  slow down right side
 elseif (quad_encoder_left > quad_encoder_right)
  slow down left side
  go full speed

I can’t test this code right now but, the basics should work. Hope this helped! :slight_smile:

I believe you can find sample programs under File>Open sample program>quad encoders or something similar. Code from the old quad encoders can be reused with the IME’s, but you just have to replace “SensorValue[encoder]” with “nMotorEncoder[motor that encoder is attached to]”.

nMotorEncoder[port1] = 0; //reset encoder to 0
while(abs(nMotorEncoder[port1]) < 1000) //repeat while the absolute value of the encoder is < 1000
  motor[port1] = 127; //motor forward
motor[port1] = 0; //stop the motor since the while loop has exited, meaning the encoder has traveled more then 1000 counts

Here is some basic code. abs(nMotorEncoder[port1]) is used because when going backwards, the encoder value becomes negative. Using abs() would always convert it to a positive number. To move backwards, just assign a negative motor power value.

I have attached a small encoder test sample which should help you get this working. Here are the key objectives…

There is a “wait 25” line in the code, you need this to allow the encoders to update values properly. This small example only drives a single motor for 1 second.

Init the encoder at the top.
Get an intial value from the encoder
Create a loop
Read the encoder
Perform actions you want until the encoder reaches the value you want
When the value is reached, drop out of the loop

Drop me a message if you want some additional help. I could create a quick example in Easy C and send it. Good luck.
Encoder Test (46.4 KB)