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();
}
}
}
1 Answer 1
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'
}
}
}
-
Good work around for the menu! But how would you suggest getting from the functions back to the menu switch?ROff– ROff2017年12月05日 21:05:55 +00:00Commented Dec 5, 2017 at 21:05
-
@ROff: This is already handled here: when
srvConfig()
returns, webreak
out of the switch, then we get into another iteration of the infinitefor (;;)
loop, then back toswitch (Serial.read())
. The infinite loop is only broken when the user types '3'.Edgar Bonet– Edgar Bonet2017年12月05日 21:10:57 +00:00Commented 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 againROff– ROff2017年12月05日 21:18:41 +00:00Commented Dec 5, 2017 at 21:18
-
@ROff: In
srvConfig()
, replacex == 1
withx = 1
, or more simply withreturn
(and then get rid ofx
)..Edgar Bonet– Edgar Bonet2017年12月05日 21:57:56 +00:00Commented Dec 5, 2017 at 21:57