Can't make the code work how I want

Hey there,

I did a course at Carnegie over the summer but my files didn’t transfer properly. So I am trying to get something done from memory and it is not working. I want my students to make the Vex EXP clawbot move using the remote control. We have the claw, arm and drive motors working fine. However, I want to play a game of tag so if their rear bumper gets pressed, I want the drive motors to stop until the student resets their code on the robot. I can’t seem to remember how to make this work. I have it written in notes but it doesn’t seem to be working. Here’s what we have, we are using C++:

// Include the VEX Library
#include "vex.h"
  
// Allows for easier use of the VEX Library
using namespace vex;

int main() {
 clawMotor.setPosition(0, degrees);
  armMotor.setPosition(0, degrees);
  clawMotor.setStopping(hold);
  armMotor.setStopping(hold);
  clawMotor.setTimeout(5, seconds);
  armMotor.setTimeout(5, seconds);
  clawMotor.setVelocity(600, percent);
  armMotor.setVelocity(30, percent);
  rightMotor.setVelocity(30, percent);
  leftMotor.setVelocity(30, percent);
 

 
while (1==1) {
if (Controller.ButtonR1.pressing()) {
  clawMotor.spin(forward, 30, percent);
}
else if (Controller.ButtonR2.pressing()) {
  clawMotor.spin(reverse, 30, percent); 
}
else {
  clawMotor.stop(hold);
}
if (Controller.ButtonL1.pressing()) {
 armMotor.spin(forward, 30, percent);
}
else if (Controller.ButtonL2.pressing()) {
 armMotor.spin(reverse, 30, percent);
}
else {
   armMotor.stop(hold);
}
rightMotor.spin(forward, Controller.Axis2.position(percent)/2, percent);
leftMotor.spin(forward, Controller.Axis3.position(percent)/2, percent);
}
while (!rearBumper.pressing()) {
  leftMotor.stop();
  rightMotor.stop();
}
}

If you only want the drive motors to stop when the bumper is pressed, remove the ! in front of the rearBumper.pressing() in the while loop. Right now you are telling it to stop when the bumper is not being pressed. And if you want to robots drive to stop completely when the bumper is pressed, that while loop is not going to work, because all its doing is stopping the robot while the bumper is pressed, when the bumper is released the robot should be able to move again. Use a boolean to allow the drive motors to move, and have an if statement so that when the bumper is pressed, the boolean is set to false, and that boolean cant be changed back to true unless you restart the code.

If you use the stop project command it should stop until the driver restarts the program

Thanks. I actually just replaced the 1==1 with the !rearbumper.pressing and it seems to be working.

1 Like