Evaluate test cases

I am using Arduino Uno board. I wanted to evaluate test cases Serially.
It should keep executing case 1 unless user change input to other test case. Can someone guide me here. what changes can be done.

int incomingByte = 0; // for incoming serial data

void setup() {
  pinMode(13,OUTPUT);
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }

switch(incomingByte)
{
  case 1: digitalWrite(13,HIGH); Serial.println(HIGH);break;
  case 2: digitalWrite(13,LOW); Serial.println(LOW);break;
  case 3: digitalWrite(13,HIGH); Serial.println(HIGH);delay(1000);
          digitalWrite(13,LOW); Serial.println(LOW);delay(1000);
  default : break;

}

  
}

What’s the problem you’re seeing ?

What do you see in the Serial monitor ?

I suspect that it is something like

I received: 49
I received: 50
I received: 51

when you enter 1,2 and 3, each followed by pressing Return

If you see

I received: 49
I received: 13
I received: 10
I received: 50
I received: 13
I received: 10
I received: 51
I received: 13
I received: 10

Then the 10s and 13s are the Line ending added by the Serial monitor which you set by changing the Line Ending setting of the Serial monitor

Now, what about the numbers 59, 50 and 51 ?
These are the ASCII values of the numbers you entered

You can fix this by either changing the values in the case statements to match or by subtracting 48 from the value of incomingByte before printing it or using it in switch/case

char incomingByte = 0; // for incoming serial data
char myCommand;

void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop()
{
  // send data only when you receive data:
  if (Serial.available() > 0)
  {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte);

    if (incomingByte >= 'a' && incomingByte < 'd')
    {
      myCommand = incomingByte;
    }
  }

  switch (myCommand)
  {
    case 'a':
      digitalWrite(13, HIGH);
      Serial.println(HIGH);
      break;

    case 'b':
      digitalWrite(13, LOW);
      Serial.println(LOW);
      break;

    case 'c':
      digitalWrite(13, HIGH);
      Serial.println(HIGH);
      delay(1000);
      digitalWrite(13, LOW);
      Serial.println(LOW);
      delay(1000);

    default :
      break;
      
  } //END of  switch...case

} //end of    loop()

i want to set flag which is based on receive byte. So it keep executing test cases.

I am receiving data 49/50/51 as when run alone.

Its like this if send 1, I would read 49. 49 should keep driving test case 49. until the 2 send as command.

Have you read the replies to this topic ?

They explain why you receive 49, 50 and 51 and how to fix it

I could able to change easily one Switch case based on input. Thanks for the input.
In a similar way, can I do it for Serial?

I have attached an image for reference. I am getting character printed like this when goes inside second loop

Currently, I am switching Upper switch cases and could able to print particular cases.
I have another switch case in the test case:
Q: used to quit
C: continue the test
XYZ used to evaluate other cases as mentioned.

char incomingByte = 0; // for incoming serial data
char Switch_byte=0;
char Switch_command;
char myCommand;
unsigned int Main_flag=0;
unsigned int Sub_flag=0;


void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop()
{
  // send data only when you receive data:
  if(Main_flag==0)
  {
  if (Serial.available() > 0)
  {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte);

    if (incomingByte >= 'a' && incomingByte < 'd')
    {
      myCommand = incomingByte;
      Main_flag=1;Sub_flag=0;
    }
  }
  }

  switch (myCommand)
  {
    case 'a':
      Serial.print("Testcase1: \n");
      Serial.print(1);
      Serial.print("value: \n");
      Serial.println(1);
      break;

    case 'b': 
    Serial.print("Testcase-b Executing \n");

    if(Sub_flag==0)
  {
  if (Serial.available() > 0)
  {
    // read the incoming byte:
    Switch_byte = Serial.read();

    // say what you got:
    Serial.print("I received sub function: ");
    Serial.println(Switch_byte);

    if (Switch_byte >= 1 && Switch_byte < 3)
    {
      Switch_command = Switch_byte;Sub_flag=0;Main_flag=1;
      
    }
    if  (Switch_byte >= 'q' )
    {
      Sub_flag=1;Main_flag=0;
    }

  }
  } 
    
    Serial.print("Switch_command: ");
    Serial.println(Switch_command);
  
    switch(Switch_command)
    {
      case 1:Serial.println("Case X \n");break;
      case 2:Serial.println("Case y \n");break;
      case 3:Serial.println("Case z \n");break;
    }

      break;

    case 'c':
      
      

    default :
      break;
      
  } //END of  switch...case
delay(1000);
} 
![image|690x388](upload://sZHR7j6Uo0e9MA1Z3exQa14BAWo.jpeg)

Hello

I'm just repeating what others have said but didn't explain

When you send 1 in serial monitor, it's not sent as decimal number 1 but as ASCII character '1' (which is 49 in decimal)

So you have to change case 1 to case '1' etc