How do i use buttons to control the motors in v5

idk the specific code to use the r1 and r2 buttons on the front of the controller to spin the motors and this is what ihave so far for my program

#include “robot-config.h”
using namespace vex;
vex::brain Bran;
vex::motor dill(vex::PORT1, vex::gearSetting::ratio18_1, false);
vex::motor biily(vex::PORT10, vex::gearSetting::ratio18_1, true);
Vex::motor bob(vex::PORT5, vex::gearSetting::ratio18_1, true);
Vex::motor joe(vex::PORT6, vex::gearSetting::ratio18_1, true);
Vex::motor rob(vex::PORT7, vex::gearSetting::ratio18_1, true);
Vex::motor tom(vex::PORT4, vex::gearSetting::ratio18_1, true);
vex::controller controller1 = vex::controller();

int main() {
while (true) {
dill.spin(vex::directionType::fwd, Controller1.Axis2.value() + Controller1.Axis4.value() - Controller1.Axis1.value(), vex::velocityUnits::pct);
bily.spin(vex::directionType::fwd, Controller1.Axis2.value() + Controller1.Axis4.value() + Controller1.Axis1.value(), vex::velocityUnits::pct);
bob.spin(vex::directionType::fwd, Controller1.Axis2.value() - Controller1.Axis4.value() + Controller1.Axis1.value(), vex::velocityUnits::pct);
joe.spin(vex::directionType::fwd, Controller1.Axis2.value() - Controller1.Axis4.value() - Controller1.Axis1.value(), vex::velocityUnits::pct)
if(Controller1.ButtonA.pressing())
{
rob.spin(directionType::fwd, , velocityUnits::pct);
}

}
}

It depends on how you want it to function.

You could run the motor while the button is being pressed
or
one button turns the motor on and one turns it off
or
clicking a button once will turn the motor on and clicking it another time turns it off.

Also your code has some uppercase ‘v’ in vex which should all be lowercase. This might just be a formatting issue. Also next time please note that vexforum as a button for code.

Here are Example of how to do this…

This will run the motor while the button is being pressed and stop when the button is released.

if(controller1.ButtonA.pressing())
{
    rob.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
else rob.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);

Here one button starts the motor and one stops the motor.

if(controller1.ButtonA.pressing())
{
    rob.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}

if(controller1.ButtonB.pressing())
{
    rob.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
}

Finally you can have a button toggle the motor meaning clicking a button once will turn the motor on and clicking it another time turns it off.

outside loop…

bool clickState = true;
bool motorState = false;

inside loop…

if(controller1.ButtonA.pressing() && clickState)
{
    motorState = !motorState;
    clickState = false;
}
if(!controller1.ButtonA.pressing()) clickState = true;

if(motorState) rob.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
if(!motorState) rob.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);

In this example clickState is to only do the action once per click, requiring an button release before allowing it again. Then motorState stores the state of the motor: ture is running and false is stoped.

5 Likes

sorry i was in a rush since imtired and i want to go to sleep… but will this work for it ( i wont have access to the computer with the software so im writing this on a google doc…
int main() {

while (true) {

dill.spin(vex::directionType::fwd, Controller1.Axis2.value() + Controller1.Axis4.value() - Controller1.Axis1.value(), vex::velocityUnits::pct);

bily.spin(vex::directionType::fwd, Controller1.Axis2.value() + Controller1.Axis4.value() + Controller1.Axis1.value(), vex::velocityUnits::pct);

bob.spin(vex::directionType::fwd, Controller1.Axis2.value() - Controller1.Axis4.value() + Controller1.Axis1.value(), vex::velocityUnits::pct);

joe.spin(vex::directionType::fwd, Controller1.Axis2.value() - Controller1.Axis4.value() - Controller1.Axis1.value(), vex::velocityUnits::pct)

if(controller1.ButtonA.pressing())
{
rob.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
else rob.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);

}
}


i’m not sure where to put it since i’m not used to c++

Yes that would work.


Also it is good practice to include…

vex::task::sleep(3);

in your while loop.

Make that vex::task::sleep(30); because the time is in milliseconds. 3 milliseconds won’t be enough time for the loop to process input and you’ll end up skipping cycles of the loop. If you really want to push it, 10 milliseconds could work.

Apparently you don’t even need include vex::task::sleep(), though I would still put it (you need to make sure to do an API method).

1 Like

So, I’ve been trying to implement the single button to start and top my flywheel into a ping pong robot I’ve built. I am not an experienced programmer, so what would I put in place of clickState and motor state? I’m struggling to figure that out.

If you are trying to make a button toggle a motor or motors, you don’t have to replace clickState and motorState. They are just variables that keep track of the state that your system/controller is in. You can change their name but it is not necessary. What you want to change/set is the motor that is being powered…

if(motorState) rob.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
if(!motorState) rob.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);

