Controller not reading button R2.

   In our program we read whether buttons R1 or R2 are pressed to choose the direction of our front roller. When we press R1 the motor spins forwards but when we press R2  it doesn't spin at all. I also changed the direction of the motor in the program and it worked spinning backwards. I believe this is a controller problem is there anything we can do other than just avoid R2.

Post your code, and state what language you are using from the following list:
https://vexforum.com/index.php/conversation/post/283889

Here is the program in VEX C++, but I don’t think it’s the issue. It seems like a hardware problem

#include "robot-config.h"
          
/*---------------------------------------------------------------------------*/
/*                                                                           */
/*        Description: Competition template for VCS VEX V5                   */
/*                                                                           */
/*---------------------------------------------------------------------------*/

//Creates a competition object that allows access to Competition methods.
vex::competition    Competition;

/*---------------------------------------------------------------------------*/
/*                          Pre-Autonomous Functions                         */
/*                                                                           */
/*  You may want to perform some actions before the competition starts.      */
/*  Do them in the following function.  You must return from this function   */
/*  or the autonomous and usercontrol tasks will not be started.  This       */
/*  function is only called once after the cortex has been powered on and    */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

void pre_auton( void ) {
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
  
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              Autonomous Task                              */
/*                                                                           */
/*  This task is used to control your robot during the autonomous phase of   */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void autonomous( void ) {
  // ..........................................................................
  // Insert autonomous user code here.
  // ..........................................................................

}

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*                              User Control Task                             */
/*                                                                            */
/*  This task is used to control your robot during the user control phase of  */
/*  a VEX Competition.                                                        */
/*                                                                            */
/*  You must modify the code to add your own robot specific commands here.    */
/*----------------------------------------------------------------------------*/

void usercontrol( void ) {
  // Set motor velocities
  // 90 % to conserve battery on things that do not need much power.
  // set to port 8
  Front.setVelocity(200,velocityUnits::pct);
  // set to port 12
  VerticalL.setVelocity(25,velocityUnits::pct);
  // set to port 2
  VerticalR.setVelocity(25,velocityUnits::pct);
  while (1){
      // set port 11
      DriveL.spin(vex::directionType::fwd, Controller1.Axis3.value()/2, vex::velocityUnits::pct);
      // set port 1
      DriveR.spin(vex::directionType::fwd, Controller1.Axis2.value()/2, vex::velocityUnits::pct);
      if(Controller1.ButtonR2.pressing()) //if back right trigger is pressed then...
      {
          Front.spin(directionType::fwd);
          VerticalL.spin(directionType::rev);
          VerticalR.spin(directionType::fwd);
      }
      if(Controller1.ButtonR1.pressing()) //if front right trigger is pressed then...
      {
          Front.spin(directionType::rev);
          VerticalL.spin(directionType::rev);
          VerticalR.spin(directionType::fwd);
      }
      else //if no trigger is pressed then...
      {
          Front.stop(brakeType::brake);
          VerticalL.stop(brakeType::brake);
          VerticalR.stop(brakeType::brake);
      }
      if (Controller1.ButtonX.pressing() == true)
      {
          // set to port 8
          Flywheel.spin(directionType::fwd,200,velocityUnits::rpm);
      }
      if (Controller1.ButtonB.pressing() == true)
      {
          Flywheel.spin(directionType::fwd,100,velocityUnits::rpm);
      }
      else
      {
          Flywheel.stop(brakeType::coast);
      }
    vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources. 
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
    
    //Run the pre-autonomous function. 
    pre_auton();
    
    //Set up callbacks for autonomous and driver control periods.
    Competition.autonomous( autonomous );
    Competition.drivercontrol( usercontrol );

    //Prevent main from exiting with an infinite loop.                        
    while(1) {
      vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
    }    
       
}








Code issue. This section:

      if(Controller1.ButtonR2.pressing()) //if back right trigger is pressed then...
      {
          Front.spin(directionType::fwd);
          VerticalL.spin(directionType::rev);
          VerticalR.spin(directionType::fwd);
      }
      if(Controller1.ButtonR1.pressing()) //if front right trigger is pressed then...
      {
          Front.spin(directionType::rev);
          VerticalL.spin(directionType::rev);
          VerticalR.spin(directionType::fwd);
      }
      else //if no trigger is pressed then...
      {
          Front.stop(brakeType::brake);
          VerticalL.stop(brakeType::brake);
          VerticalR.stop(brakeType::brake);
      }

The first


if

block checks R2 and sets motors if it is pressed. The following


if...else

then overwrites those same motors no matter what the state of R2 was. Add an


else

before the second


if

and this will work.

In the future, if you want to see if it’s a problem with the button, make a minimal program that only uses that button and one output based on it (or even just having it write the state of the button to the debug console would work). This lets you isolate hardware problems from software problems.

Looks like you need an additional “else” in this code.


      if(Controller1.ButtonR2.pressing()) //if back right trigger is pressed then...
      {
          Front.spin(directionType::fwd);
          VerticalL.spin(directionType::rev);
          VerticalR.spin(directionType::fwd);
      }
      if(Controller1.ButtonR1.pressing()) //if front right trigger is pressed then...
      {
          Front.spin(directionType::rev);
          VerticalL.spin(directionType::rev);
          VerticalR.spin(directionType::fwd);
      }
      else //if no trigger is pressed then...
      {
          Front.stop(brakeType::brake);
          VerticalL.stop(brakeType::brake);
          VerticalR.stop(brakeType::brake);
      }

Edit: Ninjaed: by John.

And looking at it, you’re going to have the same problem with your X button, too.

So it was a code mistake. Thank you
I forgot to make the second if an else if.