# Code help (Motor)

So i want the R1 to spin a motor reverse, and I want L1 to spin the motor Forward but is is not working

``````if(Controller1.ButtonR1.pressing())
mid.spin(reverse);

if(Controller1.ButtonL1.pressing())
mid.spin(forward);
``````

I am no coding expert, but

I thing you need brackets?? also you have to tell the motors to stop at some pointâ€¦
and you need the motors to move on some value I believe, whether velocity or voltage.
see if this works

``````//reverse
if(Controller1.ButtonR1.pressing()) {
mid.spin(reverse, 12, voltageUnits::volts);
} else {
mid.stop();
}
//forward
if(Controller1.ButtonL1.pressing()) {
mid.spin(forward, -12, voltageUnits::volts);
} else {
mid.stop();
}
``````

-12 volts is the same as going backwards, also u donâ€™t have to have voltage control, but thatâ€™s what I do.

You donâ€™t need bracket for an if statement if there is only one function. Can you describe what exactly is not working? (i.e. motors donâ€™t spin, they never start spinning, they spin once but donâ€™t again, etc.)

1 Like

eah, @JThomas is right. You need brackets.

``````if(Controller1,ButtonR1.pressing()){
mid.spin(reverse,100,velocityUnits::pct); /* pct runs the motors on a
percentage of their overall power, which I prefer over volts personally,
and find it easier to use/*
} // First 'if' statement spins the motor in reverse when conditions are met
else{
mid.stop();
} // If these conditions are not met, don't spin the motor

if(Controller1.ButtonL1.pressing()){
mid.spin(forward,100,velocityUnits::pct);
} // When conditions are met, fulfill the code in the brackets
else{
mid.stop();
} // if not, stop the motor.
``````

Really quick piece of advice in relation to voltage control: The newer rev 10 motors have been proven to die when quickly switching between 12 and -12 volts. I would recommend being careful when using voltage control (a.k.a. limiting voltage to Â±11 volts, introducing a slew rate, or using the native velocity controller).

3 Likes

Hereâ€™s a recommend correction. The code as shown above may have a problem running. We need 3 states: Fwd, Rev, & Stop. Having two Stops instead of one will cause them to stop the other.

3 Likes

The way you are trying to code it looks like python but since the bots use C++ you will need to use brackets when using conditional statements like â€śifâ€ť, â€śelseâ€ť, â€śwhileâ€ť and â€śforâ€ť. You will also need to switch the second condition to an â€śelse ifâ€ť statement so that if you hold down both buttons the motor wonâ€™t try to spin both ways, you also need an else statement after the first 2 to stop the motor if no button is being pressed. Here is the correctly formatted code.

``````if(Controller1.ButtonR1.pressing()){
mid.spin(reverse, 100, percent);
//change motor speed by changing the
//100 to a number from -100 to 100
}
else if(Controller1.ButtonL1.pressing()){
mid.spin(forward, 100, percent);
//change motor speed by changing the
//100 to a number from -100 to 100
}
else{
mid.stop();
//if the "mid' motor is a flywheel or launcher
//use this command instead to keep the motor
//from burning out: mid.stop(coast);
}
``````
2 Likes

V5 supports Python as well as C++.

6 Likes

Right my bad forgot about that, dudeâ€™s code just looked like he was trying to use C++ but with python formatting

1 Like

You actually donâ€™t always need brackets when coding in c++, and my team uses this in a couple places in our code. However, it works differently then in python. It only runs the line immediately after it
This code:

``````if (true)
function1();
else
function2();
``````

Compiles and runs the same as this:

``````if (true) {
function1();
}
else {
function2();
}
``````

However this:

``````if (true)
function1();
function2();
``````

Is actually the same as this:

``````if (true) {
function1();
}
function2();
``````

As for the OPâ€™s question, I canâ€™t really answer it without seeing more of the surrounding code and knowing what it is doing that is wrong

4 Likes

But you still donâ€™t need brackets when you have one line inside of an if statement or loop

While the option to use it without the brackets works and compiles the same, I think it is a good habit to get. This is especially true for begginer programmers because from my experience teaching others on my team if you tell someone that you can write an if statment without the brackets, they will try to write stuff like your last example and get confused when it doesnâ€™t work.

3 Likes

Ok Iâ€™ve put this code into vex, and it still wonâ€™t turn. I checked that the motor is connected properly and everything.

Mind just copy pasting you whole code so we can look at the surrounding code?

1 Like

I made new code and It worked

{

``````mid.setVelocity(100, percent);

while (true) {
if (Controller1.ButtonL1.pressing()) {
mid.spin(forward);
} else if (Controller1.ButtonR1.pressing()) {
mid.spin(reverse);
} else {
mid.stop();
}
wait(25, msec);

}
``````

}

}

1 Like