Simple code, not so simple

Does anyone have a suggestion why this code isn’t working properly. When the program is run, the motor immediately starts without the bump being pressed.
{
(BumperF.pressed());

LeftMotor.setVelocity(50, percent);
LeftMotor.spin(forward);
wait(5, seconds);

LeftMotor.stop();
}

what exactly do you want your code to do?
(BumperF.pressed()); is not how you detect a bumper press.

try

if(BumperF.pressing()){
//motor commands here
}

Also you’re telling LeftMotor to stop instead of RightMotor, although that could be intentional.

3 Likes

Wanting a basic code for beginners: that once the bump switch is pressed, the motor starts for 5 seconds and stops.

Try putting your motor commands inside that if statement. They will only run when the program detects that the bump switch is pressed.

3 Likes

Good morning,
Is there a way around the “if” statement? I wasn’t ready to introduce “if” statements yet.

The way around if statements involves syntax much more complicated than if statements.

5 Likes

Here is the code with the if statement: It’s still not working.

int main()

{

if (BumperF.pressing())

{
LeftMotor.setVelocity(50, percent) ;
LeftMotor.spin(forward);
wait(5, seconds);
LeftMotor.stop();
}

}

What behavior does it execute? Do any of the motors spin at all?

It doesn’t do anything. No behavior executes. OH, the LED is on.

Your code needs to be surrounded in a while statement…

A while statement makes it so the code checks the statement frequently and continuously. Right now it only checks the if statement once.

E.G.

int main(){
while(true)
{
//if statement thing here
wait(0.1, seconds);
//This wait statement prevents the loop from going to fast
}}

Let me know if you have questions.

4 Likes

Sam is correct. You need a while loop surrounding your if statement.

4 Likes

Your code needs to have a while statement and an if statement, if I’m correct! Hope this helps!

1 Like

Thanks that worked. Thanks for the help. I was attempting to use V5 Text for PLTW POE Control Systems versus Robot C.

For RobotC, I didn’t have to use the while and if statements for a basic code, it was simply:

untilbump(bumpswitch)
startmotor(rightmotor, 63);
wait(5);
stopmotor(rightmotor);

1 Like

If you’re trying to create “simple” projects, why not use Blocks? It generates the same C++ code as if you were doing it in VEXcode Pro/Text - so there’s no disadvantage in terms of execution speed.

4 Likes

I wanted to use the text, since we tend to compete in VEX competitions.

Good choice. If you have any more issues or questions, feel free to ask, and we’ll be more than happy to assist.

Great and Thank you to everyone.

1 Like

hehe

VEXcode’s block interface is 100% competition legal and capable. A number of teams have made it to VEX Worlds programming in block-based coding.

2 Likes

The reason for this is that the untilbump method essentially does

while(!bumpswitch.pressing()){
    sleep(20);
}

If you want to maintain a similar structure to that code, then the code can be rewritten as:

int main()
{
	while(!BumperF.pressing()){	//this loop effectively replaces the untilbump() method
		wait(0.02, seconds);
	}

	LeftMotor.setVelocity(50, percent) ;
	LeftMotor.spin(forward);
	wait(5, seconds);
	LeftMotor.stop();
}
3 Likes