0

How can I program in Arduino Uno a serial.available that will detect some chars that aren't supposed to belong in a list of options, like this:

void lerRespostaUtilizador(){
  if(Serial.available('s' && 'v' && 'r'){
    Serial.println("Podes visualizar: (s)inais de controlo, (v)ariaveis do sistema e conteudo de (r)egistos.")
  }
  switch(Serial.read()){
    case 's': // Mostrar sinais de controlo
      mostrarSinais();
      Serial.println("Podes visualizar ainda: (v)ariaveis do sistema e conteudo de (r)egistos.");
      Serial.println("");
      break;
    case 'v': // Mostrar variáveis do sistema
      mostrarVariaveis();
      Serial.println("Podes visualizar ainda: (s)inais de controlo e conteudo de (r)egistos.");
      Serial.println("");
      break;
    case 'r': // Mostrar conteúdo de registos
      mostrarRegistos();
      Serial.println("Podes visualizar ainda: (s)inais de controlo e (v)ariaveis do sistema.");
      Serial.println("");
      break;
    }
}

What I want is if the user inserts some chars that aren't 's', 'v' or 'r', a message will appear saying what they can insert to obtain certain results.

3
  • 1
    Check this Serial.available('s' && 'v' && 'r'). I can guess what you think it means but it does not. Commented May 4, 2017 at 14:36
  • How can I correct it then? Commented May 4, 2017 at 14:37
  • Simply wait for something to read, read the character, and add a default switch section case that catches illegal characters. Commented May 4, 2017 at 14:38

1 Answer 1

1

I would rewrite that as something like this:

void lerRespostaUtilizador(){
  while (!Serial.available());
  switch(Serial.read()){
    case 's': // Mostrar sinais de controlo
      mostrarSinais();
      break;
    case 'v': // Mostrar variáveis do sistema
      mostrarVariaveis();
      break;
    case 'r': // Mostrar conteúdo de registos
      mostrarRegistos();
      break;
    default:
      Serial.println(F("Podes visualizar: (s)inais de controlo, (v)ariaveis do sistema e conteudo de (r)egistos."));
    }
}
1
  • Yeap, that works perfectly! But can you explain me how the while works and how is it connected to the default option? Commented May 4, 2017 at 14:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.