RobotC Controller Help

Is there a function in RobotC where I can press a button on my controller once and have the robot do an action instead of having to hold down the button on the controller?
For Example: I want to press a controller button once and have my lift go to a certain angle using a potentiometer, then have my lift go to a different angle using a different button on my controller (I do not want to hold down each button. I simply want to press the button once and have an action follow it)

There is not a function, but you can write a few lines of code to do what you want. You’ve written the problem out clearly, now think of the code to do each step.

Post what you write and we’ll help debug it. See the RobotC help on how to read the buttons, and how to use the pots. Good luck

@Foster I was able to program the action stated above…

void pressButton()
{
SensorValue[pot] = 0; //Clears potentiometer

if(vexRT[Btn5D] == 1) //If button is pressed
{
	while(SensorValue[pot] < 60) //While potentiometer is less than 60
		{
			motor[motor2] = 127; //Motor spins
		}
		motor[motor2] = 0; //When potentiometer reaches 60, motor stops
}

}

task usercontrol()
{
while (1==1)
{
pressButton(); //Function Called
}
}

I tested the program and it worked, thankfully. Please let me know if you have any suggestions to improve the program. Thanks!

With a function, it’s basically copying and pasting information within the brackets you’ve stated above into lines you wish the code to be. If you have a while loop, this means that no other part of the code in the task that calls the function will run until the while statement is considered false. AKA if you have your drivetrain be in the same task as this function, the motor power will not change according to the joystick until that while loop is deemed false, which may result in a massive pain while driving.
Let me see what I can do, but I will not garuntee that it will work the first time


task usercontrol()
{
	int buttonpressed = false; //Will be used to only fire a statement once
	int movetoposition = false; //Will be used to determine when to move to the designated position
  while (1 == 1)
  {
  	//Do an action once when button 5D is pressed 
			if(vexRT[Btn5D] == 1 && buttonpressed == false){
				buttonpressed = true;
				movetoposition = true;
			}
			else if(vexRT[Btn5D] == 0){
				buttonpressed = false;
			}
			
		//Move the motor until the potentiometer reaches 60
			if(movetoposition == true){
				if(SensorValue[pot]<60){
					motor[motor2] = 127;
				}
				else{
					motor[motor2] = 0;
					movetoposition = false;
				}
			}
		
	}
}

@dsibal Good Job, small, simple and easy to understand.

Just a reminder if you spin the pot 360 degrees it breaks the inside stops and no longer works.

@[TVA]Connor – He stuck it inside a task loop, so the RobotC master Loop will split time between his arm moving and the drive base. For stuff like this, I’ve never seen any kind of stutter with something this simple.