# Arduino programming for beginners -2

In  the previous article, we studied the basic structure of Arduino programming with examples. Now, in this article let’s look at a few more Arduino programming concepts like the following:

• if statement
• if else statement
• for loop
• while loop
• do while loop
• pinMode
• digitalWrite
• analogWrite

if statement

The “if” statement is a conditional statement, it checks if a certain condition is met. If yes, it executes the set of statements enclosed in curly braces. If the condition is false, then the set of statements will skip the execution.

The syntax of the “if” statement is follows:

if(some variable ?? state)

{

statement-1;

.

.

.

statement-n;

}

In the above format, ?? represents comparison operator

X==Y // Check if X is equal to Y

X!=Y // X is not equal to Y

X<Y // Check if X is less than Y

X>Y // Check if X is greater than Y

X<=Y // Check if X is less than or equal to Y

X >=Y // Check if X is greater than or equal to Y

Note: “=” is used to assign a value, where as == is used for comparison.

Example

if (LED == HIGH)

{

digitalWrite(LED, LOW);

}

if-else statement

This statement makes an “either-or decision. The if statements checks a condition. If it is true, it executes a set of statements; if the condition is not true, it executes other set of statements.

The syntax of “if-else” statement is as follows:

if (some variable ?? state)

{

statement-1;

.

.

.

statement-n;

}

else

{

statement-1;

.

.

.

statement-n;

}

Example

if(LED==High)

{

digitalWrite(LED, LOW);

}

else

{

digitalWrite(LED,HIGH);

}

else can also be preceded by another if statement; the syntax is as follows:

if (some variable ?? state)

{

statement-1;

.

.

.

statement-n;

}

else if ( some variable ?? state )

{

statement-1;

.

.

.

statement-n;

}

else

{

statement-1;

.

.

.

statement-n;

}

The if-else state is used when you want to run two mutually exclusive tests and run a separate set of statements for each test.

for loop

If you want to repeatedly execute a set of statements for a specific number of times, then you can use a for loop.

The syntax of the “for” statement is as follows:

for(initialization; condition; expression)

{

Statement-1;

.

.

.

Statement-n;

}

In the for loop, the variable initialization is done only once. After this the set of statements is executed, the initialization step is skipped and directly the condition will be checked. If the result is true, before executing the set of statements in the curly braces, the expression will be executed.

Example

for (int X=0; X<50; X++) //Variable X is initialized to 0, check if X is less than 50, if yes, increase the value of X by 1

{

digitalWrite (12, HIGH); //Turn on the component connected to pin number 12

delay(1000); //Wait for 1 sec

digitalWrite ( 12, LOW); //Turn OFF the component connected to pin number 12

delay (1000); //Wait for 1sec

}

Note: X++ is same as X=X+1 or X+=1

while loop

The while loop executes a set of statements until the expression inside the parentheses is false.

The syntax of the “‘while” loop is as follows:

while (some variable ?? value)

{

Statement-1

.

.

.

Statement-n;

}

Example

while(i<=200)

{

digitalWrite (12, HIGH); //Turn on the component connected to pin number 12

delay(1000); //Wait for 1 sec

digitalWrite ( 12, LOW); //Turn OFF the component connected to pin number 12

delay (1000); //Wait for 1sec

i++;

}

The set of statements under a while loop is executed only if the condition is true. In the while loop, there should be a statement which changes the state of the tested variable. If not, the while loop will never exit. In the example above, the tested variable is “i” and the statement that changes its state is “i++.”

do while loop

If you want to execute a set of statements once and repeatedly execute the set if a certain condition is true,the syntax of the “do-while” loop is as follows:

do

{

statement-1;

.

.

.

statement-n;

} while (some variable ?? value);

The do-while loop is a bottom-driven loop because the condition is checked at the end, after executing the set of statements at least once.

Example

do

{

temperature=readSensor( ); // Variable temperature is set to boiler temperature

digitalWrite(13, HIGH); //Turn ON the LED connect to pin number 13

delay(1000); //Wait for 1sec

}while(temperature<=35); // Check if temperature is less than or equal to 35

The program above turns on the LED if the boiler temperature is less than or equal to 35.

pinMode

This statement is used in the preparation block of the Arduino program, that is, in the void setup() function.The pinMode statement is used to configure a pin to behave in either the INPUT mode or OUTPUT mode.

The syntax of the “pinMode” statement is as follows:

pinMode (pin-number, behaviour);

Example

pinMode (13, OUTPUT);

Note: Digital pins on the Arduino board are input, by default, so these pins can be used in the INPUT mode even without declaration.

This statement reads the state of the specified digital pin and the result will be either HIGH or LOW.

The syntax is as follows:

digitalWrite

digitalWrite statement is used to either turn ON or OFF the device connected to a specified digital pin.

The syntax of the “digitalWrite” statement is as follows:

digitalWrite(pin-number, status)

Example

digitalWrite(12,HIGH);

digitalWrite(13,LOW);

This statement reads the value from the specified analog pin on the Arduino board with 10-bit resolution. The result will be an integer value in the range of 0 to 1023.

The syntax of the “analogRead statement” is as follows:

analogWrite

This statement writes a pseudo-analog value to the specified pin. The value is called pseudo-analog because it is generated by Pulse Width Modulation pins (PMW) on the Arduino board. The value can be specified as a variable or constant in the range of 0 to 255.

The syntax of “analogWrite” statement is as follows:

analogWrite(pin-number, value);

Example

analogWrite(9,255);

Note: Analog pins need not be declared as INPUT or OUTPUT pin.

Congratulations! You have completed the basic level of Arduino programming, but do not settle down as there are a few more things to learn before you become a pro.

In the next article, we will discuss how you can work with different sensors. Till then keep building.

### 7 New HackerEarth Assessments Product Updates in 2023 You Should Know About

This year, as the industry went through a hiring freeze, we at HackerEarth took the time to elevate our product lineup so that…

### AI Recruiting Software: Revolutionizing the Hiring Process

In today’s dynamic business landscape, organizations are constantly seeking ways to optimize their talent acquisition strategies to attract and retain top performers. The…

System design interviews are becoming increasingly popular, and important, as the digital systems we work with become more complex. The term ‘system’ here…

### Bridging the Bias Gap: Enhancing Technical Recruiting with Psychometric Assessment

In the dynamic world of technical recruiting, where skills testing forms the cornerstone of the selection process, we have a potent tool waiting…

### How Do You Get Senior Engineers to Take Coding Assessments

I recently came across this post on Reddit, and though it was posted about 3 years ago, it wonderfully encapsulates every senior engineer’s…

### How Developer Observability is Transforming Dev Role

This article was written by Alwayne Powell, the Senior Digital Marketing Manager at 8×8 contact centre and communication platform. You can find them…

Hackathons

Assessments