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);
}
}
}
}}
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.
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
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'.
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;
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
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 ) it is valid to have cases inside an if() instead of more conventional other way around.