In the example, rob is the name of the motor (the name can be whatever you want). For example here is how you would control two motors at either 0% speed or 50% speed…

vex::motor motorLeft(vex::PORT3);
vex::motor motorRight(vex::PORT5);

In your loop…

if(motorState) //same as (motorState == true)
{
    motorLeft.spin(vex::directionType::fwd, 50, vex::velocityUnits::pct);
    motorRight.spin(vex::directionType::fwd, 50, vex::velocityUnits::pct);
}
if(!motorState) //same as (motorState == false)
{
    motorLeft.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
    motorRight.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
}

Hope this helps. If you have any more question feel free to ask.

2 Likes

Okay. So, I implemented it within my code and I have one or two slight issues.

One:
Where does the first statement go within my code?

This portion in particular:
if(motorState) Flywheel.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct); if(!motorState) Flywheel.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);

Two:
Something is wrong here, but I’m not sure what:

if(motorState())
{
Flywheel.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
if(!motorState())
{
Flywheel.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
}

Once again, thanks for the help, I’m pretty inexperienced when it comes to programming.

Your code is likely set up something like this…

int main()
{
    while(true) //while loop
    {
        //code here will repeatedly run

        vex::task::sleep(7);
        //also make sure to include this line in the while loop
    }
}

That portion of the code (and anything labeled as in the loop) goes inside that while loop, and will run over and over indefinitely.


The error is the brackets after motorState. Brackets like that are used for calling function which the variable motorState is not.


Also you can use…
```
code
```
to make your code look good on Vexforum.

2 Likes

Yeah. I used to know how to make code look nice on the forum, but I forgot. Thanks for the reminder. Also, the only issue now is that it says motorState isn’t declared in the scope. Why would I have to declare it in the scope?

Code can only use variables that are within it’s scope. Most likely, you just defined the variable in the wrong place. If you share you full code, or at least more of it I can tell you were the problem is.

1 Like
#include "vex.h"
using namespace vex;

//#region config_globals
vex::brain Brain;
vex::motor FrontLeft(vex::PORT1, vex::gearSetting::ratio18_1, false);
vex::motor BackLeft(vex::PORT2, vex::gearSetting::ratio18_1, false);
vex::motor FrontRight(vex::PORT3, vex::gearSetting::ratio18_1, true);
vex::motor BackRight(vex::PORT4, vex::gearSetting::ratio18_1, true);
vex::motor Intake(vex::PORT5, vex::gearSetting::ratio18_1, false);
vex::motor Flywheel(vex::PORT6, vex::gearSetting::ratio18_1, true);
vex::controller Controller(vex::controllerType::primary);
//#endregion config_globals

int main(void) 
{
    {
        Brain.Screen.drawImageFromFile("Bluejay.png", -75, -40);
    }
    while(1)
    {
        {
    Controller.Screen.print("Flywheel: %d", Flywheel.velocity(pct));
    }
        {
        if(motorState) Flywheel.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
        if(!motorState) Flywheel.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
        }
        if(Controller.ButtonRight.pressing())
        {
            FrontLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 20,velocityUnits::pct);
            BackLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 20,velocityUnits::pct);
            FrontRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 20,velocityUnits::pct);
            BackRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 20,velocityUnits::pct);
        }
        else if(Controller.ButtonA.pressing())
        {
            FrontLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            FrontRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
        }
        else if(Controller.ButtonLeft.pressing())
        {
            FrontLeft.spin(directionType::rev,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackLeft.spin(directionType::rev,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            FrontRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
        }
        else if(Controller.ButtonY.pressing())
        {
            FrontLeft.spin(directionType::rev,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackLeft.spin(directionType::rev,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            FrontRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
        }
        else if(Controller.ButtonUp.pressing())
        {
            FrontLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 100,velocityUnits::pct);
            BackLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 100,velocityUnits::pct);
            FrontRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct) + 60,velocityUnits::pct);
            BackRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct) + 60,velocityUnits::pct);
        }
        else
        {
            FrontLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct),velocityUnits::pct);
            BackLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct),velocityUnits::pct);
            FrontRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct),velocityUnits::pct);
            BackRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct),velocityUnits::pct);
        }
        if(Controller.ButtonL2.pressing() && Controller.ButtonR2.pressing())
        {
            Intake.spin(directionType::fwd,100,velocityUnits::pct);
        }
        else if(Controller.ButtonR2.pressing())
        {
            Intake.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller.ButtonL2.pressing() && Controller.ButtonR1.pressing())
        {
            Intake.spin(directionType::rev,50,velocityUnits::pct);
        }
        else if(Controller.ButtonR1.pressing())
        {
            Intake.spin(directionType::rev,15,velocityUnits::pct);
        }
        else
        {
            Intake.spin(directionType::rev,0,velocityUnits::pct);
        }
        if(motorState) 
        {
        Flywheel.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
        }
        if(!motorState) 
        {
        Flywheel.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
        }
    }
}

