You are on page 1of 21

Basic Embedded Systems Course

Training & Development Department


training@uruktech.com
 Using PushButtons
 Digital Input in Arduino
 IF Statement
 IF/ELSE Statement
 Switch/Case Statement
 Controlling LED by using Push Button
 Controlling the rate of Blinking Two LEDs by using Push Button (three Rates)

Training & Development Department


training@uruktech.com
Training & Development Department
training@uruktech.com
You can read the status of any Pin by using the following command:
Syntax:
Val = digitalRead(pin)

Parameters:
pin: the Pin number you wish to read its status
Val: variable to save the return of the function
Note: the pin should be previously set as INPUT port.

Training & Development Department


training@uruktech.com
if, which is used in conjunction with a comparison operator, tests whether
a certain condition has been reached. The format for an if test is:

Syntax:

if (condition) {

Target Code;

Training & Development Department


training@uruktech.com
if, which is used in conjunction with a comparison operator, tests whether
a certain condition has been reached. The format for an if test is:

Conditions:
x == y (x is equal to y)
x != y (x is not equal to y)
x < y (x is less than y)
x > y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)

Training & Development Department


training@uruktech.com
if, which is used in conjunction with a comparison operator, tests whether
a certain condition has been reached. The format for an if test is:

Logical operators:
Not ----------- (!condition)
AND ---------- (condition1 && condition2)
OR ---------- (condition1 || condition2)

Training & Development Department


training@uruktech.com
Example:

if (x == 50)
{
digitalWrite(2, HIGH);
}

Training & Development Department


training@uruktech.com
if/else allows greater control over the flow of code than the
basic if statement, by allowing multiple tests to be grouped together.

Syntax:
if (condition) {
Target Code (if condition is True);
}
else {
Target Code(if condition is False);
}

Training & Development Department


training@uruktech.com
Example:
if (y == 100) {
pinMode(3, INPUT);
}
else {
pinMode(3, OUTPUT);
}

Training & Development Department


training@uruktech.com
Like if statements, switch/ case controls the flow of programs by allowing
programmers to specify different code that should be executed in various
conditions.
Syntax:
switch (var) {
case label_1 :
target code when var equals label_1 ;
break;
case label_2 :
target code when var label_2;
break;
}
Training & Development Department
training@uruktech.com
Circuit Diagram:

Training & Development Department


training@uruktech.com
Assumptions:

• Push Button connected to PIN 2


• LED connected to PIN 7
• Whenever the Button is pushed, the LED goes ON

Training & Development Department


training@uruktech.com
Code:

Training & Development Department


training@uruktech.com
Code:

Training & Development Department


training@uruktech.com
Assumptions:

• Push Button connected to PIN 2


• LED1 connected to PIN 7
• LED2 connected to PIN 6
• When the button is pushed, each time the blinking rate of the
two LED is increased (100 ms, 250ms, and 500 ms)

Training & Development Department


training@uruktech.com
Code:

Training & Development Department


training@uruktech.com
Code:

Training & Development Department


training@uruktech.com
Code:

Training & Development Department


training@uruktech.com
Code:

Training & Development Department


training@uruktech.com
Thank you …
Q&A

You might also like