# How can I simplify this code?

``````task usercontrol()
{
while(1 == 1)
{
//Claw Control
if(vexRT[Btn6U] == 1)
{
motor[clawMotor] = 127;
}
else if(vexRT[Btn6D] == 1)
{
motor[clawMotor] = -127;
}
else
{
motor[clawMotor] = 0;
}

//Arm Control
if(vexRT[Btn5U] == 1)
{
motor[armMotor] = 127;
}
else if(vexRT[Btn5D] == 1)
{
motor[armMotor] = -127;
}
else
{
motor[armMotor] = 0;
}

// Score Control left
if(vexRT[Btn7U] == 1)
{
motor[leftscoreMotor] = 127;
}
else if(vexRT[Btn7D] == 1)
{
motor[leftscoreMotor] = -127;
}
else
{
motor[leftscoreMotor] = 0;
}

//score Control right
if(vexRT[Btn7U] == 1)
{
motor[rightscoreMotor] = 127;
}
else if(vexRT[Btn7D] == 1)
{
motor[rightscoreMotor] = -127;
}
else
{
motor[rightscoreMotor] = 0;
}
{
//Drive left and right
motor[leftMotor] = vexRT[Ch2];
motor[rightMotor] = vexRT[Ch3];

}

}
}
``````

This is pretty much as simple as it gets for basic code for a robot. One thing you could do it combine the score right and score left if/else statements
Ex:

``````// Score Control left
if(vexRT[Btn7U] == 1)
{
motor[leftscoreMotor] = 127;
motor[rightscoreMotor] = 127;
}
else if(vexRT[Btn7D] == 1)
{
motor[leftscoreMotor] = -127;
motor[rightscoreMotor] = -127;
}
else
{
motor[leftscoreMotor] = 0;
motor[rightscoreMotor] = 0;
}

``````

if you want to get really nit-picky, you could get rid of the brackets around

``````			//Drive left and right
motor[leftMotor] = vexRT[Ch2];
motor[rightMotor] = vexRT[Ch3];
``````

because they don’t really go anything.
Also you can just say while(true) instead of while (1 == 1)

Assuming you mean braces and not brackets, it is good practice to keep them, as most languages (except for Python and a couple others) require them, and they help to keep the code organized.
EDIT Thought you were talking about the lift code, nvm

Okay thanks,

Correct me if I’m wrong… but I believe you could simplify like below:

``````
{
while(1)
{
//Claw Control
if(vexRT[Btn6U])
{
motor[clawMotor] = 127;
}
else if(vexRT[Btn6D])
{
motor[clawMotor] = -127;
}
else
{
motor[clawMotor] = 0;
}

//Arm Control
if(vexRT[Btn5U])
{
motor[armMotor] = 127;
}
else if(vexRT[Btn5D])
{
motor[armMotor] = -127;
}
else
{
motor[armMotor] = 0;
}

// Score Control
if(vexRT[Btn7U])
{
motor[rightscoreMotor] = motor[leftscoreMotor] = 127;
}
else if(vexRT[Btn7D])
{
motor[rightscoreMotor] = motor[leftscoreMotor] = -127;
}
else
{
motor[rightscoreMotor] = motor[leftscoreMotor] = 0;
}

//Drive left and right
motor[leftMotor] = vexRT[Ch2];
motor[rightMotor] = vexRT[Ch3];

}
}

``````

My preference would be to create a reusable function like this:

``````// Reusable function that takes two buttons as inputs and returns the new speed
int SetMySpeed(bool bUpPressed,bool bDownPressed)   // returns speed
{
if(bUpPressed)  // up button is being pressed
{
return 127;
}
if(bDownPressed) // down button is being pressed
{
return -127;
}
return 0;   // nothing bring pressed speed is zero
}
``````

Then use the new function in usercontrol like this:

``````task usercontrol()
{
while(1)
{
// set speed for claw
motor[clawMotor] = SetMySpeed(vexRT[Btn6U], vexRT[Btn6D]);

// set speed for Arm
motor[armMotor] = SetMySpeed(vexRT[Btn5U], vexRT[Btn5D]);

//set score control
motor[leftscoreMotor] = SetMySpeed(vexRT[Btn7U], vexRT[Btn7D]);
motor[rightscoreMotor] = SetMySpeed(vexRT[Btn7U], vexRT[Btn7D]);

//Drive left and right
motor[leftMotor] = vexRT[Ch2];
motor[rightMotor] = vexRT[Ch3];

// don't forget this wait at the bottom of the loop
wait1Msec(20);
}
}

``````

Actually, if all you do is an arm control, use the armControl function (enable Natural Language in Robot->Platform Type)

``````
{
while(1 == 1)
{
//Claw Control
armControl(clawMotor, Btn6U, Btn6D, 127);
//Arm Control
armControl(armMotor, Btn5U, Btn5D, 127);
// Score Control, both left and right
armControl(leftscoreMotor, Btn7U, Btn7D, 127);
armControl(rightscoreMotor, Btn7U, Btn7D, 127);
//Drive left and right, as long as you have proper drivebase config in motor setup
tankControl(Ch2, Ch3);
}
}

``````

BTW: “Natural Language” is pretty much just a library of functions, you can inspect them using right click->Go to Definition/Declaration. You can always write functions like this better taylored for your uses.

You could get it down to 1 line of code using ternaries, but I think what was posted above is simpler (As in less complicated)

If you want really super simple code, you could do something like:

motor[clawMotor] = VexRt[Btn6U]*127 - VexRt[Btn6D]*127;

motor[armMotor] = VexRt[Btn5U]*127 - VexRt[Btn5D]*127;

motor[rightscoreMotor] = motor[leftscoreMotor] = VexRt[Btn7U]*127 - VexRt[Btn7D]*127