Unlock skill-first hiring with HackerEarth today

Learn more
piller_image

Arduino programming for beginners -2

Arduino programming

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
  • digitalRead
  • digitalWrite
  • analogRead
  • 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.

digitalRead

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

The syntax is as follows:

state=digitalRead(pin-number);

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);

analogRead

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:

value=analogRead (pin-number);

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.

Hackerearth Subscribe

Get advanced recruiting insights delivered every month

Related reads

Recruitment Management System – An Ultimate Guide
Recruitment Management System – An Ultimate Guide

Recruitment Management System – An Ultimate Guide

Defining a Recruitment Management System In today’s competitive talent landscape, attracting and retaining top performers is crucial for organizational success. Here’s where a…

Create Your Recruitment Funnel in 7 Simple Steps
Create Your Recruitment Funnel in 7 Simple Steps

Create Your Recruitment Funnel in 7 Simple Steps

Understanding the Recruitment Funnel Imagine a broad opening at the top, gradually narrowing down to a single point at the bottom. That’s essentially…

Skills Assessment Test: How It Works, Benefits & Examples
Skills Assessment Test: How It Works, Benefits & Examples

Skills Assessment Test: How It Works, Benefits & Examples

With the growing demand for highly skilled professionals, traditional hiring methods such as reviewing resumes and conducting interviews are no longer enough to…

Recruitment Workflow Process: A Complete Guide
Recruitment Workflow Process: A Complete Guide

Recruitment Workflow Process: A Complete Guide

Finding the perfect fit for your team can feel like searching for a unicorn. But fret not, fellow recruiters! Having a well-defined recruitment…

Conquer Your Hiring Challenges: Top Recruiting Software Picks for Small Businesses in 2024
Conquer Your Hiring Challenges: Top Recruiting Software Picks for Small Businesses in 2024

Conquer Your Hiring Challenges: Top Recruiting Software Picks for Small Businesses in 2024

In today’s competitive job market, attracting and keeping top talent is crucial for small businesses to thrive. But for busy small business owners…

How To Become A Technical Recruiter: Your Guide to Launching a Rewarding Career
How To Become A Technical Recruiter: Your Guide to Launching a Rewarding Career

How To Become A Technical Recruiter: Your Guide to Launching a Rewarding Career

The tech industry thrives on innovation, and at the heart of that innovation lies a crucial role: the technical recruiter. Why Technical Recruiting…

Hackerearth Subscribe

Get advanced recruiting insights delivered every month

View More

Top Products

Hackathons

Engage global developers through innovation

Hackerearth Hackathons Learn more

Assessments

AI-driven advanced coding assessments

Hackerearth Assessments Learn more

FaceCode

Real-time code editor for effective coding interviews

Hackerearth FaceCode Learn more

L & D

Tailored learning paths for continuous assessments

Hackerearth Learning and Development Learn more