Help with Programing Vex V5 Pro

Hello My name is Evan Gonzalez and I am new to programing, so far I have not run into trouble when programing the controls but I have come across an issue that has halted my teams robot for the game tipping point. I have already programed moving controls and and a conveyor belt control but when we use the program we can not simultaneously use the different controls can you please help me.

void usercontrol(void) { 
  // User control code here, inside the loop
  while (1){
    
  //This Block of code is for Moving Forward.
  if(Controller1.ButtonUp.pressing()){
  FowardReverseMotor.spin(reverse,100, pct);
  LeftDriveMotor.spin(forward, 100, pct);
  RightDriveMotor.spin(forward, 100, pct);}
     
  //This Block of code is for Turning Left.  
  else if(Controller1.ButtonLeft.pressing()){
  RightDriveMotor.spin(forward, 100, pct);
  LeftDriveMotor.spin(reverse, 100, pct);}

  //This Block of code is for Turning Right.
  else if(Controller1.ButtonRight.pressing()){
  RightDriveMotor.spin(reverse, 100,pct);
  LeftDriveMotor.spin(forward, 100, pct);}

  //This Block of code is for Moving Backwards
  else if(Controller1.ButtonDown.pressing()){
  FowardReverseMotor.spin(forward, 100, pct);
  LeftDriveMotor.spin(reverse, 100, pct);
  RightDriveMotor.spin(reverse, 100, pct);}

  //This Block of code is for Moving the Conveyorbelt Up.
  else if(Controller1.ButtonA.pressing())
  ConveyorbeltMotor.spin(forward, 100, pct);

  //This Block of code is for Moving the Conveyorbelt Down.
  else if(Controller1.ButtonB.pressing())
  ConveyorbeltMotor.spin(reverse, 100, pct);


  //This Block of code is to let the Motors to disengage when the are not being used for free Movement. 
  else {LeftDriveMotor.stop(coast);
  RightDriveMotor.stop(coast);
  FowardReverseMotor.stop(coast);
  ConveyorbeltMotor.stop(coast);}

    wait(5, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

edit: code tags added by mods

I believe the issue is that all of your controls are in 1 if else statement. Try making it so there is a separate if statement for each button and or joystick.

3 Likes

one general tip is that if you do not need complex coding needs, then configuring controls in the controller config screen is a great option to save on lines of codes. that should also fix your issue

@Poetic Can you give me an example because I’m not really sure how to do that please and thank you.

Why does this start with else? Do you only want to move the convayer belt if a difrent button is not pushed?

I am on a phone right now so no but I will asap. It isn’t to hard instead of using else if just use if and give each statement its own set of brackets.

Also, why do you not use the controller joysticks for driving?

@RoboKnight Since Im not driving the bot for my team the person that is felt more comfortable with buttons than the joysticks.

You can not adjust the drive speed with your current code. Why anyone would like to do so?

@RoboKnight The thing is I want to be able to push the drive button while also being able to use the conveyor simultaneously so kind of like multitasking

That’s exactly his point. Suppose you have code like this:

if (ButtonUp.pressing()) {
  Drive.spin(forward, 100, pct);
}

else if (ButtonA.pressing()) {
  Conveyor.spin(forward, 100, pct);
}

The computer tackles it line by line. First, it says, “Is ButtonUp pressing?” And if ButtonUp is pressing, the computer will execute the code inside the the braces and drive forward. Then it will get to the next line and say, “Oh, the "if" is already true, so I don't need to do the "else if",” because the else only runs if the the if and all the else ifs before it weren’t true.

I drew a flow chart of some of your program to illustrate how the computer sees it.

If you still don’t understand, I would recommend reading this chapter. While it’s specifically about Python, not C++, the same principle applies, and I think it will help you understand if/else statements.

Another thing you might want to consider is a button to stop your conveyor, so instead of having to hold the button, and if you stop pushing it, the conveyor stops, you have to push a button to stop the conveyor. That way your driver won’t have to hold the button the entire time.

All you have to do is have

if (Button.pressed()) {
  Conveyor.stop();
}

And then of course take it out of your else stop all motors.

1 Like
void usercontrol(void) { 
  // User control code here, inside the loop
  while (1){
    
  LeftDriveMotor.spin(forward, Controller1.Axis3.position(), pct);
  RightDriveMotor.spin(forward, Controller1.Axis2.position(), pct);

  //This Block of code is for Moving the Conveyorbelt Up.
  if(Controller1.ButtonA.pressing())
  ConveyorbeltMotor.spin(forward, 100, pct);

  //This Block of code is for Moving the Conveyorbelt Down.
 else if(Controller1.ButtonB.pressing())
  ConveyorbeltMotor.spin(reverse, 100, pct);
  else  ConveyorbeltMotor.stop( );

    wait(5, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}
1 Like

This is almost correct.

and then there is the fact that you want the button movements, instead of the joysticks. This would be cone by this code:

  if(Controller1.ButtonUp.pressing()){
  FowardReverseMotor.spin(reverse,100, pct);
  LeftDriveMotor.spin(forward, 100, pct);
  RightDriveMotor.spin(forward, 100, pct);}
     
  //This Block of code is for Turning Left.  
  else if(Controller1.ButtonLeft.pressing()){
  RightDriveMotor.spin(forward, 100, pct);
  LeftDriveMotor.spin(reverse, 100, pct);}

  //This Block of code is for Turning Right.
  else if(Controller1.ButtonRight.pressing()){
  RightDriveMotor.spin(reverse, 100,pct);
  LeftDriveMotor.spin(forward, 100, pct);}

  //This Block of code is for Moving Backwards
  else if(Controller1.ButtonDown.pressing()){
  FowardReverseMotor.spin(forward, 100, pct);
  LeftDriveMotor.spin(reverse, 100, pct);
  RightDriveMotor.spin(reverse, 100, pct);}

//This block is for stoping
else {
LeftDriveMotor.stop(coast);
RightDriveMotor.stop(coast);
FowardReverseMotor.stop(coast);}

This would make your drive function work by:

  • Only letting your drive go one direction at a time
  • Letting motors not on your drive work while the drive is working

The full code for this is:

void usercontrol(void) { 
  // User control code here, inside the loop
  while (true){

//drive code
if(Controller1.ButtonUp.pressing()){
  FowardReverseMotor.spin(reverse,100, pct);
  LeftDriveMotor.spin(forward, 100, pct);
  RightDriveMotor.spin(forward, 100, pct);}
     
  //This Block of code is for Turning Left.  
  else if(Controller1.ButtonLeft.pressing()){
  RightDriveMotor.spin(forward, 100, pct);
  LeftDriveMotor.spin(reverse, 100, pct);}

  //This Block of code is for Turning Right.
  else if(Controller1.ButtonRight.pressing()){
  RightDriveMotor.spin(reverse, 100,pct);
  LeftDriveMotor.spin(forward, 100, pct);}

  //This Block of code is for Moving Backwards
  else if(Controller1.ButtonDown.pressing()){
  FowardReverseMotor.spin(forward, 100, pct);
  LeftDriveMotor.spin(reverse, 100, pct);
  RightDriveMotor.spin(reverse, 100, pct);}

//This block is for stoping
else {
LeftDriveMotor.stop(coast);
RightDriveMotor.stop(coast);
FowardReverseMotor.stop(coast);}

//intake code
 if(Controller1.ButtonA.pressing())
  ConveyorbeltMotor.spin(forward, 100, pct);

  //This Block of code is for Moving the Conveyor belt Down.

 else if(Controller1.ButtonB.pressing())
  ConveyorbeltMotor.spin(reverse, 100, pct);
 
 else  ConveyorbeltMotor.stop( );

If you have any questions about this, please ask me

Now, I add this sentence at the bottom of the code, where I know there is a good chance that it is ignored.: Please do not just copy-paste the code, sit down, and think about what each piece of the code is doing. Also, please draw a flow map, similar to this one,

where you analyze the code as if you were a robot. After you do that, feal free to use the code

4 Likes

@RoboKnight thank you for your help but Its clear to me still need to learn more programing because I kind of understand what I did wrong If I have anymore questions is it okay to message you for more information.

Yes, you can message me, or just ask them in this thread

Of course you can ask questions! That’s what the forum is for!

1 Like

you could also use callbacks. These are quite useful. U type Controller1.ButtonL1.pressed(()[] {// entercodehere});
Repeat for other buttons
can run stuff simultaneously

So, purely out of curiosity, is there are particular reason you prefer the directional buttons over the joysticks?

@EH-Coder Yes earlier in the chat I had said that the driver for my team preference was the buttons over the joystick so that’s why I had programed it like this

I must have missed that, my apologies. Do you know why he prefers it? (I wanna see if this is something my driver should try)