Switch case condition in Arduino programming??

What is a switch case condition in Programming, how switch case condition works & how to use a switch case condition are some questions that may arise in Arduino programming.

Hello guys! Welcome back to my blog today we are going to see about switch case conditions in the program so let’s begin.

What is the switch case condition??


switch statement Arduino

Switch cases condition is a replacement for long long lines of code of if else condition.

The lengthy lines used by the if else condition in the program is eliminated by the switch case condition to look the code short and easy to understand. The switch statement compares the value of the variable which is specified in a case statement.

switch case Condition syntax
Arduino switch case example

We have initialized the value of x=3

In the first case, it compares the value of x. In the first case, the condition has a value of 1 it is not equal to 3 so it will jump to the next case statement.

In the second case, it compares the value of x again. In the second case the condition has a value of 2 it is not equal to 3 so it will jump to the next case statement. It will compare the case condition until the condition is satisfied, in our case x=3 once the condition is satisfied the loops get broken. If none, if the condition is satisfy the default condition, will execute, in our case value of x is not defined.

In the third case, it will compare the value of x with the condition where the value of x and case 3 conditions are satisfied so the loop will break.

Example of switch case with user input led turn ON

Arduino switch case example code

Controlling LED with User input & Switch case condition

switch case LED blinking

Connect the negative terminal of all LEDs to the GND pin of the Arduino
Connect positive pin of LED 1 to PIN no 2 of Arduino
Connect positive pin of LED 2 to PIN no 3 of Arduino
Connect positive pin of LED 3 to PIN no 4 of Arduino
Connect positive pin of LED 4 to PIN no 5 of Arduino
Connect the positive pin of LED 5 to PIN no 6 of Arduino
Connect positive pin of LED 6 to PIN no 7 of Arduino
Connect positive pin of LED 7 to PIN no 8 of Arduino
Connect positive pin of LED 8 to PIN no 9 of Arduino
Connect positive pin of LED 9 to PIN no 10 of Arduino
Connect the positive pin of LED 10 to PIN no 11 of Arduino

Code - 

int ledpins[] = {2,3,4,5,6,7,8,9,10,11};
int total_ledpin = 10;
String message ="Which LED do you want to Turn ON";
int answer;

void setup() 
 {
   Serial.begin(115200);
   for (int i=0;i<=total_ledpin;i++)
 {
  pinMode(ledpins[i],OUTPUT);
  }
}


void loop() 
{
  
Serial.println(message);
while(Serial.available()==0)
{
  
}
answer=Serial.parseInt();


switch (answer) {
  case 2:
    digitalWrite(2,HIGH);
    break;
  case 3:
     digitalWrite(3,HIGH);
    break;
     case 4:
     digitalWrite(4,HIGH);
    break;
     case 5:
     digitalWrite(5,HIGH);
    break;
     case 6:
     digitalWrite(6,HIGH);
    break;
     case 7:
     digitalWrite(7,HIGH);
    break;
     case 8:
     digitalWrite(8,HIGH);
    break;
     case 9:
     digitalWrite(9,HIGH);
    break;
     case 10:
     digitalWrite(10,HIGH);
    break;
     case 11:
     digitalWrite(11,HIGH);
    break;
    
  default:
    break;
}
     
}

First, we have created an array that consists of pins of Arduino pins from pin no 1 to 10. On each pin, we have connected a led. So in total, we have 10 LEDs. Here we have created an array and assigned a name of the array as ledpins. Here we are using a total of 10 pins of Arduino to connect 10 LEDs. We are connecting LED to Arduino pin numbers 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. We have declared the total LED pin count as 10 as in this example we are using 10 LED. We have initialized the variable name as the answer to store the number entered by the user in the serial monitor. We have also created the message ”Which LED do you want to Turn ON" and stored it in a variable that will be displayed on the serial monitor for users.

In the void setup, we have configured the baud rate to 115200. We configure pins as INPUT or OUTPUT. We have to declare the LED pin as output. Here we have 10 LEDs so instead of declaring each 10 LED pin as output. We will use for loop to declare LED pins. We will discuss for loop in detail. In for loop, we initialize the value of i as 0 first and later increment till the value of i reaches the total length of LED which we have declared as 10 (i = 0 to i =10). 

In the void loop, we have first displayed the message through the variable "Which LED do you want to Turn ON". We have used a while loop where we have to use Serial.available() to know whether the user has entered the value or not as the serial monitor. If the user has not entered the value then Serial.available() will be 0. The while loop continues until the user enters the value. Once the user has entered the led pin in the serial monitor then while loops breaks and by using Serial.parseInt() function we store the value in variable name answer. We have a switch condition that will compare the value stored in the variable and the switch case condition. If the user enters 2 in the serial monitor then it will compare the value and case value if it matches then the condition corresponding to that case condition will be executed, in our case LED pin will be turned ON. if the user enters 5 in the serial monitor then it will compare the value and case value having 5 and turn ON LED pin 5 using the digitalWrite() function.

Restrictions while using switch cases are

 1)  Not allowed to use duplicate cases

int x=1
Switch (x)
{
case 1: print (“value of X is 1”)
    break;
case 1: print (“value of X is 1”)
    break;
case 2: print (“value of X is 2”)
    break;
}

      2)  Don’t use float values such as 1.5, 2.0, or 3.5 in switch variable and in case label

     3) Variable expressions are not allowed in the case of labels although macros are allowed

 

Conclusion –

Today we learn about Switch case condition in Arduino, where to use Switch case condition, how to use Switch case condition, and what Switch case condition does in Arduino. we have seen the Switch case condition example of LED controlling through user input.

"I hope you find this IOT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu