HELP! Program multiple Bump Switches -VexVRPro

I have bumper car built with 2 motors and 8 different bumper switches (2 per side of car). How can I program multiple bumper switches to cause the car to move in different directions based on where it is hit (which bumper is pressed)?

Using VexVRPro (text C++)

I want the car to move once program is ran, then react once one of the 8 bumpers are hit. Each bumper would be programmed to move the car in a different direction.

this is what I have thus far, but it only runs the initial program, nothing happens with bumper 1. Also need to add the other bumpers in.

any thoughts???

while(true){

Motor1.setVelocity(100,rpm);
Motor2.setVelocity(100,rpm);
Motor1.spin(forward);
Motor2.spin(forward);
}
if(B1.pressing());
Motor1.setVelocity(100,rpm);
Motor2.setVelocity(70,rpm);
Motor1.spin(reverse);
Motor2.spin(reverse);

}

Below is one option using the competition example. There is a separate function called SetSpeed that controls the chassis motors. The usercontrol routine simply sends a request to the SetSpeed function based on your design.


//robot-config.cpp
motor LeftMotor = motor(PORT1, ratio18_1, false);
motor RightMotor = motor(PORT2, ratio18_1, true);
bumper BumperA = bumper(Brain.ThreeWirePort.A);
bumper BumperB = bumper(Brain.ThreeWirePort.B);
bumper BumperC = bumper(Brain.ThreeWirePort.C);
//main.cpp
#include "vex.h"

using namespace vex;
competition Competition;

void pre_auton(void) {
  vexcodeInit();
}

void autonomous(void) {
}

//Stops and then sets the new speed of the motors. 
// Use negative values for reverse.
void SetSpeed(int left, int right){
  LeftMotor.stop();
  RightMotor.stop();
  wait(1, sec);
  LeftMotor.setVelocity(left, percent);
  RightMotor.setVelocity(right, percent);
}

void usercontrol(void) {
  LeftMotor.spin(forward);
  RightMotor.spin(forward);
  SetSpeed(30,30);
  while (1) {
    //Various bumper press send a request to change the speed.
    if(BumperA.pressing())
      SetSpeed(60,-60);
   if(BumperB.pressing())
      SetSpeed(60,60);
   if(BumperC.pressing())
      SetSpeed(-30,30);
    wait(20, msec);
  }
}


int main() {
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

  pre_auton();

  while (true) {
    wait(100, msec);
  }
}
3 Likes

Thank you! Will this work if I want it completely autonomous? No controller.

The above code will work. If you don’t want to use the competition template you can use the following which puts the main block of code in the main function.

#include "vex.h"
using namespace vex;

void SetSpeed(int left, int right){
  LeftMotor.stop();
  RightMotor.stop();
  wait(1, sec);
  LeftMotor.setVelocity(left, percent);
  RightMotor.setVelocity(right, percent);
}

int main() {
  vexcodeInit();

  LeftMotor.spin(forward);
  RightMotor.spin(forward);
  
  while (1) {
    //Various bumper press with a change in the speed function.
    if(BumperA.pressing())
      SetSpeed(60,-60);
    else if(BumperB.pressing())
      SetSpeed(60,60);
    else if(BumperB.pressing())
      SetSpeed(-30,30);
    else
      SetSpeed(30,30);
    wait(20, msec);
  }
}
2 Likes