Potentiometer Trouble (and discussion on RobotC logic)

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;
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;

	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 :confused: I only have a few supplies right now because school is out.

But hey, 100th post, nice!

Well I don’t know if this could be the problem, but I’m pretty sure it needs to be


, with brackets, not with parentheses. Other than that, the logic looks sound.

Congrats on 100!

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.

I just meant that in this particular case, it didn’t give me an error for having parentheses.

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).

You guys are far better programmers than me, but shouldn’t the if statement say:

if(vexRT[Btn6U] == 1 && SensorValue [in1] < pot)

instead of

if(vexRT[Btn6U] && SensorValue(in1) < pot)?

There is no condition for VexRT[6U] in the original program plus, as others have pointed out, the Sensor Value needs square brackets…


else if (vexRT[Btn6D])

should be replaced with

else if(vexRT[Btn6D] ==1)

At least, that’s what I think. HTH!

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.

Your understanding is correct (except for the misplaced ‘(’ above).

The expression needs to evaluate to either true (ie. any non-zero value) or false.

So all the following would be valid (and I’m sure there are more).

if( vexRT Btn6U ]  == 1) {

if( vexRT Btn6U ]  !=  0) {

if( vexRT Btn6U ]  ) {

if( vexRT Btn6U ]  >  0) {

if( 1 == vexRT Btn6U ] ) {

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).

if( vexRT Btn6U ]  =  1) {

but this will cause a syntax error

if( 1 = vexRT Btn6U ] ) {