1

I'm trying to make a menu through serial monitor, but it always on a infinite loop... My question is where i call this function? On Setup or on Loop?

The menu:

  int controle_opera = 0;

  void menu() {


  int i = -1;
  do {
    char opcao = '\0';
    Serial.println("\nQual operação deseja realizar?");
    Serial.println("\n1 - Cadastro");
    Serial.println("\n2 - Validação");
    Serial.println("\n3 - Deletar");
    Serial.println("\n4 - Apagar database");
    if (Serial.available()) {opcao = Serial.read();}
    switch (opcao) {
      case '1': controle_opera++;
      i++;
      break;
      case '2': controle_opera += 2;
      i++;
      break;
      case '3': controle_opera += 3;
      i++;
      break;
      case '4': controle_opera += 4;
      i++;
      break;
      default: Serial.println("Opção desconhecida, tente novamente!");
              delay(100);
              break;
    }
  } while(i != 0);
}
5
  • 1
    You would run your menu from loop() - but not that menu since it is fundamentally flawed in many many ways. Commented Oct 21, 2019 at 11:42
  • Even when there're many others functions that need, for example, the value of "controle_opera"? There's any easy way to correct this menu? Commented Oct 21, 2019 at 11:46
  • 1
    Learn how to write a proper finite state machine. Commented Oct 21, 2019 at 11:48
  • @Mdsp take a look at this link for a correct format of switch case example. Commented Oct 21, 2019 at 11:57
  • github.com/neu-rah/ArduinoMenu works over serial Commented May 30, 2020 at 12:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.