Help with potentiometers

I am trying to use a potentiometer to move a lever up and down a certain distance when I press digital button 8. I created two variables for my button and a variable for the potentiometer. The program works, but the motors won’t stop at the specific spot I want. Also, my program only lets me use the button once. Please help. I have my program but I’m having issues with attaching it.

here is the code:
{while (1==1)
button_8_up=getjoystick digital (1,8,2)
button_8_down=getjoystick digital (1,8,1)
pot=get analog input 8
{if button_up_2==1
{while pot<600
motor 5, -30
}
motor 5, 0
}

{if button_down_1==1
      {while pot>400

motor 5, 30
}
motor 5, 0
}
}

If it’s being done in RobotC then it would be done something like this

while (true){
	//Value of Potentiometer
	int potValue = SensorValue[potLever];
	//Maximum and Minimum Potentiometer Values
	int maxValue = 600;
	int minValue = 0;
	//If ONLY Btn8U is pressed 
	if (vexRT[Btn8U] == 1 && vexRT[Btn8D] == 0){
		//Move the arm up until the potValue is equal to the Maximum value 
		while(potValue < value){
			motor[leverMotor] = 127;
		}
	}
	//If ONLY Btn8D is pressed
	if (vexRT[Btn8D] == 1 && vexRT[Btn8U] == 0){
		//Move the arm down until potValue is equal to the Minimum Value
		while(potValue > minValue){
			motor[leverMotor] = -127;
		}
	}
	//Stop the motor when it reaches either value
	if (potValue == value || potValue == minValue){
		motor[leverMotor] == 0
	}
}

Just replace the Sensor Port to whichever one your pot is in and max and min value to your predefined values and it should work. hasn’t been tested but if my brain works then it should work.

I guess that this is being written in EasyC, yes? Regardless, I think you should take a closer look at some of your variables.

Specifically:


{if button_up_2==1//this one
{while pot<600 
motor 5, -30
}
motor 5, 0
}

{if button_down_1==1//and this one
{while pot>400
motor 5, 30
}
motor 5, 0
}
}

Shouldn’t those variables be:


button_8_up
button_8_down

which are previously defined in your code? I don’t see those other two variables defined, so maybe this is what is causing your problem.

ok, so i added the two variables min value and max value and used the online window for my numbers. I made two if statements if pot < max value, motor -30 and if pot > min value motor 30. I also added a if pot==max and min then motor o.

the motors are still not stopping and i can only use the button once. How can i get it to stop and where I can use the button again and again?

sorry, when the lever is in neutral, the bottom 8 button doesn’t work, which is a sign of improvement. But if the lever is down and I push the 8 button, the motor will go past the min value and not stop.

ok, i got it. I got rid of the while pot line and just used the if statement with a double &&. I don’t know what double && means but ok (comparing two conditions?). Now, the lever is not always on its mark so I am guessing i would need some kind of kp for error. I dont know how to do that, but i’ll look it up and try it. For now, I got some standoffs blocking it from over rotating.

thanks Nehalem.

Sorry I didn’t reply sooner.

The next step I was thinking about was removing the while loops since those seemed pretty iffy to me. I didn’t remember if EasyC had the “and” operator either so I wasn’t sure on that. Anyways, good job on fixing your problem.

As for the kp, there are number of threads on PID that should help you. I’d explain, but I have some stuff to take care of.:stuck_out_tongue:

Thanks man. Have a merry x-mas.

&& means that the two conditions need to be true in order to enter the loop
Example: If left_motor>= 500 && right_mofor>= 500
//This means that the robot will ONLY enter the loop if the two set conditions are met
& means that at least one of the conditions have to be true to enter the loop
Example: If left_motor>= 500 & right_motor>= 500
//This means that just one of the conditions needs to be true to enter this loop