A simple "else" statement in my autonomous code is not working

Hello because this year I am my auton focus is on the rollers I made an auton code using what I call “Keys” where it’s a limit switch that when pressed will do the red side when not pressed will do the blue side. the red side works just fine but after I tried to add a else statement to run the blue side and I got an error. Also I am a newbie to vexcode and C++ so if this works and I’m just being a bit dumb please tell me. any ways here is my code

if (keyRandB.pressing()

)roller.spin(directionType::fwd, 100, velocityUnits::pct);

wait(1500, msec);

frontright.spin(directionType::fwd, 200, velocityUnits::pct);

wait(750, msec);

frontright.spin(directionType::rev, 200, velocityUnits::pct);

frontleft.spin(directionType::rev, 200, velocityUnits::pct);

backright.spin(directionType::rev, 200, velocityUnits::pct);

backleft.spin(directionType::rev, 200, velocityUnits::pct);

wait(2000, msec);

backleft.spin(directionType::fwd, 200, velocityUnits::pct);

wait(750, msec);

frontright.spin(directionType::rev, 200, velocityUnits::pct);

frontleft.spin(directionType::rev, 200, velocityUnits::pct);

backright.spin(directionType::rev, 200, velocityUnits::pct);

backleft.spin(directionType::rev, 200, velocityUnits::pct);

wait(3000, msec);

}

else

{

roller.spin(directionType::rev, 100, velocityUnits::pct);

wait(1500, msec);

frontright.spin(directionType::fwd, 200, velocityUnits::pct);

wait(750, msec);

frontright.spin(directionType::rev, 200, velocityUnits::pct);

frontleft.spin(directionType::rev, 200, velocityUnits::pct);

backright.spin(directionType::rev, 200, velocityUnits::pct);

backleft.spin(directionType::rev, 200, velocityUnits::pct);

wait(2000, msec);

backleft.spin(directionType::fwd, 200, velocityUnits::pct);

wait(750, msec);

frontright.spin(directionType::rev, 200, velocityUnits::pct);

frontleft.spin(directionType::rev, 200, velocityUnits::pct);

backright.spin(directionType::rev, 200, velocityUnits::pct);

backleft.spin(directionType::rev, 200, velocityUnits::pct);

wait(3000, msec);

}

Probably should add that it is the else statement straight in the middle

What is the error you’re getting and what behavior are you seeing that it is doing?

Also, very helpful to surround your code with the code block markers, as it makes it easier to read the code.

1 Like

@EngineerMike this is the error message. expected unqualified-id (79, 1)

Reformatting the code so it’s easier to read as a whole.

if (keyRandB.pressing()

)roller.spin(directionType::fwd, 100, velocityUnits::pct);

wait(1500, msec);

frontright.spin(directionType::fwd, 200, velocityUnits::pct);

wait(750, msec);

frontright.spin(directionType::rev, 200, velocityUnits::pct);

frontleft.spin(directionType::rev, 200, velocityUnits::pct);

backright.spin(directionType::rev, 200, velocityUnits::pct);

backleft.spin(directionType::rev, 200, velocityUnits::pct);

wait(2000, msec);

backleft.spin(directionType::fwd, 200, velocityUnits::pct);

wait(750, msec);

frontright.spin(directionType::rev, 200, velocityUnits::pct);

frontleft.spin(directionType::rev, 200, velocityUnits::pct);

backright.spin(directionType::rev, 200, velocityUnits::pct);

backleft.spin(directionType::rev, 200, velocityUnits::pct);

wait(3000, msec);

}

else

{

roller.spin(directionType::rev, 100, velocityUnits::pct);

wait(1500, msec);

frontright.spin(directionType::fwd, 200, velocityUnits::pct);

wait(750, msec);

frontright.spin(directionType::rev, 200, velocityUnits::pct);

frontleft.spin(directionType::rev, 200, velocityUnits::pct);

backright.spin(directionType::rev, 200, velocityUnits::pct);

backleft.spin(directionType::rev, 200, velocityUnits::pct);

wait(2000, msec);

backleft.spin(directionType::fwd, 200, velocityUnits::pct);

wait(750, msec);

frontright.spin(directionType::rev, 200, velocityUnits::pct);

frontleft.spin(directionType::rev, 200, velocityUnits::pct);

backright.spin(directionType::rev, 200, velocityUnits::pct);

backleft.spin(directionType::rev, 200, velocityUnits::pct);

wait(3000, msec);

}

It looks like you’re missing an open brace at the beginning of the if loop, and the closing parenthesis of the ‘if’ statement is pushed to the next line in front of the roller spin command.

if (keyRandB.pressing()

)roller.spin(directionType::fwd, 100, velocityUnits::pct);

^ So that should be replaced with this:

if (keyRandB.pressing()) {

roller.spin(directionType::fwd, 100, velocityUnits::pct);

Without that opening brace the if statement ended after the first roller command and there were a lot of other commands between that and the else statement so the compiler got confused at what you were trying to tell it to do.

That’s the only thing that I can see that would cause it to error out, but there might be some unintended behavior depending on how you plan on using the limit switch. Just to clarify, It sounds like you’d need something holding the limit switch down during the match to keep it set to red, is this what you’re planning on doing?

4 Likes

In the first line you are missing a closing ). After that change i would move the first bracket for the if statement onto the first line to make it easy to read down the road.

could just be a typo, but in java the lack of an opening bracket will cause it to run the next line in the if statement.

if (keyRandB.pressing())
     roller.spin(directionType::fwd, 100, velocityUnits::pct);
wait(1500, msec);

vs

if (keyRandB.pressing()){
     roller.spin(directionType::fwd, 100, velocityUnits::pct);
    wait(1500, msec);
}

Just a guess?

1 Like