# Help Programming Shaft Encoders

We have almost finished our robot and would like to use sensors on our arm so that we can easily stack on Stationary Goals. We have a Chain Bar Lift so we cannot use a potentiometer as it only has 250° of turning. So instead, we have decided to use Shaft Encoders on our lift since they have 360° of turning. So i was wondering how i would go about programming the shaft encoders on the arm so that it can go straight up with the click of a button. A sample code would be nice. Thanks in advance.

You’d have something like this, assuming button 6U is the one you want to press to get it to the right level:

``````if Btn6U == 1 {
while (SensorValue[armEncoder] < targetClicks) {
motor[armMotor] = 50;
wait1Msec(20);
}
}
``````

However, if it’s possible that your cone arm will approach its target value from either side (flip up from front of robot, or flip up from back of robot), then you’ll need to think about that too. This is a simplistic way you could do that (hence not the most efficient, but you can see the logic more easily this way).

``````if Btn6U == 1 {
// if flipping up from front of robot, say
if (SensorValue[armEncoder] < targetClicks {
// run motors until encoder gets to target
while (SensorValue[armEncoder] < targetClicks) {
motor[armMotor] = 50;
wait1Msec(20);
}
}
// if flipping up from back of robot, say
else if (SensorValue[armEncoder] > targetClicks {
// run motors the other direction until it hits target
while (SensorValue[armEncoder] > targetClicks) {
motor[armMotor] = -50;
wait1Msec(20);
}
}
}
``````

Keep in mind that if this code is used in driver control in the main while(true) loop, then all other joystick buttons will be locked out until the arm gets to its target. As such, this is the perfect thing to put into a RobotC task, which will not lock out the other joystick buttons. (See this article I wrote on tasks if you’re not familiar with them)

Addition to my comments above. You’ll want something after the above loops to set the motors back to 0 so they actually stop when they get to the target.