Issues with Switching Motor Speed by Pressing the Button

I have tried to create a program during driver control period that switches the speed to 60% of the speed of the joysticks by clicking the Up Button and 100% of the speed of the joysticks by clicking it again. However, the string “driver” was listed as both unused and undeclared identifiers. Is there anything wrong with the code? Can someone please find similar threads/solutions to refer me to? Thanks in advance!

if (Controller1.ButtonUp.pressing()) {
       driveSpeed += 1.0;
    }
    else {
      driveSpeed += 0.0;
    if (remainder(driveSpeed, 2) = 0) {
      double driver = 0.6;
    }
    else {
      double driver = 1.0;
    }
    // Set the speed of the left motor. If the value is less than the deadband,
    // set it to zero.
    if (abs(Controller1.Axis3.position()) > 10) {
      // Set the speed to zero.
      LeftSide.spin(forward, (Controller1.Axis3.position()*driver), percent);
    } 
    else if (Controller1.ButtonA.pressing()) {
      // Set the speed to leftMotorSpeed
      LeftSide.spin(forward, 10, percent);
    }
    else if (Controller1.ButtonY.pressing()) {
      // Set the speed to leftMotorSpeed
      LeftSide.spin(reverse, 10, percent);
    }
    else {
      LeftSide.stop(brake);
    }

  // Set the speed of the left motor. If the value is less than the deadband,
    // set it to zero.
    if (abs(Controller1.Axis2.position()) > 10) {
      // Set the speed to zero.
      RightSide.spin(forward, (Controller1.Axis2.position()*driver), percent);
    } 
    else if (Controller1.ButtonA.pressing()) {
      // Set the speed to leftMotorSpeed
      RightSide.spin(forward, 10, percent);
    }
    else if (Controller1.ButtonY.pressing()) {
      // Set the speed to leftMotorSpeed
      RightSide.spin(reverse, 10, percent);
    }
    else {
      RightSide.stop(brake);
    }

The concept you are running afoul of is called “scoping”. You declare driver in two places, but because the scopes of each declaration are limited to the blocks that the declarations appear in, the declarations have gone out of scope and been discarded by the time you try to read them.

2 Likes

Can you please provide a fix for it?

Short answer: no.

Long answer: look up and learn about C++ scoping rules, specifically block scope. This is something you’re going to need to know to write C++, so I’m not inclined to just provide the answer without you learning why it’s the answer. And the answer should be readily apparent once you learn about block scoping.

1 Like