The optical sensor did not read any values

This is my code. When I run it, the motor keeps spinning, but the sensor does not seem to read any values or might only read once. Additionally, it prevents my other code from running properly, as if it occupies the execution space of other code. How can I fix this issue?

void colorBlue(double speed) {
  bool a=true;
  while(a){
      Optical4.gestureDisable(); 
      Optical4.color();
        if (Optical4.color() == blue) {
            Brain.Screen.clearScreen();
            Brain.Screen.print("Blue");
            M5.spin(directionType::fwd, speed, pct);
            M6.spin(directionType::fwd, speed, pct);
            a=false;
        } else if (Optical4.color() == red) {
            Brain.Screen.clearScreen();
            Brain.Screen.print("Red");
            wait(140,msec);
            M5.stop(brake);
            M6.stop(brake);
            wait(220,msec);
        } else {
            M5.spin(directionType::fwd, speed, pct);
            M6.spin(directionType::fwd, speed, pct);
        }
    }
  }

The color β€œred” is very dependant on lighting and other factors. Best to stop the motor when the hue() you are detecting is within a certain range. Use the V5 devices screen to determine what the optical sensor is detecting for red and blue game objects.

2 Likes