- Basics of Arduino Coding
- How to use Android Phone to write codes and upload in Arduino?
- Effective coding techniques in Arduino: More about datatypes
- Effective coding techniques in Arduino: More about variables
- How to design Cathode Ray Oscilloscope using Arduino? Code of just 9 lines!
- How to start learning NodeMCU in simple steps? Basic Tutorial on NodeMCU
- How to use array to rotate servo motor in different angles?
- Blinking LED code for Arduino in Assembly Language Programming (ALP)
- Having trouble to drive Servo motor with Arduino Uno SMD? Get perfect solution now…
You don’t require to know anything about coding before reading this article.
We are starting it from scratch…!
Arduino coding is very simple to understand and use in embedded system programming. We will discuss basic details about Arduino coding in this article so that you will be quite confident to start with Arduino coding and design your own circuits and ideas and projects using Arduino.
Introduction
Here we assume that you have already installed Arduino IDE i.e. Arduino compiler software on your Windows PC. If you have not installed it yet, visit this direct downloading link of the software.
When downloading is complete, search the “arduino-1.8.19.zip” file in download folder of your PC, unzip the file by double clicking on it and then double click on “arduino-1.8.19.exe” file inside the zipped folder to start the installation of the software.
Now when you are ready, start reading the following topics. Remember it’s a series of posts to learn complete coding techniques about Arduino.
So when you complete reading the 1st post, continue with the 2nd post and 3rd post and so on.
Take your own time to understand each topic completely and then go to the next post in this series.
Coding Structure
The coding structure in Arduino programming is very simple. You must have at least two basic or default functions in every Arduino code, as shown below.
void setup() { statement1; statement2; .......... } void loop() { statement1; statement2; .......... }
Control Structure
When you write a particular program in Arduino IDE to control some hardware (firmware) circuit, then you will need some controlling commands with which you will be able to control the behavior of the hardware (firmware) like a robotic trolley to move it forward, backward, stop in front of an obstacle, then turn and so on.
There are different types of control structures used in Arduino, as follows –
The ‘if-else’ condition
The ‘if-else’ conditions are used to perform either this or that task i.e. when you want to perform a task when a particular condition is true. If that condition is not true i.e. false, then the control is passed on to ‘else’ condition.
if(int i=5) { // write your commands below, // which you want to execute when i=5 command1; command2; ........ } else { // write another commands below, // which you want to execute when i not equal to 5 command3; command4; ......... }
The ‘case-switch’ statements
In Arduino programming, the ‘switch-case’ statement is used to select one of many code blocks to be executed. It is commonly used when you have a variable with multiple possible values, and you want to execute different code based on the value of that variable.
You can of course use ‘case-switch’ statements within conditionals statements also.
switch(variable) { // code to be executed if variable equals value1 case value1: break; // code to be executed if variable equals value2 case value2: break; // ......... add more cases as required default: // code to be executed if variable doesn't match any case }
Example of ‘switch-case’
Here is an example code to control an LED on/off (connected at pin-13 of Arduino UNO) if the IR sensor output i.e. variable ‘irs’ (connected to pin-7) is either 0 or 1 i.e. LOW or HIGH.
The ‘IRStatus’ variable is used to record either 0 or 1 status of IR Sensor output.
int LED=13; int irs=7; int IRStatus; void setup() { pinMode(irs,INPUT); pinMode(LED,OUTPUT); } void loop() { IRStatus = digitalRead(irs); switch(IRStatus) { case 0: // in case 0, the LED is OFF digitalWrite(LED,LOW); break; case 1: // in case 1, the LED is ON digitalWrite(LED,HIGH); break; } }
The ‘for’ loop
In Arduino programming, the ‘for’ loop is used to execute a block of code repeatedly for a specified number of iterations i.e. repetitions. The general syntax i.e. structure of a ‘for’ loop in Arduino is as follows:
for(initialization; condition; increment) { statement1; statement2; .......... }
- Initialization: This is where you initialize a loop control variable or set the initial conditions. It is typically a variable that will be used to control the loop.
- Condition: This is a Boolean expression that is checked before each iteration. If the condition evaluates to true, the loop continues; otherwise, it exits.
- Increment: This is an expression that is evaluated after each iteration of the loop. It is used to update the loop control variable.
Example of ‘for’ loop
Suppose we want to blink an LED (connected at pin-13) in Arduino UNO in two different styles. First we have blink the LED at a rate of 1sec, 5 times and then blink it at a rate of 2 sec, 4 times. The example code is given below –
int LED=13; int i=0; void setup() { pinMode(LED,OUTPUT); } void loop() { // LED will blink 5 times for 1 sec each for(i=0;i<5;i++) { digitalWrite(LED,HIGH); delay(1000); digitalWrite(LED,LOW); delay(1000); } // LED will blink 4 times for 2 sec each for(i=0;i<4;i++) { digitalWrite(LED,HIGH); delay(2000); digitalWrite(LED,LOW); delay(2000); } }
The ‘while’ loop
The ‘while’ loop is used to execute particular commands when a condition is true. The general syntax of ‘while’ loop is as follows –
while(condition) { statement1; statement2; ........... }
Example of ‘while’ loop
Suppose we have to produce alarm when obstacle is detected in front of IR sensor. To construct the circuit for this code, we connect buzzer at pin-11 and output of IR sensor at pin-7.
The example code is given below –
int IRSensor=7; int buzzer=11; void setup() { pinMode(IRSensor,INPUT); pinMode(buzzer,OUTPUT); } void loop() { while(digitalRead(IRSensor==LOW)) { digitalWrite(buzzer,HIGH); delay(1000); digitalWrite(buzzer,LOW); } }
Example of ‘do-while’ loop
The structure of the ‘do-while’ loop is quite similar to the ‘while’ loop. Let us see the code of ‘do-while’ loop for the above example of ‘while’ loop –
int IRSensor=7; int buzzer=11; void setup() { pinMode(IRSensor,INPUT); pinMode(buzzer,OUTPUT); } void loop() { do { digitalWrite(buzzer,HIGH); delay(1000); digitalWrite(buzzer,LOW); } while(digitalRead(IRSensor)==LOW); }
The ‘continue’ statement
In Arduino programming, the ‘continue’ statement is used to skip the rest of the code inside a loop and proceed to the next iteration. This statement is commonly used with loops like ‘while’, ‘for’ and ‘do-while’. The continue statement allows you to skip specific iterations based on a certain condition.
In this example, we will print only ‘odd’ numbers on serial monitor and skip the even numbers –
int i=1; void setup() { Serial.begin(9600); } void loop() { int i=1; while (i<=10) { if (i%2==0) { // Skip even numbers using continue i++; continue; } // Print odd numbers Serial.println(i); // Increment the loop variable i++; } // write any other code outsite 'do-while' loop }
The ‘return’ statement
In Arduino programming, the ‘return’ statement is used to exit a function and optionally pass a value back to the calling code. Functions in Arduino, like in many programming languages, can be defined to return a value by specifying a data type in the function signature.
// Function declaration int add(int a,int b); void setup() { // Your setup code here Serial.begin(9600); // Function call int result=add(5,3); // Display the result using Serial Monitor Serial.println("Result: " + String(result)); } void loop() { // Your loop code here } // User defined function int add(int a,int b) { // Calculate the sum int sum=a+b; // Return the result return sum; } }
The ‘goto’ statement
The use of ‘goto’ is generally discouraged in modern programming due to its potential for creating spaghetti code and making the program difficult to understand and maintain. It’s often considered a bad practice because it can lead to unstructured and hard-to-follow code.
However, if you specifically want to convert the given code using ‘goto’, here’s an example:
int IRSensor=7; int buzzer=11; void setup() { pinMode(IRSensor, INPUT); pinMode(buzzer, OUTPUT); } void loop() { startLoop: if(digitalRead(IRSensor)==LOW) { digitalWrite(buzzer, HIGH); delay(1000); digitalWrite(buzzer, LOW); // Jump back to the beginning of the loop using goto goto startLoop; } }