Control Statements and Loops
Control Statements and Loops
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:
while(1) {
// statement block
}
do {
Block of statements;
}
while(1);
analogWrite()
Syntax-
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