Is this code good?

Is this code good? I am new to coding in vex.

using namespace vex;
vex::motor Frontleftmotor (vex::PORT1, vex::gearSetting::ratio18_1, true);
vex::motor Frontrightmotor (vex::PORT2, vex::gearSetting::ratio18_1, true);
vex::motor Backleftmotor  (vex::PORT3, vex::gearSetting::ratio18_1, false);
vex::motor Backrightmotor (vex::PORT4, vex::gearSetting::ratio18_1, true);
vex::motor Arm1 (vex::PORT5, vex::gearSetting::ratio18_1, true);
vex::motor Arm2 (vex::PORT6, vex::gearSetting::ratio18_1, true);
vex::motor Claw1 (vex::PORT7, vex::gearSetting::ratio18_1, true);
vex::motor Claw2 (vex::PORT8, vex::gearSetting::ratio18_1, true);
vex::motor Capflipper (vex::PORT9, vex::gearSetting::ratio18_1, true);
#include "robot-config.h"

int main() {
    
    while(true) {
        Backleftmotor.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis3+Axis4)/2
        Backrightmotor.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct);//(Axis1-Axis2)/2
        Frontleftmotor.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis3+Axis4)/2
        Frontrightmotor.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct); //(Axis1+Axis2)/2
        
        vex::task::sleep(20); 
        
        
    
    
    if(Controller1.ButtonX.pressing())
{
Arm1.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
} 
    
    
    else if (Controller1.ButtonB.pressing())
    {
        Arm1.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
        vex::task::sleep(20);
        
    }
        
       if(Controller1.ButtonX.pressing())
       {
           Arm2.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
           vex::task::sleep(20);
       }
        else if (Controller1.ButtonB.pressing())
        {
            Arm2.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
            vex::task::sleep(20);
        }
        if(Controller1.ButtonR1.pressing())
        {
            Claw1.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
            
        }
        else if (Controller1.ButtonR2.pressing())
        {
            Claw1.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
            
        }
        if(Controller1.ButtonR1.pressing())
        {
            Claw2.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
            
        }
        else if(Controller1.ButtonR2.pressing())
        {
            Claw2.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
            
        }
        if(Controller1.ButtonL1.pressing())
        {
            Capflipper.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
            
        }
        else if(Controller1.ButtonL2.pressing())
        {
            Capflipper.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
        }
        vex::task::sleep(20);
    }
}

If you liked this post, check out these

https://vexforum.com/t/is-this-good-code/49992/1
https://vexforum.com/t/view-all-vex-products-at-once/18851/1
https://vexforum.com/t/several-zero-scores-during-division-eliminations/18852/1
https://vexforum.com/t/plz-test-my-code/50267/1
https://vexforum.com/t/teaching-math-trough-robots/18924/1

On Robot Mesh Studio we have the option to create a Mimic, a digital robot mockup complete with emulated LCD output and emulated joystick input. You might try creating one of those to test basic code functionality if you are away from your robot a lot.

That being said, I can spot about twelve things wrong with this code, though a lot of them are the same mistake made in multiple places.

1: Inconsistent motor directions
2-5: extraneous sleep commands
6-7: checking buttonX and buttonB twice each
8-12: no way to stop any non-drive motor

Thankyou!!!

How do you stop a non drive motor

And how do you fix in consistent motor directions?


yourMotorNameHere.stop()

is the way. Optionally you can also include a


brakeType

as an argument to


stop

.

Change the motor constructors. At the moment, you have all but


Backleftmotor

reversed. Your motor constructors are these, with the reverse flag bolded in each:

Ok but how would you say if this button is not being pressed stop the motor?

I think it is something like thhis.

if(ButtonX.pressing.stop)

How would you say if that a specific motor is not being pressed then stop it?

if(!Controller1.ButtonWhatever.pressing())
{
MotorNameHere.stop();
}

Notice the exclamation point (!) means not, so if it is NOT being pressed, it tells the motor to stop.

