PG Motor Encoder Question

My robotics class is programming in JAVA on a testbed we have. They have figured out how to read the encoder value from the PG motor, and can use that value in code.

They want to be able to have a motor stop running when a certain value is reached. They have set the code to a button on an xBox controller, and when they press the button the motor moves; however, if they keep the button pressed it goes past the encoder position. If they bump the button the motor will stop at the position they want. Any idea why that would be happening?

Can you post the code they have so far?

This is the code they have for their buttons. I have checked the count2 integer and it is working when the motor moves. They have also tried using while instead of if, and do/while, but neither of those worked, but it could be that we were doing them wrong.

if (count2 <=2000)
{
if (buttona == true)
{
testMotor.set(1);
}
else if ((buttona == false) && (buttony == false))
{
testMotor.set(0);
}
}

Can you post the entirety of the robot code? It helps to know the surrounding context.

I think the issue is that you don’t have an else{} statement for the first if. Try adding
else { testMotor.set(0);}
after the closing curly brace of the first if statement.

When you say the first if, I assume you mean the if (count <=2000), right? I’ll have them try that and see what happens

Well, I feel like an idiot now. Adding the else fixed the problem. Thank you

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.