0% found this document useful (0 votes)
16 views

Control Statements and Loops

This document discusses control statements and loops in Arduino. It covers if, if/else, if/else if/else statements, switch/case statements, conditional operators, while loops, do/while loops, for loops, nested loops, infinite loops, and using analogWrite() to fade an LED. An example is provided to fade an LED on and off by using analogWrite() within for loops to increment or decrement the brightness from 0 to 255 over 30 milliseconds to produce a dimming effect.

Uploaded by

Muntasir Sunny
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Control Statements and Loops

This document discusses control statements and loops in Arduino. It covers if, if/else, if/else if/else statements, switch/case statements, conditional operators, while loops, do/while loops, for loops, nested loops, infinite loops, and using analogWrite() to fade an LED. An example is provided to fade an LED on and off by using analogWrite() within for loops to increment or decrement the brightness from 0 to 255 over 30 milliseconds to produce a dimming effect.

Uploaded by

Muntasir Sunny
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Control Statements and

Loops in Arduino
CSE- 315
Peripherals & Interfacing
Abdullah Al Omar
Lecturer, CSE, UAP
Control statements
Control statement:
• If
• If…. else
• If….else if()….else
• Switch case
• Conditional Operator ?:
If statement:
if (expression)
statement;

OR,

if (expression) {
Block of statements;
}
If statement: (contd.)
int A = 5 ; /* Global variable definition */
int B = 9 ;
Void setup () {
}
Void loop () { /* check the boolean condition */
if (A > B) /* if condition is true then execute the following statement*/
A++;
/* check the boolean condition */
If ( ( A < B ) && ( B != 0 )) /* if condition is true then execute the following statement*/ {
A += B;
B--;
}
}
If… else statement:
if (expression) {
Block of statements;
}
else {
Block of statements;
}
If… else statement: (contd.)
int A = 5 ; /* Global variable definition */
int B = 9 ;
Void setup () {

}
Void loop () {
/* check the boolean condition */
if (A > B) /* if condition is true then execute the following statement*/ {
A++;
}else {
B -= A;
}
}
If..else If…. else statement:
if (expression_1) {
Block of statements;
}

else if(expression_2) {
Block of statements;
}
.
.
.

else {
Block of statements;
}
If..else If…. else statement: (contd.)
int A = 5 ; /* Global variable definition */
int B = 9 ;
int c = 15;
Void setup () {
}
Void loop () {
/* check the boolean condition */
if (A > B) /* if condition is true then execute the following statement*/ {
A++;
}
/* check the boolean condition */
else if ((A == B )||( B < c) ) /* if condition is true then
execute the following statement*/ {
C = B* A;
}else
c++;
}
Switch case statement:
switch (variable) {
case label:
// statements
break;
}

case label: {
// statements
break;
}

default: {
// statements
break;
}
Switch case statement: (contd.)
switch (phase) {
case 0: Lo(); break;
case 1: Mid(); break;
case 2: Hi(); break;
default: Message("Invalid state!"); break;
}
Conditional Operator:
expression1 ? expression2 : expression3

max = ( a > b ) ? a : b;
If the condition is true then a otherwise b.
Loops
Loops:
• While
• Do…. While
• For
• Nested Loop
• Infinite Loop
While loop:

while(expression) {
Block of statements;
}
Do… While

do {
Block of statements;
}
while (expression);
For loop:

for ( initialize; control; increment or decrement) {


// statement block
}
Nested Loop:
for ( initialize ;control; increment or decrement) {
// statement block
for ( initialize ;control; increment or decrement) {
// statement block
}
}
Infinite Loop:
for (;;) {
// statement block
}

while(1) {
// statement block
}

do {
Block of statements;
}
while(1);
analogWrite()
Syntax-

analogWrite(Pin Number, PWM value);


Fading a LED with analogWrite:
Hardware Required:
• Arduino or Genuino board
• LED
• 220 ohm resistor
• hook-up wires
• breadboard
Fading a LED with analogWrite: (Contd.)
Fading a LED with analogWrite: (Contd.)
int ledPin = 9; // LED connected to digital pin 9

void setup() {
// nothing happens in setup
pinMode(ledPin, OUTPUT);
}
Fading a LED with analogWrite: (Contd.)
void loop() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Fading a LED with analogWrite: (Contd.)
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Fading a LED with analogWrite: (Contd.)
[Loop code at once.]
void loop() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Thank You

You might also like