Button to change Drivetrain direction

Hello, forum! I am looking for a way to change the forward/backward direction with a button on the controller (aka making the controller go backwards). I found directionType, but I’m not sure if it’s for just motors or drivetrains anymore :sweat_smile: Here’s my code!

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Drivetrain           drivetrain    1, 2, 3, 4      
// Flippy               motor         5               
// Climber              motor         6               
// Controller1          controller                    
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"
#include "functions.h"

using namespace vex;

// A global instance of competition
competition Competition;

bool isBackwards = false;
bool buttonPressed = false; // no spamming hehe
bool buttonA = false;

/*---------------------------------------------------------------------------*/
/*                          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 V5 has been powered on and        */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // 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) {
  doAuton(1); // choose which auton to run, 1 or 2 (check functions.cpp for the awesome code 👍)
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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) {
  while (1) {
  buttonA = Controller1.ButtonA.pressing();
  
    if (buttonA && !buttonPressed){
      buttonPressed = true; 
      isBackwards = !isBackwards;
    }
    else if (!buttonA) buttonPressed = false;

    if (isBackwards) {
      Brain.Screen.print("!! NOW REVERSING !!");
      // reverse direction
      Drivetrain.directionType = reverse; // error
    }
    else {
      Brain.Screen.print("!! NOW FORWARDING? !!");
      // regular direction
      Drivetrain.directionType = forward; // error
    }


    wait(20, msec);
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);
  pre_auton();
  
  while (true) {
    wait(100, msec);
  }
}

If it’s some really simple answer or something, I’m sorry for wasting your time. I tried looking at other posts to see an answer but I couldn’t figure anything out. This post is the closest I could get

1 Like

I’ve done it in blocks, using a boolean. (and included a built-in antispam)
Here is a simple split arcade reversing code for a drivetrain, which needs to have some math switched to make turning work (unless you use tank control)


simple reversing drive.v5blocks (10.4 KB)

Here is a another approach. You could multiply the velocity value by a direction variable.

If direction is 1 then the robot moves forward, and if -1 then the robot moves in reverse.

The controller X button event multiplies the direction variable by -1 each time the button is pressed so it will flip from 1 to -1 and then back to 1. (-1 * -1 = 1)

4 Likes

Using the convert to text feature in VEXcode V5, this worked with some tweaking! Thanks!

1 Like