void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Input 1 and press Enter:");
while (Serial.available() == 0) {} // Wait for data available
String teststr = Serial.readString(); // Read until timeout
teststr.trim(); // Remove any \r \n whitespace at the end of the String
if (teststr == "1")
{
Serial.println("Input any character to continue:");
while (Serial.available() == 0) {} // Wait for the user to input any character
char inputChar = Serial.read(); // Read a single character
//Serial.readString(); // Clear remaining characters in the buffer
if (inputChar)
{ // Check if a character was entered
String teststr2;
bool validColor = false;
do //process eather ways
{
Serial.println("Select the color of the part (Red, Green, Blue, Orange):");
Serial.println(Serial.readString());
while (Serial.available() == 0) {} // Wait for color input
teststr2 = Serial.readString();
teststr2.trim(); // Remove extra spaces or newline characters
// Convert input to lowercase for case-insensitivity
teststr2.toLowerCase();
if (teststr2 == "red")
{
Serial.println("The red part has been picked and placed in the red bin.\n");
validColor = true;
}
else if (teststr2 == "green")
{
Serial.println("The green part has been picked and placed in the green bin.\n");
validColor = true;
}
else if (teststr2 == "blue")
{
Serial.println("The blue part has been picked and placed in the blue bin.\n");
validColor = true;
}
else if (teststr2 == "orange")
{
Serial.println("The orange part has been picked and placed in the orange bin.\n");
validColor = true;
}
else
{
Serial.println("Error: Invalid color. Please try again.\n");
}
} while (!validColor); // Repeat until a valid color is entered
}
}
else
{
Serial.println("Invalid input. Try again.\n");
}
}