First, you did not declare the two variables that you need. Right after were you define the controller you need to add…

bool clickState = true;
bool motorState = false;

They are variables that you need to use.


Second, you need to add this part to your code too…

if(controller1.ButtonA.pressing() && clickState)
{
    motorState = !motorState;
    clickState = false;
}
if(!controller1.ButtonA.pressing()) clickState = true;

With out this the variable motorState while just stay at false and nothing will happen.


Finally, you have this…

if(motorState) Flywheel.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
if(!motorState) Flywheel.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);

and then later in the code this…

if(motorState) 
{
        Flywheel.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
if(!motorState) 
{
        Flywheel.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
}

You only need on of them, they do the exact same thing.


Also here are some more tips…

I would recommend adding this to your loop…

vex::task::sleep(7);

The explanation is a bit complicated, but just know that it is something you should have, (even though your current would technically function without it).

Also…

int main(void) 
{
    {
        Brain.Screen.drawImageFromFile("Bluejay.png", -75, -40);
    }
    while(1)
    {
        {
    Controller.Screen.print("Flywheel: %d", Flywheel.velocity(pct));
    }
        {
        if(motorState) Flywheel.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
        if(!motorState) Flywheel.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
        }

You have brackets around some of the lines for no reason. It is technically not incorrect code, but they serve no purpose. Also keep your code looking nice; add tabs ever time you go into another set of brackets; the editor even helps, but you have some lines that are further back. It isn’t necessary, but it makes your code a lot easier to read.

2 Likes

It still says there is an issue, but it didn’t point to a particular section of code. All it says is…
Saved Time: 149 ms

lib_type: [default]
Compiling: main.cpp
arm-none-eabi-g++: error: vex_smartdrive.cpp.o: No such file or directory
arm-none-eabi-g++: error: vex_motorgroup.cpp.o: No such file or directory

error: link failed

User build failed.

Compiling failed: Program has errors. See output for details. Time: 450 ms

Here is what I have now:

#include "vex.h"
using namespace vex;

//#region config_globals
vex::brain Brain;
vex::motor FrontLeft(vex::PORT1, vex::gearSetting::ratio18_1, false);
vex::motor BackLeft(vex::PORT2, vex::gearSetting::ratio18_1, false);
vex::motor FrontRight(vex::PORT3, vex::gearSetting::ratio18_1, true);
vex::motor BackRight(vex::PORT4, vex::gearSetting::ratio18_1, true);
vex::motor Intake(vex::PORT5, vex::gearSetting::ratio18_1, false);
vex::motor Flywheel(vex::PORT6, vex::gearSetting::ratio18_1, true);
vex::controller Controller(vex::controllerType::primary);
bool clickState = true;
bool motorState = false;
//#endregion config_globals

int main(void) 
{
    {
        Brain.Screen.drawImageFromFile("Bluejay.png", -75, -40);
    }
    while(1)
    {
        {
    Controller.Screen.print("Flywheel: %d", Flywheel.velocity(pct));
        }
        if(Controller.ButtonRight.pressing())
        {
            FrontLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 20,velocityUnits::pct);
            BackLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 20,velocityUnits::pct);
            FrontRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 20,velocityUnits::pct);
            BackRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 20,velocityUnits::pct);
        }
        else if(Controller.ButtonA.pressing())
        {
            FrontLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            FrontRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
        }
        else if(Controller.ButtonLeft.pressing())
        {
            FrontLeft.spin(directionType::rev,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackLeft.spin(directionType::rev,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            FrontRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
        }
        else if(Controller.ButtonY.pressing())
        {
            FrontLeft.spin(directionType::rev,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackLeft.spin(directionType::rev,Controller.Axis3.position(percentUnits::pct) + 75,velocityUnits::pct);
            FrontRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
            BackRight.spin(directionType::rev,Controller.Axis2.position(percentUnits::pct) + 75,velocityUnits::pct);
        }
        else if(Controller.ButtonUp.pressing())
        {
            FrontLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 100,velocityUnits::pct);
            BackLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct) + 100,velocityUnits::pct);
            FrontRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct) + 60,velocityUnits::pct);
            BackRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct) + 60,velocityUnits::pct);
        }
        else
        {
            FrontLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct),velocityUnits::pct);
            BackLeft.spin(directionType::fwd,Controller.Axis3.position(percentUnits::pct),velocityUnits::pct);
            FrontRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct),velocityUnits::pct);
            BackRight.spin(directionType::fwd,Controller.Axis2.position(percentUnits::pct),velocityUnits::pct);
        }
        if(Controller.ButtonL2.pressing() && Controller.ButtonR2.pressing())
        {
            Intake.spin(directionType::fwd,100,velocityUnits::pct);
        }
        else if(Controller.ButtonR2.pressing())
        {
            Intake.spin(directionType::fwd,50,velocityUnits::pct);
        }
        else if(Controller.ButtonL2.pressing() && Controller.ButtonR1.pressing())
        {
            Intake.spin(directionType::rev,50,velocityUnits::pct);
        }
        else if(Controller.ButtonR1.pressing())
        {
            Intake.spin(directionType::rev,15,velocityUnits::pct);
        }
        else
        {
            Intake.spin(directionType::rev,0,velocityUnits::pct);
        }
        if(Controller.ButtonA.pressing() && clickState)
        {
            motorState = !motorState;
            clickState = false;
        }
        if(!Controller.ButtonA.pressing()) clickState = true;
        if(motorState) 
        {
        Flywheel.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
        }
        if(!motorState) 
        {
        Flywheel.spin(vex::directionType::fwd, 0, vex::velocityUnits::pct);
        }
    }
}

I am not fully sure why this is happening. It say that it cannot find a file that you are including (or that some file from the compile step is missing). It doesn’t seem to be a problem in this file though.

Only thing that I would try is to compile again or restarting VexCode.

1 Like

I’m using RMS C++, not VexCode.

In that case I am really not sure, maybe someone else will be able to help out.

1 Like

Is the code set up how it’s supposed to be though?

Yes it all looks good, just add

vex::task::sleep(7);

in the loop.
Also maybe format your code nicely and remove extra brackets.:wink:

1 Like