0% found this document useful (0 votes)
158 views3 pages

Switch Case 21

This document describes an Arduino project that uses a switch case statement to control six LEDs connected to pins 2 through 7 based on input from the serial monitor. When a character from 'a' to 'f' is entered, the corresponding LED is turned on by using switch case statements. The code defines pin modes, reads the serial input, and uses switch case to light the correct LED based on the character. This demonstrates how switch case can replace chained if/else statements to selectively execute code based on an expression's value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views3 pages

Switch Case 21

This document describes an Arduino project that uses a switch case statement to control six LEDs connected to pins 2 through 7 based on input from the serial monitor. When a character from 'a' to 'f' is entered, the corresponding LED is turned on by using switch case statements. The code defines pin modes, reads the serial input, and uses switch case to light the correct LED based on the character. This demonstrates how switch case can replace chained if/else statements to selectively execute code based on an expression's value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MARWADI UNIVERSITY

DEPARTMENT OF INFORMATION AND


COMMUNICATION TECHNOLOGY
Foundation Skills in Sensor Interfacing (01CT1103)

Name: Rajnikant hirapara Roll Number: 92200133013

Subject Name and Code: Foundation Date of Experiment: 19-12-22


Skills in Sensor Interfacing (01CT1103)

Task: Use of switch case statement to read the values

Components: Arduino, 6 LEDs, breadboard, jumper wires, 6resistors

About the Project:

To show the application of switch case in Arduino programming for the following example.
Assume that six LEDs have been connected to digital input/output pins of arduino board
from 2 to 7. Use for switch case statement to read the values from serial monitor from "A
to F" and turn on the respective LED from pins 2 to 7

Output: (your circuit implementation and its working photo)

Serial monitor: “d"

Code:
int n=0;
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION AND
COMMUNICATION TECHNOLOGY
Foundation Skills in Sensor Interfacing (01CT1103)
void setup()
{
Serial.begin(9600);

pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop()
{
if (Serial.available()>0)
{
n=Serial.read();
switch(n)
{ case'a' : digitalWrite(1,HIGH);
delay(1000);
digitalWrite(1, LOW);
delay(1000);
break;

case'b' : digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,HIGH);
delay(1000);
break;

case'c' : digitalWrite(3,HIGH);
delay(1000);
digitalWrite(3,HIGH);
delay(1000);
break;

case'd' : digitalWrite(4,HIGH);
delay(1000);
digitalWrite(4,HIGH);
delay(1000);
break;
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION AND
COMMUNICATION TECHNOLOGY
Foundation Skills in Sensor Interfacing (01CT1103)

case'e' : digitalWrite(5,HIGH);
delay(1000);
digitalWrite(5,HIGH);
delay(1000);
break;

case'f' : digitalWrite(6,HIGH);
delay(1000);
digitalWrite(6,HIGH);
delay(1000);
break;
}
}
}

Application: They save you from having to write tons of chained if else
statements. The Arduino switch statement takes a single expression. Multiple
Arduino case statements act on the expression. If the expression evaluates to
3 then an action 1 is executed.

Conclusion: The switch case controls the flow of the program by executing the
code in various cases. A switch statement compares a particular value of a
variable with statements in other cases

You might also like