Using Serial imput to run a code

OK, got it.

You can split your mandmcolor into two distinct steps; the first one does the sorting and the second one does the waiting. This is can be done using a so-called finite state machine; you actually already did implement one using if statements for your start/restart/pause/terminate.

First define the steps and add a static variable to remember those steps.

#define MMC_SORT 0
#define MMC_WAIT 1

//==================================================================
void mandmColor() {
  static byte currentState = MMC_SORT;

  ...
  ...
}

The static variable is like a global variable, but it's only known inside the function so you can not change it inside other functions. It's initialised with MMC_SORT so when mamdmcolor is called the first time it will do the sorting part.

Now you can setup the framework for the two steps

  switch (currentState)
  {
    case MMC_SORT:
      do some work
      // done, go to WAIT step (state)
      currentState = MMC_WAIT;
      break;
    case MMC_WAIT:
      wait for some time
      // done, go to SORT step (state)
      currentState = MMC_SORT;
      break;
  }

The above uses a switch statement; you can use an if statement as well.

Before you switch from the SORT state to the WAIT state, you will have to set the startTime.

#define MMC_SORT 0
#define MMC_WAIT 1

//==================================================================
void mandmColor() {
  static byte currentState = MMC_SORT;

  switch (currentState)
  {
    case MMC_SORT:
      Serial.print(" R:  ");
      Serial.print(red, DEC);
      Serial.print("   | G:  ");
      Serial.print(green, DEC);
      Serial.print("   | B:  ");
      Serial.print(blue, DEC);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("R:");
      lcd.print(red, DEC);
      lcd.print("  G:");
      lcd.print(green, DEC);
      lcd.print("  B:");
      lcd.print(blue, DEC);
      lcd.setCursor(0, 1);

      if (red >= 17 && red <= 23 && green >= 23 && green <= 28 && blue >= 23 && blue <= 28)  {
        Serial.print("  - (Yellow)");
        lcd.print(" - (Yellow)");
        bottomservo.write(30);
        mandmcount = mandmcount + 1;
      }

      else if (red >= 23 && red <= 30 && green >= 38 && green <= 49 && blue >= 26 && blue <= 34)  {
        Serial.print("  - (Red)");
        lcd.print(" - (Red)");
        bottomservo.write(60);
        mandmcount = mandmcount + 1;
      }

      else if (red >= 30 && red <= 40 && green >= 27 && green <= 39 && blue >= 25 && blue <= 31)  {
        Serial.print("  - (Green)");
        lcd.print(" - (Green)");
        bottomservo.write(90);
        mandmcount = mandmcount + 1;
      }

      else if (red >= 38 && red <= 45 && green >= 32 && green <= 42 && blue >= 17 && blue <= 26)  {
        Serial.print("  - (Blue)");
        lcd.print(" - (Blue)");
        bottomservo.write(120);
        mandmcount = mandmcount + 1;
      }

      else if (red >= 18 && red <= 22 && green >= 36 && green <= 47 && blue >= 28 && blue <= 34)  {
        Serial.print("  - (Orange)");
        lcd.print(" - (Orange)");
        bottomservo.write(150);
        mandmcount = mandmcount + 1;
      }

      else if (red >= 31 && red <= 39 && green >= 40 && green <= 48 && blue >= 28 && blue <= 35)  {
        Serial.print("  - (Brown)");
        lcd.print(" - (Brown)");
        bottomservo.write(180);
        mandmcount = mandmcount + 1;
      }

      else {
        Serial.print("  - (No M&M)");
        lcd.print(" - (No M&M)");
        //bottomservo.write(0);
      }
      lcd.setCursor(12, 1);
      lcd.print("#");
      lcd.print(mandmcount);
      Serial.print("  - #");
      Serial.println(mandmcount);

      // set up the start time for the wait period
      startMillis = millis();
      // go to next state
      currentState = MMC_WAIT;
      break;
    case MMC_WAIT:
      // get the current time
      currentMillis = millis();
      // if wait period is over
      if (currentMillis - startMillis >= period) {
        // go to next state
        currentState = MMC_SORT;
      }
      break;
  }
}

Note that there is no while. There is hardly ever a need for a while-loop or for-loop in Arduino code; it just requires a little bit of different thinking :wink:

Shout if you don't understand.