How to switch between different drive codes?

We have our tournament in 2 days, and in 2 days we’ve blown out 2 motors, so we cannot practice in case we blow out ANOTHER motor. Ours are two years old, so lol. I have two different drive codes: one that’s normal and one in reverse. I can switch between the two with two buttons. But then I tried to add a new drive code switcher using integers: 1 for normal, 2 for reverse, 3 for precision. for some reason it didn’t work at all. I then tried an AND gate with booleans: it runs, but for some reason it doesn’t actually read controller input? I don’t understand this at all. I’m away from my computer rn, but when I do get it I’ll send all the code for it. Can any of you think of a way to switch drive code, or done it yourself?

Can you post your code? That helps us a great deal. If posting text code please place it between two sets of ``` (three ticks in the keyboard upper left). If posting blocks then paste a screenshot.

1 Like

I can’t post the code thst doesn’t work right now since I’m away from the computer, but I can post the one that DOES work. If you find a function called ‘ ’ ’bruhmoment();’ ’ ’ that’s how it works.

And I don’t see the reverse ‘ it must not be on iPad?

Good, descriptive function and variable names are important

1 Like

Lmao you should see me in Unreal Engine

I’m thinking a Toffoli gate can work for this. I was kinda trying to code this anyways, just badly. It can convert booleans to integers.

That is a lot of code and it looks like a lot of repeat code which you will want to minimize.

Below is one concept. I created a chassisDirection variable with a ReverseDirection function. When you press the A button it will tell the chassisDirection variable to reverse one time and then it sets the ReverseLatch so the program doesn’t keep flipping Fwd/Rev every 20 ms.

You also don’t need the if > 0 or < 0 to determine direction. The controller sticks range from 100 to -100 so it automatically handles that.

directionType chassisDirection = directionType::fwd;
bool ReverseLatch = false;

void ReverseDirection(){
  if(ReverseLatch) return;
  ReverseLatch = true;
  if(chassisDirection == fwd) {
    chassisDirection = reverse;
    return;
  }
  chassisDirection = fwd;
}

void DriveCode() {
  int rightValue = static_cast <int>(pow(Controller1.Axis2.value(), 2)/50);
  int leftValue = static_cast <int>(pow(Controller1.Axis3.value(), 2)/50);
  
  leftSide.spin(chassisDirection, leftValue, pct);  
  rightSide.spin(chassisDirection, rightValue, pct); 
}

void DriverControl(){
  while(true){

    DriveCode();
    
    if(Controller1.ButtonA.pressing()){
      ReverseDirection();
    } else {
      ReverseLatch = false;
    }
  }
}
3 Likes

Doesn’t seem necessary here. A simpler solution would be a switch statement.
https://en.cppreference.com/w/cpp/language/switch

So in a switch statement, i would increase every time I click the button?

Btw I made the toffoli gate work, with some help of a teacher who knows c++

If you made it work, you should probably just keep it. But here’s how you would do it with a switch

int driveMode = 0;  //this variable stores the current mode
void changeDriveMode(){
  driveMode++;  //increase the driveMode
  if(driveMode > 2) driveMode = 0;  //but if it becomes too large, reset it to 0
}

void driverControl(){
  Controller.ButtonA.pressed(changeDriveMode);

  while(true){
    switch(driveMode){
      case 0:{
        //put code for mode 1 here
      } break;
      case 1:{
        //put code for mode 2 here
      } break;
      case 2:{
        //put code for mode 3 here
      } break;
    }
  }
}
3 Likes

thank you. it luckily works the way i want it to