Button pressed, switch to tank drive won't work

So recently I decided to make a slow mode and a control switch button on my controller but when I press the button nothing happens, also, I want it to run once on press not like 8 times because it is pressing. I do get test on the robot when the button is pressed but the rest won’t work. Let me know if you can help!

else if(Controller1.ButtonRight.pressing()){
    Brain.Screen.print("Test");
    if(Controller1RightArrowDriveSwitchArcade == true){
            drivetrainLeftSideSpeed = Controller1.Axis3.position();
    drivetrainRightSideSpeed = Controller1.Axis2.position();
    Controller1RightArrowDriveSwitchTank = true;
    Controller1RightArrowDriveSwitchArcade= false;
    }
      else if(Controller1RightArrowDriveSwitchArcade == false){
        drivetrainLeftSideSpeed = Controller1.Axis3.position() + Controller1.Axis1.position();
        drivetrainRightSideSpeed = Controller1.Axis3.position() - Controller1.Axis1.position();
    Controller1RightArrowDriveSwitchTank = false;
    Controller1RightArrowDriveSwitchArcade= true;
      }
  }



    if(Controller1.ButtonY.pressing()){
    
      if(slowMode == false){
      speedLeft = drivetrainLeftSideSpeed - 50;
      speedRight = drivetrainRightSideSpeed - 50;
      slowMode = true;
    }
    else if(slowMode == true){
      speedLeft = drivetrainLeftSideSpeed;
      speedRight = drivetrainRightSideSpeed;
      slowMode = false;
    }
    
    }

Also another thing you should know, speedLeft and speedRight are bellow

//Arcade Drive
  int drivetrainLeftSideSpeed = Controller1.Axis3.position() + Controller1.Axis1.position();
  int drivetrainRightSideSpeed = Controller1.Axis3.position() - Controller1.Axis1.position();
  int speedLeft = drivetrainLeftSideSpeed;
  int speedRight = drivetrainRightSideSpeed;

The first button press should be in a if statement and the second button press should all so be a if statement
If (button pressing()) {
(Drive for not tank)
}

If(button pressing()) {
(Tank drive)
}
Also why does the first line say
Else if {
Please share the whole code for reference

1 Like

Here’s a part in my code that uses a state machine one that works for one button


Edit: Always use button pressing
Double edit: the most important part is the
!
In the code. It makes the code after it the opposite

1 Like

I think, what you want is this:

bool bDriveModeTank = false;
bool bRightButtonWasPressing = false;

while(1) // your main driver control loop
{
  bool bSlowDriveMode = Controller1.ButtonY.pressing();

  if( Controller1.ButtonRight.pressing() && 
      !bRightButtonWasPressing ) // you only want to catch it once
  {
     bDriveModeTank = ! bDriveModeTank; // flip the drive mode
  }
  // remember for the next iteration
  bRightButtonWasPressing = Controller1.ButtonRight.pressing();

  if( bDriveModeTank == true )
  {
      drivetrainLeftSideSpeed = Controller1.Axis3.position();
      drivetrainRightSideSpeed = Controller1.Axis2.position();
  } 
  else // Arcade
  {
      drivetrainLeftSideSpeed = Controller1.Axis3.position() + Controller1.Axis1.position();
      drivetrainRightSideSpeed = Controller1.Axis3.position() - Controller1.Axis1.position();
  }

  if(bSlowDriveMode == true)
  {
      speedLeft = drivetrainLeftSideSpeed / 2;
      speedRight = drivetrainRightSideSpeed / 2;
  }

//  send speed control to the motors
  sleep(10);
}

3 Likes

Now will this switch back to arcade if I press it again? Also does the slow mode toggle like the drive switch? Sorry, I am new to VEXCode but not to c++ so this is different for me.

I would believe so
20c

Ok well I will test it then see if it is the solution, should be ready around 2:30 as that is when I have robotics

1 Like

It did not work, here is my whole code

#include "vex.h"

 using namespace vex;
 using signature = vision::signature;
  using code = vision::code;

  // A global instance of brain used for printing to the V5 Brain screen
 brain  Brain;

 // VEXcode device constructors
 controller Controller1 = controller(primary);
 motor leftMotorA = motor(PORT1, ratio18_1, false);
 motor leftMotorB = motor(PORT10, ratio18_1, false);
 motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
 motor rightMotorA = motor(PORT11, ratio18_1, true);
 motor rightMotorB = motor(PORT20, ratio18_1, true);
 motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 319.19, 295, 40, mm, 1);
