FRC problem with getting to run a motor for a certain amount of time

So I’m trying to have the code run the motor for a certain amount of time based on what the REV color sensor V3 senses. I’ve tried many things. but in the end it just gets stuck in an infinite loop. I don’t know how to fix this thing I don’t know if there is a way to do this rather then what I’m trying to do.

Here’s the code:

private void redDetect(){
  long t= System.currentTimeMillis();

  if (buttons[9]){
    long endg = t+15000; 
    while (System.currentTimeMillis() < endg) {
      trenchMotor.set(0.6);
      break;
    }
  }
  if (buttons[10]){
    long endy = t+10000; 
    while (System.currentTimeMillis() < endy) {
      trenchMotor.set(0.6);
      break;
    }
  }
    if (buttons[11]){
      long endr = t+7000; 
      while (System.currentTimeMillis() < endr) {
        trenchMotor.set(0.6);
        break;
      }
    }
      if (buttons[12]){
        long endb = t+5000; 
        while (System.currentTimeMillis() < endb) {
          trenchMotor.set(0.6);
          break;
        }
    } 
} 

private void blueDetect(){
  long t= System.currentTimeMillis();

  if (buttons[9]){
    long endg = t+15000; 
    while (System.currentTimeMillis() < endg) {
      trenchMotor.set(0.6);
      break;
    }
  }
  if (buttons[10]){
    long endy = t+10000; 
    while (System.currentTimeMillis() < endy) {
      trenchMotor.set(0.6);
      break;
    }
  }
    if (buttons[11]){
      long endr = t+7000; 
      while (System.currentTimeMillis() < endr) {
        trenchMotor.set(0.6);
        break;
      }
    }
      if (buttons[12]){
        long endb = t+5000; 
        while (System.currentTimeMillis() < endb) {
          trenchMotor.set(0.6);
          break;
        }
    } 
} 

private void greenDetect(){
int endg = 10;

  if (buttons[9]){
    while (endg > 0) {
      trenchMotor.set(0.6);
      endg --; 

      break;
    }
  }
  if (buttons[10]){
    int endy = 1000;
    while (endy > 0) {
      trenchMotor.set(0.6);
      endy--;
      break;
    }
  }
    if (buttons[11]){
      int endr = 7000; 
      while (endr > 0) {
        trenchMotor.set(0.6);
        endr--;
        break;
      }
    }
      if (buttons[12]){
        int endb = 5000; 
        while (endb > 0) {
          trenchMotor.set(0.6);
          endb--;
          break;
        }
    } 
} 

private void yellowDetect(){
  long t= System.currentTimeMillis();

  if (buttons[9]){
    long endg = t+15000; 
    while (System.currentTimeMillis() < endg) {
      trenchMotor.set(0.6);
      break;
    }
  }
  if (buttons[10]){
    long endy = t+10000; 
    while (System.currentTimeMillis() < endy) {
      trenchMotor.set(0.6);
      break;
    }
  }
    if (buttons[11]){
      long endr = t+7000; 
      while (System.currentTimeMillis() < endr) {
        trenchMotor.set(0.6);
        break;
      }
    }
      if (buttons[12]){
        long endb = t+5000; 
        while (System.currentTimeMillis() < endb) {
          trenchMotor.set(0.6);
          break;
        }
    } 
} 
if(detectedColor.red > detectedColor.green && detectedColor.red > detectedColor.blue) {
      redDetect();
    }

    if(detectedColor.blue > detectedColor.green && detectedColor.blue > detectedColor.red) {
      blueDetect();
    }

    if(detectedColor.green > detectedColor.blue && detectedColor.green > detectedColor.red) {
      greenDetect();
    }

You should not use while loops for long periods of time. The robot calls the iterative methods every 20 milliseconds and you need to complete all of your work within this timing.

You will need to rewrite your code to be called multiple times instead of looping.

How would you do such thing?

int timer;

public void teleopInit() {
    timer = 0;
}

public void teleopPeriodic() {
    timer++;
    if  (timer < 300) {
        // do something
    } else {
        // turn off "something"
    }
    if (/* joystick get button pressed*/) {
        timer = 0;
    }
}

wouldn’t this code start the timer as soons as the teleoperation began. I want the timer to start when I press a button and continue for a certain amount of time even tough the button is not pressed.

I would highly recommend the command-based framework. Put this code in a Command and bind it to a button on the controller. Look over the WPILib the docs and examples.

Set timer to 300 in teleopInit then

And yes, command based would work better, but I’ve never learned to use it so I don’t throw it around

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.