using if inside case

Hi, I want to use if inside a case but this is not working, kindly have a look on code below in case d I have added if but this is not working

/*
  Switch statement  with serial input
 
 Demonstrates the use of a switch statement.  The switch
 statement allows you to choose from among a set of discrete values
 of a variable.  It's like a series of if statements.
 
 To see this sketch in action, open the Serial monitor and send any character.
 The characters a, b, c, d, and e, will turn on LEDs.  Any other character will turn
 the LEDs off.
 
 The circuit:
 * 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
 
 created 1 Jul 2009
 by Tom Igoe
 
This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/SwitchCase2
 */
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pins:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}
 
void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    // do something different depending on the character received.
    // The switch statement expects single number values for each case;
    // in this exmaple, though, you're using single quotes to tell
    // the controller to get the ASCII value for the character.  For
    // example 'a' = 97, 'b' = 98, and so forth:
 
    switch (inByte) {
      case 'a':
        digitalWrite(2, HIGH);
        break;
      case 'b':
        digitalWrite(3, HIGH);
        break;
      case 'c':
        digitalWrite(4, HIGH);
        break;
      case 'd':
        digitalWrite(5, HIGH);

    if (inByte == "1") {digitalWrite(5, LOW);

         
        break;
      case 'e':
        digitalWrite(6, HIGH);
        break;
      default:
        // turn all the LEDs off:
        for (int thisPin = 2; thisPin < 7; thisPin++) {
          digitalWrite(thisPin, LOW);
        }
    }
  }
}}

I am a little bit surprised it even compiles.

But I reformatted it a bit, spot the error now?

      case 'd':
        digitalWrite(5, HIGH);
        if (inByte == "1") {
          digitalWrite(5, LOW);
         
        break;
      case 'e':
        digitalWrite(6, HIGH);
        break;
      default:
        // turn all the LEDs off:
        for (int thisPin = 2; thisPin < 7; thisPin++) {
          digitalWrite(thisPin, LOW);
        }
    }
  }
}}

BUT, that is only the minor error. How can inByte be both 'd' AND '1' at the same time? Not to even mention you try to compare it to a string....

sir thanks for your quick responds, i am very new to arduino coding, kindly suggest me how i can achieve the functionalists which i am looking for, if you provide a hint i will try to write code

septillion provided a hint in reply #1. It's a hint though, and not an outright answer.

By 'reformatting' septillion means to use the Arduino IDE Tools menu > Auto Format command so you can see where the logic of the code no longer follows the indentation. This is usually because a brace was opened and not closed.

The above is an even more obvious hint.

You want to turn off pin 5 when you send a '1'?

If so, do you understand the working of the rest? Woudn't that simply mean a case for '1' (NOT "1") as well?

septillion:
You want to turn off pin 5 when you send a '1'?

If so, do you understand the working of the rest? Woudn't that simply mean a case for '1' (NOT "1") as well?

yes, i just want to work if condition in case d but not achieve the results, not understanding what is wrong here :confused:

Please be assured that an "if" will work inside a "case", but your conditional block includes the next "case".

Your { after the "if" needs a closing }

sir i have to submit my school project please help with me correct code :sob:

These fine people are helping you. They will not do your homework for you.

But WHY make it an if inside the case?

If the case 'd' is executed, what will 'inByte' be? So will 'inByte' ever be equal to '1' there?

septillion:
But WHY make it an if inside the case?

If the case 'd' is executed, what will 'inByte' be? So will 'inByte' ever be equal to '1' there?

I want to create sub menu for case 'd', let me explain you with different example, just forgot previous case 'd' now updated case 'd' is below , i want to run a servo motor after sending '1' .

required program is :

when i send 'a' case 'a' will execute

when i send 'b' case 'a' will execute

and when i send 'd' after that only '1' should work to move servo

case 'd':
        
        if (inByte == "1") {
          servo.write(90)}

How are you getting a new value for inByte once you are in case 'd'?
Why are you checking for "1" and not '1'?

evanmars:
How are you getting a new value for inByte once you are in case 'd'?
Why are you checking for "1" and not '1'?

I am getting a new value inByte via serial monitor and i also checked for '1' b but nothing worked for me

abhixs:
I am getting a new value inByte via serial monitor and i also checked for '1' b but nothing worked for me

Think about it. If you get a '1' then the code won't run for case 'd' will it ?

abhixs:
I am getting a new value inByte via serial monitor

No you're not.

For your code execution path to get to case 'd', inByte must be 'd'. Then you execute a digitalWrite and then you check to see whether inByte is "1". Should be checking for '1', but putting that aside, no code has been executed that will change inByte so it is still 'd'.

Yep, code execution is sequential.

So if you want only something o happen is you receive a '1' after a 'd' you have to make it remember you send it a 'd' before.

To get a good base for that see Serial Input Basics - updated.

void loop() {
 //here is where you get your input
  if (Serial.available() > 0) {
    int inByte = Serial.read();   // 'd' has been entered

 
    switch (inByte) {   // using 'd'

      // cases a-c

      case 'd':        
        digitalWrite(5, HIGH);

   // you never do a Serial.read() again, so inByte has not changed from 'd'
    if (inByte == "1") {digitalWrite(5, LOW);

         
        break;

septillion:
Yep, code execution is sequential.

So if you want only something o happen is you receive a '1' after a 'd' you have to make it remember you send it a 'd' before.

To get a good base for that see Serial Input Basics - updated.

I am trying sir, I will let you know soon, I am very new to arduino so getting confused at many point but I will learning day by day, thanks you all for your support :slight_smile:

septillion:
I am a little bit surprised it even compiles.

Why wouldn't it? The whole body of a switch is just one continuous statement with case labels peppered all over. You can write anything inside that statement.

Montmorency:
Why wouldn't it? The whole body of a switch is just one continuous statement with case labels peppered all over.

Because it isn't anymore. The 'e' case is now inside a "conditional jump". But I guess (yes, I have to because no Arduino on hand :slight_smile: ) it is valid to have cases inside an if() instead of more conventional other way around.