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();
}