motor Motor19 = motor(PORT19, ratio18_1, false);
motor Motor5 = motor(PORT5, ratio18_1, true);
motor Motor6 = motor(PORT6, ratio36_1, false); //Copy replace 6 with 7
motor Motor7 = motor(PORT7, ratio36_1, true);
// VEXcode generated functions
// define variable for remote controller enable/disable
  bool RemoteControlCodeEnabled = true;
  // define variables used for controlling motors based on controller inputs
 bool Controller1RightShoulderControlMotorsStopped = true;
  bool Controller1LeftShoulderControlMotorsStopped = true;
 bool Controller1RightArrowDriveSwitchTank = false;
 bool Controller1RightArrowDriveSwitchArcade= true;
 bool DrivetrainLNeedsToBeStopped_Controller1 = true;
 bool DrivetrainRNeedsToBeStopped_Controller1 = true;
 bool slowMode = false;

  // define a task that will handle monitoring inputs from Controller1
 int rc_auto_loop_function_Controller1() {
  // process the controller input every 20 milliseconds
 // update the motors based on the input values
 while(true) {
       if(RemoteControlCodeEnabled) {
        // calculate the drivetrain motor velocities from the controller joystick axies
      // left = Axis3 + Axis1
      // right = Axis3 - Axis1
  //Arcade Drive
    int drivetrainLeftSideSpeed = Controller1.Axis3.position() + Controller1.Axis1.position();
    int drivetrainRightSideSpeed = Controller1.Axis3.position() - Controller1.Axis1.position();
 int speedLeft = drivetrainLeftSideSpeed;
 int speedRight = drivetrainRightSideSpeed;
  // int drivetrainLeftSideSpeed = Controller1.Axis3.position();
  // int drivetrainRightSideSpeed = Controller1.Axis2.position();
  
    // check if the value is inside of the deadband range
      if (speedLeft < 5 && speedLeft > -5) {
     // check if the left motor has already been stopped
    if (DrivetrainLNeedsToBeStopped_Controller1) {
      // stop the left drive motor
      LeftDriveSmart.stop();
      // tell the code that the left motor has been stopped
      DrivetrainLNeedsToBeStopped_Controller1 = false;
    }
  } else {
    // reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
    DrivetrainLNeedsToBeStopped_Controller1 = true;
  }
  // check if the value is inside of the deadband range
  if (speedRight < 5 && speedRight > -5) {
    // check if the right motor has already been stopped
    if (DrivetrainRNeedsToBeStopped_Controller1) {
      // stop the right drive motor
      RightDriveSmart.stop();
      // tell the code that the right motor has been stopped
      DrivetrainRNeedsToBeStopped_Controller1 = false;
    }
  } else {
    // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
    DrivetrainRNeedsToBeStopped_Controller1 = true;
  }
  
  // only tell the left drive motor to spin if the values are not in the deadband range
  if (DrivetrainLNeedsToBeStopped_Controller1) {
    LeftDriveSmart.setVelocity(speedLeft, percent);
    LeftDriveSmart.spin(forward);
  }
  // only tell the right drive motor to spin if the values are not in the deadband range
  if (DrivetrainRNeedsToBeStopped_Controller1) {
    RightDriveSmart.setVelocity(speedRight, percent);
    RightDriveSmart.spin(forward);
  }
  // check the ButtonR1/ButtonR2 status to control Motor19
if (Controller1.ButtonR2.pressing()) {
    Motor19.spin(directionType::fwd, 75, velocityUnits::rpm);
     Motor5.spin(directionType::fwd, 75, velocityUnits::rpm);
    Controller1RightShoulderControlMotorsStopped = false;
  } else if (Controller1.ButtonR1.pressing()) {
    Motor19.spin(directionType::rev, 75, velocityUnits::rpm);
     Motor5.spin(directionType::rev, 75, velocityUnits::rpm);

    Controller1RightShoulderControlMotorsStopped = false;
  } else if (!Controller1RightShoulderControlMotorsStopped) {
    Motor19.stop();
    Motor5.stop();
    // set the toggle so that we don't constantly tell the motor to stop when the buttons are released
    Controller1RightShoulderControlMotorsStopped = true;
  }
 else if (Controller1.ButtonL2.pressing()) {
    printf("Test");
    Motor6.spin(directionType::fwd, 50, velocityUnits::rpm);
    Motor7.spin(directionType::fwd, 50, velocityUnits::rpm);
    Controller1LeftShoulderControlMotorsStopped = false;
  } else if (Controller1.ButtonL1.pressing()) {
    Motor6.spin(directionType::rev, 50, velocityUnits::rpm);
    Motor7.spin(directionType::rev, 50, velocityUnits::rpm);
    Controller1LeftShoulderControlMotorsStopped = false;
  } else if (!Controller1LeftShoulderControlMotorsStopped) {
    Motor6.stop();
    Motor7.stop();
    // set the toggle so that we don't constantly tell the motor to stop when the buttons are released
    Controller1LeftShoulderControlMotorsStopped = true;
  }




  //Switches
 bool bDriveModeTank = false;
 bool bRightButtonWasPressing = false;


 bool bSlowDriveMode = Controller1.ButtonY.pressing();

 if( Controller1.ButtonRight.pressing() && 
  !bRightButtonWasPressing ) // you only want to catch it once
 {
 bDriveModeTank = ! bDriveModeTank; // flip the drive mode
  }
 // remember for the next iteration
 bRightButtonWasPressing = Controller1.ButtonRight.pressing();

 if( bDriveModeTank == true )
 {
  drivetrainLeftSideSpeed = Controller1.Axis3.position();
   drivetrainRightSideSpeed = Controller1.Axis2.position();
 } 
  else // Arcade
  {
      drivetrainLeftSideSpeed = Controller1.Axis3.position() + Controller1.Axis1.position();
     drivetrainRightSideSpeed = Controller1.Axis3.position() - Controller1.Axis1.position();
   }

   if(bSlowDriveMode == true)
    {
     speedLeft = drivetrainLeftSideSpeed / 2;
     speedRight = drivetrainRightSideSpeed / 2;
  }



  //Auto Lift










}
// wait before repeating the process
wait(20, msec);
 }
  return 0;
 }

 /**
 * Used to initialize code/tasks/devices added using tools in VEXcode Pro.
 * 
 * This should be called at the start of your int main function.
 */
 void vexcodeInit( void ) {
 task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
 }

It wasn’t easy to follow your code. I am not sure if you need any of the DrivetrainLNeedsToBeStopped_Controller1 and DrivetrainRNeedsToBeStopped_Controller1 logic. I think your code could be significantly simplified and its size could be reduced.

One of the obvious problems that I se tho, is that after you set drivetrainLeftSideSpeed and drivetrainRightSideSpeedvariables, you never send them to the motors.

From what I could understand LeftDriveSmart and RightDriveSmart get their velocity much earlier from speedLeft and speedRight variables that do not account for Arcade/Tank switch logic.

2 Likes

Its ok, I completed it, I ended up using a while loop and just put duplicate code with one difference. Thank you though, I should have responded earlier.

1 Like