ok thanks

Maybe try using some functions


void setDrive(int left, int right) {
  BaseMotor1.setVelocity(left); // or right
  ...
  BaseMotorn.setVelocity(right); // or left
}

void setClaw(int speed) {
  ClawMotor.setVelocity(speed);
}

int buttonToPower(vex::controller::button upButton, vex::controller::button downButton, int speed) { // Correct me if I'm wrong, I'm using pros so I don't know how VCS works
  return upButton.isPressed() ? speed : downButton.isPressed() ? -speed : 0; // an if statement would probably be better here but yolo
}

while (true) {
  setDrive(...);
  setClaw(buttonToPower(...));
}

Use the functions in header files and you can reuse them in autonomous and other projects.

All you have to do is add one last


else

case after each


if...else if

that you already have. You check one button, then the other, so if both fail then you should stop the motor.

Ok. With this code for some reason the front right motor and the backright motor are moving in opposite directions.

using namespace vex;
vex::motor Frontleftmotor (vex::PORT1, vex::gearSetting::ratio18_1, true);
vex::motor Frontrightmotor (vex::PORT2, vex::gearSetting::ratio18_1, true);
vex::motor Backleftmotor (vex::PORT3, vex::gearSetting::ratio18_1, true);
vex::motor Backrightmotor (vex::PORT4, vex::gearSetting::ratio18_1, true);
vex::motor Arm1 (vex::PORT5, vex::gearSetting::ratio18_1, true);
vex::motor Arm2 (vex::PORT6, vex::gearSetting::ratio18_1, true);
vex::motor Claw1 (vex::PORT7, vex::gearSetting::ratio18_1, true);
vex::motor Claw2 (vex::PORT8, vex::gearSetting::ratio18_1, true);
vex::motor Capflipper (vex::PORT9, vex::gearSetting::ratio18_1, true);
#include “robot-config.h”

int main() {

while(true) {
    Backleftmotor.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis3+Axis4)/2
    Backrightmotor.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct);//(Axis1-Axis2)/2
    Frontleftmotor.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis2+Axis1)/2
    Frontrightmotor.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct); //(Axis4+Axis3)/2
    
    vex::task::sleep(20); 
    
    


if(Controller1.ButtonX.pressing())

{
Arm1.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
if(!Controller1.ButtonX.pressing())
{
Arm1.stop();
}

else if (Controller1.ButtonB.pressing())
{
    Arm1.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
    
}
  if(!Controller1.ButtonB.pressing())

{
Arm1.stop();
}
if(Controller1.ButtonX.pressing())
{
Arm2.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
vex::task::sleep(20);
}
if(!Controller1.ButtonX.pressing())
{
Arm2.stop();
}
else if (Controller1.ButtonB.pressing())
{
Arm2.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
vex::task::sleep(20);
}
if(!Controller1.ButtonB.pressing())
{
Arm2.stop();
}
if(Controller1.ButtonR1.pressing())
{
Claw1.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);

    }
    if(!Controller1.ButtonR1.pressing())

{
Claw1.stop();
}
else if (Controller1.ButtonR2.pressing())
{
Claw1.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);

    }
    if(!Controller1.ButtonR2.pressing())

{
Claw1.stop();
}
if(Controller1.ButtonR1.pressing())
{
Claw2.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);

    }
    if(!Controller1.ButtonR1.pressing())

{
Claw2.stop();
}
else if(Controller1.ButtonR2.pressing())
{
Claw2.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);

    }
    if(!Controller1.ButtonR2.pressing())

{
Claw2.stop();
}
if(Controller1.ButtonL1.pressing())
{
Capflipper.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);

    }
    if(!Controller1.ButtonL1.pressing())

{
Capflipper.stop();
}
else if(Controller1.ButtonL2.pressing())
{
Capflipper.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
}
if(!Controller1.ButtonL2.pressing())
{
Capflipper.stop();
}

}

}

NM I got it.