Vex Coding Studio Mistake

I am trying to learn how to code and I wrote a program. It says it is wrong but I can’t figure out what is wrong. Can anyone help?

#include “robot-config.h”

int main() {

while(true) {
    LeftMotor.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis3+Axis4)/2
    RightMotor.spin(vex::directionType::rev, Controller1.Axis2.value(), vex::velocityUnits::pct);//(Axis3-Axis4)/2
    
    LeftClawMotor.spin(vex::directionType::rev, Controller1.ButtonL1.pressing(), vex::velocity::pct,false);
    RightClawMotor.spin(vex::directionType::fwd, Controller1.ButtonL1.pressing(), vex::velocity::pct,true);
    
    LeftTiltMotor.spin(vex::directionType::fwd, Controller1.ButtonUp.pressing(), vex::velocity::pct,false);
    RightTiltMotor.spin(vex::directionType::rev, Controller1.ButtonUp.pressing(), vex::velocity::pct,true);
    
    vex::task::sleep(20); 
}

}

1 Like

I think the most help I can give you right now is advice on how to ask for coding help:

First up, you posted code. This is good. The only critique I would offer on this part is to suggest formatting it for the forum a little better. The </> button at the top of the post editor will take what you have highlighted and format it like code. Something like this:

//demonstrating
    //code
//formatting

Next up, you usually want to include a description of what the code should do. In this case it’s somewhat implied that you want it to compile. Just be sure it’s clear what you expected it to do.

Which brings us to: a description in as much detail as possible of what went wrong. This is where your post here needs some work. “It says it is wrong” is not descriptive enough. Is the compiler giving you a warning? An error? What is the error? If it’s giving you error output, copying that into your post or giving some information about it is incredibly useful.

The last piece to include when asking for coding help is a description of what you’ve already tried.

4 Likes

Your Claw and Tilt motors speed is being defined by the results of a controller button, which is a Boolean true or false. The computer doesn’t know what to do with a true/false when it is looking for a number.

The fix is to use if/else statements to tell these motors what to do. Below is a sample of your tilt motors formatted as such.

if(Controller1.ButtonUp.pressing()){
    LeftTiltMotor.spin(vex::directionType::fwd, 100, vex::velocity::pct,false);
    RightTiltMotor.spin(vex::directionType::rev, 100, vex::velocity::pct,true);
}
else{
    LeftTiltMotor.stop();
    RightTiltMotor.stop();
}

As to what John Tyler Said, please explain what the intent of the code is in the future. A quick reading indicates this is a remote control code.

1 Like

It does, actually. (In some languages like C++, anyway.) True casts to 1, false casts to 0. This is a program logic error as his code could only ever command a paltry 1% speed, but it’s not the source of his compiler error.

1 Like

My mistake, thank you for the correction. I will have to test this the next time I have access to VexCode tonight.

The code is a driver controls program. I have the wheel base working but I need the claw and the motors that tilt the claw to work. The error says that. " No Member Names ’ velocity’ in name space ‘vex’. Thank you all for your help.

I see it, in your tilt and claw motors, you have “vex::velocity::pct”. these should be “vex::velocityUnits::pct”. That should fix your compiler error. My if/else statement will still be needed to make the motors move with any speed.

So OP’s code does “work” in that sense, but using the Boolean value of the controller button as the speed input means that the motor will be told to spin at 1% speed when the button is pressed, and 0% speed otherwise. I’m not sure what happens when you ask a V5 motor to spin at 1% speed but my guess is that in most cases it doesn’t actually spin due to mechanical resistance.

Taking the idea of casting the bool to an int a bit further, to avoid this problem we can multiply the Boolean value by a number:

LeftTileMotor.spin(vex::directionType::fwd, Controller1.ButtonL1.pressing() * 100, vex::velocityUnits::pct);

Controller1.ButtonL1.pressing() * 100 will evaluate to 100 when the button is pressed, and 0 when it isn’t. If you want the motor to spin at some other speed, multiply by that instead of 100.

Another option to avoid the if statement is to use ternary operators:

LeftTileMotor.spin(vex::directionType::fwd, Controller1.ButtonL1.pressing() ? 100 : 0, vex::velocityUnits::pct);

In general, the expression boolean_exp ? a : b will evaluate to a if boolean_exp evaluates to true and b if boolean_exp evaluates to false

I should add that while this sort of thing is interesting and cool, it’s inadvisable to do it in production, as deliberately and unnecessarily obfuscating your code is a bad idea. You should base your code on the sample @invalidflaw provided instead.

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.