Shaft Encoders

My team is trying to program shaft encoders on our robot, but for some reason our program is not working. If you could help us that would be great! Thank you.

rightEncoder is on ports 2 and 3.
leftEncoder is on ports 4 and 5.

Here is our code:

task autonomous()
{
if(SensorValue(autoSwitch) == 0)
{
int distance;
distance = 720;
SensorValue[leftEncoder] = 0;
while(SensorValue[leftEncoder] < distance)
{
motor[motor2] = 127;
motor[motor3] = 127;
motor[motor4] = 127;
motor[motor5] = 127;
}
motor[motor2] = 0;
motor[motor3] = 0;
motor[motor4] = 0;
motor[motor5] = 0;

}
else
{
if(SensorValue(autoSwitch) == 0)
{
int distance;
distance = 720;
SensorValue[rightEncoder] = 0;
SensorValue[leftEncoder] = 0;
while(SensorValue[rightEncoder] < distance && SensorValue[leftEncoder] < distance)
{
motor[motor2] = 127;
motor[motor3] = 127;
motor[motor4] = 127;
motor[motor5] = 127;
}
motor[motor2] = 0;
motor[motor3] = 0;
motor[motor4] = 0;
motor[motor5] = 0;
}
}
}

What exactly is not working? Have you checked in the debug window that the encoders are functioning and counting up?

I’ve also made a few small changes in the comments below, while most of them are just style things some might be your issue.

task autonomous()
	{
	if(SensorValue(autoSwitch) == 0)
	{
	int distance; //You can make this "int distance = 720;" to make it look nicer
	distance = 720; //Also if this is a value you plan to use often, define it once at the top
	SensorValue[leftEncoder] = 0;
	 while(SensorValue[leftEncoder] < distance) //Use abs(SensorValue[leftEnc]) so you don't need to worry about it being fwd or bwd
	 {
	  motor[motor2] = 127;
  	motor[motor3] = 127;
  	motor[motor4] = 127;
  	motor[motor5] = 127;
	}
	motor[motor2] = 0; //If you just set the motor to 0 the robot will continue to coast. Use a small value in the opposite direction like -10
	motor[motor3] = 0;
	motor[motor4] = 0;
	motor[motor5] = 0;
	

}
else
{
	if(SensorValue(autoSwitch) == 0) //Not sure what this is, you've checked the same thing above? Also use else if instead of an if in an else.
	{
//Same things here with absolute value, reverse power, etc.
	int distance; 
	distance = 720;
	SensorValue[rightEncoder] = 0;
	SensorValue[leftEncoder]  = 0;
	 while(SensorValue[rightEncoder] < distance && SensorValue[leftEncoder] < distance)
	 {
	  motor[motor2] = 127;
  	motor[motor3] = 127;
  	motor[motor4] = 127;
  	motor[motor5] = 127;
	}
	motor[motor2] = 0;
	motor[motor3] = 0;
	motor[motor4] = 0;
	motor[motor5] = 0;
}
}
}[/CODE]

We have not checked the debug window. The motors are not spinning at all.

In the debug window, make sure the sensor autoSwitch is 1, otherwise nothing will happen.

Also spin the wheel by hand to check the encoder and make sure it counts up when you turn it forwards. If it counts to negative that doesn’t matter, you just need to use the absolute value of the sensorValue, or switch where the 2 wires are plugged in.

Using the absolute value would be bad. I would suggest either multiplying by - 1 every time you read it or flip the wires to invert the direction of the encoder.

I will check the debug window today. Thanks for your help!

Is autoswitch a button? The if autoswitch stuff is probably what is messing you up.

Why is it bad? I use it always even though positive is forwards, if nothing else it makes sure your auto will still work if you have to switch the encoder or make some changes and get a plug mixed up.

Well it just lends itself better for turning all those drive straights for encoder distance into a function. In which case 1000 and - 1000 are different things.

My team also had this problem, we discovered that you can only plug it into odd and even, not even and odd.
EX. you have it in 2&3, and 4&5,
but it has to be in 1&2, 3&4, etc.
hope it helps

Well try that. We got the motors spinning, but they won’y stop.