Encoder.changed(void (*callback)()) - What do I put in for callback?

I’m attempting to write an odometry program, and what I am reading is that I need to calculate the changed value in the encoders from the last time the motors ran. Does this command do this? And what do I put in for it?

I think you forgot to add the line of code.

It should look like motor.position(degrees); if I remember correctly.

This question is easily answered by a quick trip to the VEXcode Doxygen.


Read the entry for that function and see what it says.

Uh, I'm still confused

This is what the function does:
“Calls a function when the encoder value changes.”
This is what you want it to do:

Hence, no, this is not the command to do this.


There is no specific command for this.
The encoder.changed function allows you to register a callback function that is called every time the encoder has a new value, that’s not what you want.

To calculate how much the encoder has changed you will need to subtract the current value from a previously saved value.


Like this?

RightDriveEncoder.value() - RightDriveEncoder.rotation(degrees);

But It doesn’t work, it says the “-” is useless.

You would have to save the value as a variable, then subtract from that. For example:

int change = 0;
int currentValue = 0;
int previousValue = 0;
     currentValue = RightDriveEncoder.value;
     if(previousValue = 0){
          previousValue = currentValue;
     change = currentValue - previousValue;
     previousValue == currentValue;  
     wait(100, msec);

Thank you for that. The previousValue, though, is not set up to actually hold the previous value. Any idea how to do that?

It would hold the previous value. As your can see, at the end of the loop, it will set the current value at that time to be the previous value. When the loop restarts, that value becomes the previous value and a new value would become the current value.

1 Like

Ohhh, I see what you mean. Thanks for all your help, it’s really a relief after multiple practices of just feeling really stuck.

Would this work for autonomous code?

RightDrive.spinFor(forward, 1, turns, false);

waitUntil(RightChange = 1);
vex::task::sleep (50);

What is RightChange? Also, there really is no point in programming the encoders if you are just going to use the spinFor function. (maybe odometry?)

RightChange = RightCurrentValue - RightPreviousValue;

Yes that would work. I am not sure if it is using degrees or rotations, but that would work. But, the spinFor function already exists in your program so I am not sure why you are using the waitUntil.

It is sort of a backup. We’ve found the rotations to be very unrealiable hence the reason why my captain cursed me with the task of coding odometry. And yes, I know the program isn’t odometry, I have no idea how to do that.

1 Like

comparison is ==, so that code should be

     if(previousValue == 0){
          previousValue = currentValue;

A safe way of having the compiler notice mistakes like that is to flip around the comparison to this.

     if(0 == previousValue){
          previousValue = currentValue;

then a single = will throw an error.


Thank you for pointing out that I put a single = instead of a double ==. My mistake. I edited my original post.

1 Like

Also, when it says RightDriveEncoder.value, it needs to have parenthesis at the end (RightDriveEncoder.value())