problem using "switch/case" in a serial menu

hello!

I'm a newbie and english is not my language... so sorry if my message is not clear.

I'm trying to make a program that allows me to store data on a rfid tag with the serial monitor.
The objective is to store different character strings in different sectors on the tag. And then to be able to read them individually, of course

I managed to make a function that asks a data in the serial monitor, waits until you write a name in the serial monitor, then asks for the next data, etc... nothing fancy!

it works quite fine when this function is called from the loop.

---- Press ENTER when ready ----
Number : 123
Type : Card
Name : Bobby
State : OK
----- Data to write -----
123
Card
Bobby
OK
Press ENTER to write data on the RFID tag
Writing
Written 
------------------

Since I want to do several things with this program, I made a menu using "switch/case" to choose between several actions (read, write, etc...) depending on the serial input

It works OK as well. But when I call that function (case 2 in this code) from the "case" it does weird things.

choisisez ce que vous voulez faire :
 - Lire les données :................ Tapez 1
 - Encoder de nouvelles données :.... Tapez 2
 - Autre :........................... Tapez 3
---- Press ENTER when ready ----Number : 1Type : CName : BState : O----- Data to write -----1CBOPress ENTER to write data on the RFID tag

It doesn't wait for pressing "enter" before asking for data (which is not much a problem, though I hate not knowing why) it just record the 1st digit of the input and don't do the "carriage return" thing.

WHY????!!!!!!

So here is the code,in the loop comment/uncomment lines : dataInput is the function that works, mainMenu is the menu to access it.

#define SECTORS  6

String sectorName[SECTORS] = {
  "---- Press ENTER when ready ----",
  "Number : ",
  "Type : ",
  "Name : ",
  "State : ",
  "----- Data to write -----"
};

String  confirmWrite = "Press ENTER to write data on the RFID tag";


String serialInput;
String sectorData[10];
byte menuChoice ;

bool menu = 1;
bool dataRecorded = 0;
bool dataWritten = 0;


void setup() {
  
  Serial.begin(9600);

}


void loop() {
  
  //dataInput();
  mainMenu() ;
}


void mainMenu() {

  if (menu == 1 ) {
    Serial.println("choisisez ce que vous voulez faire :");
    Serial.println(" - Lire les données :................ Tapez 1");
    Serial.println(" - Encoder de nouvelles données :.... Tapez 2");
    Serial.println(" - Autre :........................... Tapez 3");
    menu = 0;
  }

  if (Serial.available()  ) {
    menuChoice = Serial.parseInt();
  }

  switch (menuChoice) {
    case 1:
      Serial.println("choix 1");
      break;

    case 2:
      Serial.println("choix 2");
      dataInput();
      break;

    case 3:
      Serial.println("choix 3");
      break;

    default:
      break;
  }


}


void dataInput() {

  if (dataRecorded == 0) {
    Serial.print(sectorName[0]);

    for (int i = 0; i < SECTORS - 1; i ) {

      while (Serial.available()) {

        serialInput = Serial.readString();
        sectorData[i] = serialInput;
        Serial.print (sectorData[i]);
        i++;
        Serial.print(sectorName[i]);

        if (i == SECTORS - 1) {

          for (int y = 0; y < SECTORS; y ++) {
            Serial.print (sectorData[y]);
          }

          dataRecorded = 1;
          Serial.println(confirmWrite);
        }
      }
    }
  }

  else {   //test part : will later call "RFIDWrite" function
    if (Serial.available()) {
      Serial.println("Writing");
      delay(1000);
      Serial.println("Written ");
      Serial.println("------------------");
      dataWritten = 1;
      dataRecorded = 0;
    }
  }
}

If anyone can help me with that, that would be so much appreciated !
And I would gladly take any advice to make this program more efficient,more practical, elegant or simple.

Thank you very much

You print the menu and then a few microseconds later you check to see if there's been answer. It's very unlikely that there will and so you proceed to use the previous value of menuChoice.
Get rid of " if (Serial.available() ) {" and the matching end brace. parseInt will wait for input (up to some timeout limit).

Pete

thanks for your answer !

I thought Serial.available was necessary but I never used parseInt before.

It works as well without it so I erased it.

That solves nothing though...

any other idea?

Try changing the parseInt to this:

  while((menuChoice = Serial.parseInt()) == 0);

This will wait until parseInt finds a non-zero integer. It also returns zero when it times out waiting for a response so this also just waits here while there's a timeout.

Pete

you mean replace :

  if (Serial.available()  ) {
    menuChoice = Serial.parseInt();
  }

by

while((menuChoice = Serial.parseInt()) == 0);

I did and it changes nothing...