Coding an axis into an If/Else Statement

Hey there everyone, I’m trying to re-code my drive code from a while loop, into an If/Else Statement. The reason why I’m doing this is so the motors can break upon releasing the joystick. But that can only be done in an If/Else Statment.

Bellow is my current Code:
vex::motor_group left(Front_Left, Back_Left);
vex::motor_group right(Front_Right, Back_Right);
vex::drivetrain dt(left, right, 335, 380, 330, mm);

while (1) {
left.spin(directionType::fwd, Controller1.Axis3.value(), velocityUnits::pct);
right.spin(directionType::fwd, Controller1.Axis2.value(), velocityUnits::pct);

It works wonderfully, but again it’s a while loop not an If/Else Statement

This is what I currently have, but it doesn’t work:
vex::motor_group left(Front_Left, Back_Left);
vex::motor_group right(Front_Right, Back_Right);
vex::drivetrain dt(left, right, 335, 380, 330, mm);

int ChassisSpeedRPM = 200;
if(Controller1.Axis2.()) {
left.spin(directionType::fwd, ChassisSpeedRPM, velocityUnits::pct);
} else if(Controller1.Axis2.()) {
right.spin(directionType::rev, ChassisSpeedRPM, velocityUnits::pct);
} else {
drivetrain.stop(brakeType::break);
}

If anyone knows how to code drivecode in an If/Else statement please let me know. I’m really struggling. Thank you in advance for any support those may be able to give. Thanks for reading and have a great day.

First of all, you will want to put your if statement inside of a while loop. This way, the motor values will continually update.

I’m not 100 percent sure what you’re asking, but here is a basic outline of the code that I think you’re looking for.

while(1)
{


if(right joystick is not at 0)
{
spin right side of drive according to the joystick level
}
else{
brake the right side of the drivetrain
}


if(left joystick is not at 0)
{
spin left side of drive according to the joystick level
}
else{
brake the left side of the drivetrain
}


}

Hope this helps!

P.S. you can format your code so it’s easier to read by using 3 backticks (`) Note that is the key to the left of the 1 on most keyboards, not the key to the right of the m. Like this, but without the parentheses:

(```)
Code here
(```)
3 Likes

Nearly everything in this statement is wrong. When a motor’s velocity is 0, whatever the brakeType that was set for the motor will apply.

You should only need to set the brakeType once, outside of the while loop, and echo-ing @Robo_Chicken’s psuedocode:

dt.setBrakeType(brake);
while(1)
{
rightsidespeed = right joystick
if(abs(rightsidespeed ) < someDeadBandValue) {
rightsidespeed  = 0
} 

leftsidespeed = leftjoystick
if(abs(leftsidespeed ) < someDeadBandValue) {
leftsidespeed  = 0
} 

set right side of drive to rightsidespeed
set left side of drive to leftsidespeed

}
6 Likes

I agree there are too much errors and I would suggest you read some basic on programming before you write your own code.

I’m not the coder for my team. LMAO.