-1
if(SensorValue (bumpswitch)==1)
{
    startMotor(WheelMotor2,-20);
}
if(SensorValue (bumpswitch2)==1)
{
    startMotor(WheelMotor2,20);
}
if(SensorValue(bumpswitch3)==1)
{
    startMotor(LiftMotor,30);
}
if(SensorValue(bumpswitch3)==0)
{
    stopMotor(LiftMotor);
}
if (SensorValue(bumpswitch4)==1)
{
    startMotor(TopMotor,120);
}

} How do we get all of these motors to move with one bump switch? Is there a better way to code this?

1 Answers1

0

It appears that you are missing the else keyword.

To explain, here's a bit of code similar to yours:

if (condition1) {action1;}
if (condition2) {action2;}

If both are true, both are executed. This means that if both bumpswitch and bumpswitch2 have value 1, then the code will first spin the motor at 20 and then immediately change it to -20.

if (condition1) {action1;}
else if (condition2) {action2;}

This is equivalent to:

if (condition1) {
    action1;
} else {
    if (condition2) {
        action2;
    }
}

If condition1 is satisfied, then it will run the first part (action1). If it is not satisfied, and condition2 is satisfied, then it will run the second part. So:

if (SensorValue(bumpswitch) == 1) { startMotor(WheelMotor2, -20); } else if (SensorValue(bumpswitch2) == 1) { startMotor(WheelMotor2, 20); }

will fix the first issue.

Now, as for a better way to code this, there isn't really much to improve on. If you want more tips on making your code better, you may want to check out CodeReview.SE.

Your final code should look like this:

if (SensorValue(bumpswitch) == 1) {
    startMotor(WheelMotor2, -20);
} else if (SensorValue(bumpswitch2) == 1) {
    startMotor(WheelMotor2, 20);
}

if (SensorValue(bumpswitch3) == 1) {
    startMotor(LiftMotor, 30);
} else if (SensorValue(bumpswitch3) == 0) {
    stopMotor(LiftMotor);
}

if (SensorValue(bumpswitch4) == 1) {
    startMotor(TopMotor, 120);
}

Note that they're not all else if blocks, because otherwise only one action could be executed.

Alternatively, if the issue is that the motors are starting one by one, then this should work:

if (SensorValue(bumpswitch) == 1) {
    new Thread() {
        public void run() {
            startMotor(WheelMotor2, -20);
        }
    }).start();
} else if (SensorValue(bumpswitch2) == 1) {
    new Thread() {
        public void run() {
            startMotor(WheelMotor2, 20);
        }
    }).start();
}

if (SensorValue(bumpswitch3) == 1) {
    new Thread() {
        public void run() {
            startMotor(liftMotor, 30);
        }
    }).start();
} else if (SensorValue(bumpswitch3) == 0) {
    new Thread() {
        public void run() {
            stopMotor(liftMotor);
        }
    }).start();
}

if (SensorValue(bumpswitch4) == 1) {
    new Thread() {
        public void run() {
            startMotor(TopMotor, 120);
        }
    }).start();
}

This starts a new thread for each action so they run in parallel.

else if (SensorValue(bumpswitch3) == 0) can be replaced with else if the sensor value has to be either 1 or 0, because if (c1) {a1;} else {a2;} will run a1 if c1, and otherwise and only otherwise, a2.

hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50