VexCode Help / Motor Turning

How can I get a motor to spin with one input that’s a number (V5/C++)

I’m not sure if you have provided enough information but simply giving you what you have asked for…

//Define the motor plugged into port #1
vex::motor Motor1 = vex::motor(vex::PORT1);

//Declare a variable to hold your input number. (I set it to 50%)
double speedPct = 50;  

//Have the motor spin based on the input speed
Motor1.spin(vex::directionType::fwd, speedPct, vex::velocityUnits::pct );
//   Variable is used here           ^^^^^^^^

This is a general C++ question rather than a VexCode specific one, so I’ll pitch in an answer.

As I understand it, you want to be able to write something like motorX.spin(20); instead of motorX.spin(fwd,20,pct);, correct?

Probably the neatest solution would be to extend the vex::motor class and provide yourself an override of spin that only takes the arguments you’re looking for, and calls the parent class’ spin method with default arguments supplied.

An excerpt from this project where I did something similar, but to a different end:

class velocityRecordedMotor : public vex::motor {
    //quite a lot of other stuff
    void spin (vex::directionType dir, double velocity, vex::velocityUnits units) {
        //other stuff...
        } else if (myState == none) {
    //quite a bit more other stuff

In my example, my goal was to make a spin method that took the same arguments as the default one, but you could quite easily remove some arguments from your reimplementation of spin, then hard code the direction and units in your call to vex::motor::spin (unlike my example here, which just passes them through and does other things with them beforehand).

The other, far less fancy way of doing it would be to just make a plain function that either had motors hardcoded in and took a velocity or took a motor and a velocity as an argument:

void notFancyOne (double vel) {
    motorX.spin(DIR, vel, UNITS); //hardcoded motor, direction, and units
void notFancyTwo (vex::motor &m, double vel) {
    m.spin(DIR, vel, UNITS); //hardcoded direction and units

I would avoid both of these ways, tbqh. They both become far less clear about what they’re doing.

Now if you were looking to shorten the spin calls because you’re controlling more than one motor at a time, you probably want to start looking at doing what Robot Mesh Studio and VexCode do with their vex::drivetrain class, or what PROS does with its OKAPI library. That is, creating new classes dedicated to managing multiple motors. At which point you can make the arguments for the methods of such a class into whatever you want from the ground up, and you don’t have to type in multiple spin commands every time (and risk forgetting one, etc.).