Using Serial.read() several times

Hi everyone.

I've been studying different codes in order to be able to read multiple times using Serial.read(), but I couldn't make it.
This is an example of a code I'm using:

int option;

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

   //MENU
  Serial.print("- MENU -\n-------\n\n");
  Serial.print("1: Encontrar/Actualizar dispositivos\n");
  Serial.print("2: Seleccionar dispositivo\n");
  Serial.print("3: Encender dispositivo\n");
  Serial.print("4: Apagar dispositivo\n");
  Serial.print("5: Encendido programado\n");
  Serial.print("6: Encendido temporizador\n");
  Serial.print("Introduzca opcion: \n");
}

void loop(){
   if(Serial.available()){
      delay(1);
      option = Serial.read() -  48;
      Serial.print(option);

      switch (opcion) {
        case 1:
          Serial.print("\n\n-NODE DISCOVERY-\n\n");
          valvula.NodeDiscovery();
          break;
  
        case 2:
          Serial.print("\nSELECCIONAR DISPOSITIVO\n");
          break;
  
        case 3:
          Serial.print("\nENCENDER\n");
          valvula.encender(1);
          break;
  
        case 4:
          Serial.print("\nAPAGAR\n");
          valvula.apagar(1);
          break;
  
        case 5:
          int i;
          Serial.print("\nENCENDIDO PROGRAMADO\n");
          Serial.print("Introducir hora de encendido (hh): ");
          //cadHora = Serial.read();
  
          for(int i=0; cadHora[i]!='\0'; i++) hora[i]=cadHora[i];
          hora[i]='\0';
          horaOn=atoi(hora);
        
          Serial.print("Introduce hora de apagado (hh): ");
          //cadHora = Serial.read();
        
          for(int i=0; cadHora[i]!='\0'; i++) hora[i]=cadHora[i];
          hora[i]='\0';
          horaOff=atoi(hora);
          break;
  
        case 6:
          int minutos, seg;
          Serial.print("\nENECENDIDO TEMPORIZADOR\n");
          Serial.print("Introducir tiempo de encendido (minutos):");
          if(Serial.available()){
            delay(1);
            minutos = Serial.readt() - '0';
            seg = minutos*60;
            Serial.print(seg);
          }
          valvula.encender(1);
          cont1=0;
  
          if(cont1 == seg){
            valvula.apagar(1);
          }
          break;
      }
   }
}

As you see, I choose an option in the 'Menu', I read it at the beginning of the loop and I enter the 'switch'. If I choose option 5 or 6, I would like to introduce more data using the Serial monitor to work inside the switch.
It is possible to do?
Thanks in advance.

Maybe check out the SerialMenu library:

Or the Serial Input Basics.

Rather than put a lot of your code inside the if(Serial.available()){ block just use that to read and save a value - for example

  if(Serial.available()){
      option = Serial.read() -  48;
   }
   Serial.print(option);
   switch (option) {

I also think you need to reconsider the structure of your program. At the moment the assumption is that a value from Serial is a selection in the SWITCH / CASE. But IMHO that should not be the assumption. Your program needs to operate in different states. In one state it will be treating the Serial value as a selection but in another state it will use the value for some other purpose. When that purpose is finished the program should probably go back to the state for expecting a SWITCH / CASE value.

Think of it like this pseudo code

void loop() {
   if (Serial.available() ) {
       value = Serial.read();
   }
   if (programState == 'S') { // S for SWITCH
      switchFunction();
   }
   else if programState == 'T' ) {  // T for update time
       timeFunction();
   }

and the switchFunction() would be like this

void switchFunction() {      
        switch (value) {
        case 1:
          Serial.print("\n\n-NODE DISCOVERY-\n\n");
          valvula.NodeDiscovery();
          break;

       case 6:
         programState = 'T';
         break;
}

and

void timeFunction() {
           int minutos, seg;
           Serial.print("\nENECENDIDO TEMPORIZADOR\n");
            minutos = value;
            seg = minutos*60;
            Serial.print(seg);
          } 
          // etc
         programState = 'S'; // back to the SWITCH state
}

Even with this you may find yourself running into problems because the data is not received at the point in the program where it is wanted. Code for effective user input can get very complicated - what happens if the user makes a mistake, for example.

…R

      option = Serial.read() -  48;
      Serial.print(option);

      switch (opcion) {

You seem to be switching language in the middle of your program. The variable names "option" and "opcion" are not the same thing as far s the compiler is concerned.

johnwasser:
Think of it like this pseudo code

void loop() {

if (Serial.available() ) {
      value = Serial.read();
  }
  if (programState == 'S') { // S for SWITCH
     switchFunction();
  }
  else if programState == 'T' ) {  // T for update time
      timeFunction();
  }



and the switchFunction() would be like this 


void switchFunction() {      
       switch (value) {
       case 1:
         Serial.print("\n\n-NODE DISCOVERY-\n\n");
         valvula.NodeDiscovery();
         break;

case 6:
        programState = 'T';
        break;
}



and 


void timeFunction() {
          int minutos, seg;
          Serial.print("\nENECENDIDO TEMPORIZADOR\n");
           minutos = value;
           seg = minutos*60;
           Serial.print(seg);
         }
         // etc
        programState = 'S'; // back to the SWITCH state
}

Thank you very much for the answer, I will try to order the code and take into account everything you have commented. It is the first time that I use Arduino after using C++, which was easier for me to program.
Anyway, thanks for the comments

It is the first time that I use Arduino after using C++, which was easier for me to program.

I don’t quite know how to break this to you…

johnwasser:
You seem to be switching language in the middle of your program. The variable names "option" and "opcion" are not the same thing as far s the compiler is concerned.

Sorry John, it was a typo when writing in English here in the forum.

TheMemberFormerlyKnownAsAWOL:
I don't quite know how to break this to you...

I'm sure you do :wink: