Shaft encoder to potentiometer programming help

Hi guys just a question how do you program a shaft encoder to do a job of a potentiometer because i wanted to put something on my chain bar which goes more then the range of a potentiometer so please help and thanks in advance

You cannot necessarily substitute one for the other. What I mean by that is that a potentiometer always (assuming not broken) provides an absolute position. An encoder only provides a relative position. So with the encoder you need to make sure it’s zeroed in the same place to begin with every time. You can then count encoder ticks from there roughly the same as you would read the potentiometer. You won’t get as fine a reading, but each degree should be more than sufficient. However, if you skip the zeroing, it won’t substitute, which also means it won’t substitute initially since you have to go through the zeroing.

Another option is to use gears or chains and sprockets (need not be high strength chain) to transfer the chain bar’s rotation to another axle (only used by the potentiometer) at a fraction of the rotation it goes through. Surely the chain bar doesn’t swing through significantly more than 360 degrees, right? So not even halving the rotation would put it well inside what a potentiometer can handle.

the first i dont really understand what you are trying to say


For the second option the thing is that we have our chainbar on top of a four bar so we dont have a full DR4B so it will be hard to put the sprockets

here is a pic

You can use the encoder similarly. It will work.

The really, really important thing is that during the pre-autonomous you set the chain bar to a specific position and then set the value for the encoder to whatever value you want that position to be. Alternatively, during autonomous you could swing the chain bar into some physical limit using time and set the encoder value once you’re sure it’s there. I don’t like the second option because it’s more likely to go wrong and because it uses up valuable autonomous time doing what should have been done earlier. Otherwise you’ll get different values for the same position. The reason is that then encoder does not record where it is. It records how much it has rotated. So if you start it in different spots, it has to rotate different amounts to get to the same desired angle, meaning you’ll need different

As for the coding, the first part would ideally be in pre-autonomous. Are you using an LCD? If so, you could supply a prompt asking for the driver to move the chain bar to a specific spot. You could use a button press to confirm it’s there. Reset the encoder immediately.

Now use it like a potentiometer, just remembering it has 360 ticks per revolution, so one tick per degree.

no i am not using an LCD


when you say that

what do you mean it will take time in autonomous


what do you mean into a physical limit using time

sorry if these are stupid questions this is my first year in vex so i dont know much

Without the LCD screen, I would still try to use pre-autonomous. If you have a free port, you could use an LED as an indicator, for instance. When the indicator turns on, you have 10 seconds to adjust the chain bar. After the 10 seconds are up, you turn off the LED and reset the encoder.

As for what I meant in autonomous, you could do the following. At the beginning of autonomous you swing the chain bar in one direction until it hits the rest of the robot and pins itself there. Let’s say this generally takes 0.4 to 0.7 s. You want to be sure, so you run the motors for 1 s. Just before turning the motors off, you reset the encoder. Then you turn the motors off. You know where the chain bar is for that encoder value because it’s somewhere it physically cannot move past because you’ve had the motors for the chain bar into that situation. However, of the 15 s of autonomous, you have now spent 1 s resetting the encoder, so you’ve spent roughly 7% of your time in autonomous doing something you could have done before autonomous.

What he basically meant was that Shaft Encoders and Potentiometers differ in a very fundamental way.

If you turned on the robot with the lift halfway raised using a potentiometer, it will detect its starting position as halfway raised. However, if you did the same thing with a shaft encoder, it would not be able to tell where the lift was at the start.

Shaft encoders always reset to zero at the beginning of the program while potentiometers do not.

What he meant by this is to use a gear ratio to reduce the turn speed of the potentiometer.

You stated that your main problem was that the lift moved too far for your potentiometer to work in the range. To solve this, you could place a small gear on the lift axis and connect a larger one it through a gear chain. Let’s say that you use a 36-tooth gear for the lift and connect a 60-tooth gear to it. Then, mount the potentiometer to the axle with the 60-tooth gear (I will attach an image with an illustration).

Because of the small to large gear ratio, the potentiometer axle will turn at about half the speed of the lift, meaning that the angle range should be small enough to fit into the potentiometer.

I hope this helped. If you have any more questions, feel free to ask for clarification.
gear ratios.png

By the way, this principle applies to chain and sprockets as well as gears.

+1 for everything above. Is your robot in the same position at the start of every match? If so, you can insert

SensorValue[encoderName] = 0;

into the pre_auton() function. That way, 0 will be whatever position the robot starts in. For us, our chain bar starts at the bottom and that’s where 0 is.