Arduino-LED Programming (Switch-Case, If-Else, Loop, Function)
Arduino-LED Programming (Switch-Case, If-Else, Loop, Function)
(Switch-Case,If-else, loop,Function)
Arduino switch case
❑ 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. When the statements in a case matches
the value of a variable, the code associated with that
case executes.
❑ The break keyword is used at the end of each case. For
example, if there are five cases, the break statements will
also be five. The break statement exits the switch case.
❑ The switch statement without a break will continue to
execute all the cases until the end. Hence, it is essential
to include a break statement at the end of each case.
Let's understand with an example.
switch(variable)
{
case 1:
// statements related to case1
break;
case 2:
// statements related to case2
break;
.
.
case n:
// statements related to case n
break;
default:
break;
}
default:
break;
}
where,
variable: It includes the variables whose value will be
compared with the multiple cases
value: It consists of a value to compare. These values
are constants. The allowed data types
are int and char.
Consider the below flowchart:
EXP 1- Open the Serial monitor and send any
character. The characters a, b, c, d, e and f will
turn on LEDs. Any other character will turn the
LEDs off
Components Required:-
❑Arduino Board
❑6 X 220 ohm resistors
❑6 X LEDs
❑Jumpers
❑breadboard
Circuit
CODING
void setup()
{
Serial.begin(9600);
for (int thisPin = 2; thisPin < 8; thisPin++)
{
pinMode(thisPin, OUTPUT);
}
}
void loop()
{
if (Serial.available() > 0)
{
int inByte = Serial.read();
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);
break;
case 'e':
digitalWrite(6, HIGH);
break;
case ‘f':
digitalWrite(7, HIGH);
break;
default:
for (int thisPin = 2; thisPin < 8; thisPin++)
{
digitalWrite(thisPin, LOW);
}}}}
The Arduino while and do
while Loops
while Loop Structure
The while loop has a structure as follows:
while (loop test expression goes here)
{
Statements that run in the loop go here
Statement 1
Statement 2 ...
}
The while loop starts with the while keyword followed by a test expression
between opening and closing parentheses. Opening and closing braces
denote the body of the loop.
The do while Loop
The do while loop works in the same way as the while loop, except that
it always runs once even if the test expression evaluates to false.
do while Loop Structure
The do while loop consists of two keywords do and while, as shown
below.
do {
Statements that run in the loop go here
Statement 1
Statement 2 ...
}
while (test expression goes here);
The body of the do while loop falls between opening and closing
braces and contains statements that are to be run in the loop.
The while keyword and test expression come after the body of the
loop and are terminated by a semicolon (;).
Arduino While loop example 1
through 10
void setup () {
int i=0;
Serial.begin(9600);
Serial.println("Arduino do while loop");
do
{
i++;
Serial.println(i);
}
while(i<10);
}
void loop()
{}
Arduino - for Loop
❑ The statements inside the curly brackets under for loop
are executed repeatedly according to the specified
condition. An increment counter in the for loop is used to
increment or decrement the loop repetitions.
To overcome this, programmers prefer to use for loop to execute a task multiple
times, while using a single statement.
int i;
void setup ( )
Serial.begin(9600);
for ( i = 0 ; i < 15 ; i ++ )
Serial.println( "Arduino"); } }
void loop ( ) {
}
❑OUTPUT:-
Exp 1- Light multiple LEDs in
sequence, then in reverse using For
Loop Iteration
Components Required:-
❑Arduino Board
❑6 X 220 ohm resistors
❑6 X LEDs
❑Jumpers
❑breadboard
Circuit
CODING
int t = 100;
void setup()
{
for (int i = 2; i <=7; i++)
{
pinMode(i, OUTPUT);
}
}
void loop() {
for (int i = 2; i <=7; i++)
{
digitalWrite(i, HIGH);
delay(t);
digitalWrite(i, LOW);
}
for (int i = 7; i >= 2; i--)
{
digitalWrite(i, HIGH);
delay(t);
digitalWrite(i, LOW);
}
}
Exp 2- Light multiple LEDs in odd
sequence, then in reverse using For
Loop Iteration
CODING
int t = 100;
void setup()
{
for (int i = 2; i <=7; i++)
{
pinMode(i, OUTPUT);
}
}
void loop() {
for(int i=2; i<=7; i=i+2) {
digitalWrite(i,HIGH);
delay(100);
digitalWrite(i,LOW);
}
for(int i=7; i>=2; i=i-2)
{
digitalWrite(i,HIGH);
delay(100);
digitalWrite(i,LOW);
}
Exp 3- Light multiple LEDs in
patterns using For Loop Iteration
CODING
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
for( int i = 0; i < 1; i = i +1 ) {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
for( int i = 0; i < 2; i = i +1 )
{
digitalWrite(3, HIGH);
delay(500); // the duration is 0.5 seconds
digitalWrite(3, LOW);
delay(500);
}
for( int i = 0; i < 3; i = i +1 )
{
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
}
for( int i = 0; i < 4; i = i +1 )
{
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
delay(500);
}
❑ The array is normally built from the data types like integer, real,
characters, and boolean. It refers to a named list of finite number
(n) of similar data elements.
We can specify any name according to our choice. The array name is
the individual name of an element.
ARRAY DECLARATION
There are different methods to declare an array in Arduino, which are
listed below:
For example,
❑ int myarray[ ] = { 1, 4, 6, 7 } ;
For example,
For example,
The size of the array should not be less than the number of elements.
For example,
❑4x LEDs
void setup()
pinMode(ledPins[i],OUTPUT);
}
void loop()
{
control_led();
}
void control_led()
{
int delayTime = 2000;
digitalWrite(ledPins[0], HIGH);
delay(delayTime);
digitalWrite(ledPins[1], HIGH);
delay(delayTime);
digitalWrite(ledPins[2], HIGH);
delay(delayTime);
digitalWrite(ledPins[2], LOW);
delay(delayTime);
digitalWrite(ledPins[1], LOW);
delay(delayTime);
digitalWrite(ledPins[0], LOW);
delay(delayTime);
}
EXP 2- To glow LED’s in
patterns using functions
CIRCUIT
CODING
int ledPins[] = {2,3,4};
int pattern1[] = {HIGH,HIGH,LOW};
int pattern2[] = {LOW,HIGH,HIGH};
int pattern3[] = {HIGH,LOW,HIGH};
void setup()
{
❑ The first type of string that we will learn is the string that is a series of
characters of the type char.
❑ A string is a special array that has one extra element at the end of
the string, which always has the value of 0 (zero). This is known as a
"null terminated string".
String Character Array Example
This example will show how to make a string and print it to the serial
monitor window.
Example
void setup() {
char my_str[6];
Serial.begin(9600);
my_str[0] = 'H';
my_str[1] = 'e';
my_str[2] = 'l';
my_str[3] = 'l';
my_str[4] = 'o';
my_str[5] = ‘\0’;
Serial.println(my_str); }
void loop() { }
❑ The following example shows what a string is made up of; a
character array with printable characters and 0 as the last element of
the array to show that this is where the string ends.
❑ The string can be printed out to the Arduino IDE Serial Monitor
window by using Serial.println() and passing the name of the string.
This same example can be written in a more convenient way as shown below −
Example
void setup()
{
char my_str[] = "Hello";
Serial.begin(9600);
Serial.println(my_str); }
void loop()
{}
❑ In this sketch, the compiler calculates the size of the string array and also
automatically null terminates the string with a zero. An array that is six elements
long and consists of five characters followed by a zero is created exactly the
same way as in the previous sketch.
EXP 1- Serial.readString() function code to
loop-back from PC (serial monitor).
CODING
void setup()
{
Serial.begin(9600); // Set the baud rate to 9600
}
void loop()
{
String s1 = Serial.readString();// s1 is String type variable.
Serial.print("Received Data => ");
Serial.println(s1);//display same received Data back in serial
monitor.
delay(500);
}
EXP 2- Serial.readString() function code on
the LED on pin 13 when you send “on”
and turn off the LED when you send “off”.
CIRCUIT
CODING
void setup()
{
Serial.begin(9600); // Set the baud rate to 9600
pinMode(13,OUTPUT);
}
void loop()
{
String s1 = Serial.readString();// s1 is String type variable.
Serial.print("Received Data => ");
Serial.println(s1);//display same received Data back in serial monitor.
if(s1.indexOf("on")>=0)
{
digitalWrite(13,HIGH);
}
if(s1.indexOf("off")>=0){
digitalWrite(13,LOW);
}
delay(100);
}