I’m currently working with a potentiometer(for the first time) and I’ve run into a problem. The program should tell an arm to rotate upward when I press a button, but only until it hits a certain position determined by the potentiometer. And it almost works. However, the arm will not stop exactly when it should. It stops in the exact same place(roughly halfway through its full arc) whether I set it to stop at 1000 or at 4000. If anyone has run into a similar problem or knows what I can do to fix this, the help will be appreciated. Code is posted below in RobotC.
#pragma config(Sensor, in1, , sensorPotentiometer)
#pragma config(Motor, port1, , tmotorVex393_HBridge, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main()
{
int pot = 2000;
while(true)
{
if(vexRT[Btn6U] && SensorValue(in1) < pot) //button press while the arm
{ //has not yet reached its full arc
motor[port1] = 75;
}
else if(vexRT[Btn6D])
{
motor[port1] = -75;
}
else
{
motor[port1] = 0;
}
}
}
Also, I may not have explained it very well. If you’re confused, I can try to explain better.
Have you used the Sensor Debug Windows to figure out exactly the value it needs to stop at? Some potentiometers might also be broken (it’s happened to us before), so in that case you might be getting random values.
I should have mentioned, I don’t have access to the debugging tools. Or any more potentiometers I only have a few supplies right now because school is out.
Update for posterity:
I replaced the potentiometer with a brand new one out of the box, and it worked perfectly. I guess there’s a lesson in that; check if your parts are broken before doing anything complicated.
And I don’t think RobotC cares whether you use brackets or parentheses. Thanks for your help!
Well, some commands like motor] require brackets while others like clearTimer() require parentheses. The compiler will throw errors if you use the wrong symbol. Generally, if you’re accessing or assigning a value to something, like SensorValue or motor, you use brackets, and parentheses for everything else.
First off, motor] and SensorValue] are not commands. They are arrays. That’s why we can write to them in addition to reading from them. And because they are arrays, they get brackets. If you use the wrong punctuation, RobotC should throw a warning, but its compiler is intelligent enough to replace parentheses with brackets and vice versa.
And yes, always test your sensors (and motors) before using them. I’ve learned that the hard way (several times).
With Boolean values you don’t need to really have a condition for it them since it has two states on and off if you put it
if(boolean value){
}
It will run what’s in the curly brackets if the Boolean value is set to true
This is correct. You need to have a seperate condition for each part of the system or the compiler won’t know what value to check. Also, the way you know whether to use parentheses or brackets is the purpose of the statement. If the statement is a function or you are telling the program to “do” something, use parentheses. If the statement is using a value or variable, use parentheses.
The code that is listed at the beginning of this post works as intended, regardless of what the most proper way to write it would be.
It has always been my understanding that “if(vexRT(Btn6U))” does the same thing as “if(vexRT(Btn6U == 1))”. That may not be exactly correct, but in this case, it causes the program to do exactly what I mean for it to do.
The last case is interesting, we sometimes do that so as not to make one of the classic C mistakes.
This is an error (notice single = rather than == ) but in most compilers is still a valid condition (I say most as ROBOTC has special logic to catch this and flag as a warning).