1

I'm working on a project for a robot and I'm trying to make a menu that can run on start up where the user can calibrate the mid-point for the servos (so they're stationary) and the ldrs so it can tell between black lines and white paper. So, I've made the menu a switch case where it calls the calibration when user enters a value, the calibration routines are their own functions.

What I want to know how to do is how to make it so when the user is finished with a calibrator it will go back to the menu function and also how to get the menu to exit on user input. So far when the menu calls a function it will just loop on it.

The menu:

void Menu(){
 Serial.println("|****************************|");
 Serial.println("|**|Ardnoid Configuration |**|");
 Serial.println("|**| Menu |**|");
 Serial.println("|****************************|");
 Serial.println("");
 Serial.println("Select one of the following options:");
 Serial.println("1 LDR configuration");
 Serial.println("2 Servo calibration");
 Serial.println("3 Exit configuration menu");
 while(!Serial.available()){}
 int i = Serial.parseInt();
 switch(i){
 case 1:
 break;
 case 2:
 while(srvConOn == true){
 srvConfig();
 }
 break;strong text
 case 3:
 Serial.println("Leaving configuration menu"); 
 break;
 default:
 Serial.println("Unrecognized command, try again!");
 } 
 }

and the servo calibration:

void srvConfig(){
Serial.println("");
Serial.println("Begin servo stop calibration routine");
int x = 0;
 while(x == 0){
 Serial.println("Enter values for servos until both are stationary");
 delay(100);
 Serial.println("Enter value for left servo");
 while (!Serial.available()) {;}
 left = Serial.parseInt();
 Serial.println(left);
 delay(100);
 Serial.println("Enter value for right servo");
 while (!Serial.available()) {;}
 right = Serial.parseInt();
 Serial.println(right);
 delay(100);
 srvR.write(right);
 srvL.write(left);
 Serial.println("have both servos stopped?");
 Serial.println("1 = Yes | 2 = No"); 
 int j;
 while (!Serial.available()) {;}
 j = Serial.parseInt();
 if (j == 1) {
 Serial.println("Saving calibration");
 EEPROM.write(leftMid, left);
 EEPROM.write(rightMid, right);
 srvConOn == false; 
 x == 1;
 } else if (j == 2) {
 Serial.println("Try again");
 Serial.println();
 }
 }
}
sempaiscuba
1,0429 gold badges21 silver badges32 bronze badges
asked Dec 5, 2017 at 20:09

1 Answer 1

0

You could put your switch/case inside an infinite loop, and return from the Menu() function when the user presses '3'. E.g.:

void Menu() {
 Serial.println(menu_banner);
 for (;;) {
 switch (Serial.read()) {
 case '1': ldrConfig(); break;
 case '2': srvConfig(); break;
 case '3': return;
 default: continue; // includes the case 'no input'
 }
 }
}
answered Dec 5, 2017 at 20:53
4
  • Good work around for the menu! But how would you suggest getting from the functions back to the menu switch? Commented Dec 5, 2017 at 21:05
  • @ROff: This is already handled here: when srvConfig() returns, we break out of the switch, then we get into another iteration of the infinite for (;;) loop, then back to switch (Serial.read()). The infinite loop is only broken when the user types '3'. Commented Dec 5, 2017 at 21:10
  • I just ran it through and it seems like whenever I try to save the srvConfig instead of returning to the menu it will just run srvConfig again Commented Dec 5, 2017 at 21:18
  • @ROff: In srvConfig(), replace x == 1 with x = 1, or more simply with return (and then get rid of x).. Commented Dec 5, 2017 at 21:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.