bool usingInches;
double minimumDistance;
void getInput(bool & inches, double & distance) {
String answer;
bool waitingForAnswer = true;
do {
Serial.println(F("Which one do you want-- centimeters or inches? \nI only need the first letter of what you choose."));
while (Serial.available() == 0) ; // wait for something to be read
answer = Serial.readStringUntil('\n'); // read what's there (or timeout)
answer.trim(); // get rid of the spaces
if (toupper(answer[0]) == 'I') {
Serial.println("You selected inches!");
inches = true;
waitingForAnswer = false;
} else if (toupper(answer[0]) == 'C') {
Serial.println("You selected centimeters!");
inches = false;
waitingForAnswer = false;
} else {}
} while (waitingForAnswer);
Serial.println(F("With that out of the way, please input how far the robot should be from a wall before it freaks out."));
waitingForAnswer = true;
char *endPtr = nullptr;
do {
Serial.println(F("Input any number here (decimals are welcome!): "));
while (Serial.available() == 0) ; // wait for something to be read
answer = Serial.readStringUntil('\n'); // read what's there (or timeout)
answer.trim(); // get rid of the spaces
distance = strtod (answer.c_str(), &endPtr);
} while (endPtr == answer.c_str());
Serial.print(F("distance = ")); Serial.println(distance, 6);
Serial.println(F("Ok, You're all set! "));
}
void setup() {
Serial.begin(115200);
getInput(usingInches, minimumDistance);
}
void loop() {}