0% found this document useful (0 votes)
7 views

Arduino Programing

Uploaded by

Naman Bahri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arduino Programing

Uploaded by

Naman Bahri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ARDUINO PROGRAMMING

1). void setup ( ) :- setup () is a function. Void is a command by using that we declare an
Function.
 With the every function after that a open-bracket ‘(’ and close-bracket ‘)’ exist.
 In void setup(), the functions executed by Arduino is one time only. But, In void loop(),
the function executed by Arduino repeatedly again and again.
NOTE:-
 In Arduino, program run in linear pattern / fashion.
 Curley Brackets { }, used after void setup() and void loop() to show that program start
and end up here.
 In Arduino, program is case-sensitive.
 In Arduino, we use ‘ // ’ double slash to show or add comment in program.
 We put ‘ ; ’ semi-colon after every statement in program.

2). Variables :- Variables are used to store information to be reference and manipulated in a
computer program.
There are 3-things are required to declare a variable:-
1. Data Type :– It is show that which of the Data is like Numeric, Word, Letter, etc.
2. Variable Name :– Like we put in program i.e. “ int_value_=10; ” ‘int’ is a Data Type
(integer), ‘Value’ is a variable name, ‘10’ is the Value assign to it.
3. Value:- It is a value which is assign to any variable. Like show in above example.

Now, There is two types of variable used used in arduino programming:-


1. Global Variable :- Global variable declare globally and it can be used in any function in
our program.
2. Local Variable :- Local variable declare only inside a function and can be used only
inside of that function. It cannot be used by any other function in programming.
Example:-
Global Variable Local Variable
int value = 10; void setup()
void setup() {
{ int value = 10;
} }
void loop() void loop()
{ {
} }

3). Some Basic for controlling the Arduino Board :-


1. pinMode ( ) :- It is used to configures the specified pin to behave either as an input or an
output.
Syntax:- pinMode(pin, mode);
Where, ‘pin’ shows the arduino-pin number and ‘mode’ shows that this pin work
as INPUT / OUTPUT.
Example:- pinMode(13,OUTPUT);
2. digitalWrite ( ) :- It is a command to send 5V (volt) or 0V to any digital pin of Arduino
Board. We can write ‘HIGH’ for 5V and ‘LOW’ for 0V for a digital pin.
Syntax:- digitalWrite(pin,value);
Where, pin shows the pin number and value choose by us as HIGH or LOW.
Example:- digitalWrite(13,HIGH);
3. delay ( ) :- This command will pause the program for specific amount of time (in-
millisecond) specified as parameter.
Syntax:- delay(ms); where, the ms – number of millisecond to pause.
Example:- dealy (1000); (Note:- 1-Second is equal to 1000 milli-second)
4). Serial Monitor :- In Arduino Board there are two pins D0(Rx0) and D1(Tx1), which is used
for UART (Universal Asynchronous Transmitter Receiver) communication and it is also known
as Serial Communication.
We can upload program code to Arduino board using this Rx and Tx pin also.
Through, Serial Monitor we observe the data which is transmitted or received by our Arduino
board.
Example:- void setup ()
{
serial.begin(9600); // it is used when we communicate with Rx and Tx.
// 9600 is (Bit’s per second) speed of transferring data Known
as Baud-Rate.
}
void loop ()
{
if (serial.available()=1)
{
char value = serial.read ( );
serial.print (value);
}
}

5). Numeric Data types in in Arduino Programming:-


1. byte :- A byte stores an 8-bit unsigned (only +ve value) number, from ‘0’ to ‘255’.
Maximum value of (28 - 1).
Syntax:- byte var = value;
Where, var – variable name and value – the value to assign to the variable.
Example:- byte var = 155;
2. word :- A word can store an unsigned number of at least 16-bits from ‘0’ to ‘65535’
[ maximum value (216 - 1)].
Syntax:- word var = value;
Example:- word var = 2568;
3. short :- A short is a 16-bit data type. A short stores a 16-bit (2-bytes) value. This yields a
range of -32,768 to 32,767 [minimum value of −215 and a maximum value of (215−1)].
Syntax:- short var = value;
Example:- short var = -1552;
4. int :- It is totally same as to ‘short’ data type.
Syntax:- int var = value;
Example:- int var = -282;
5. long :- Long variables are extended size variables for number storage and store 32-bits
(4-byte) from -2,147,483,648 to 2,147,483,647 [minimum value of −231 and a maximum
value of (231−1)].
Syntax:- long var = value;
Example:- long var = -25682;
6. float :- Data type for floating-point numbers, a number that has a decimal point. Floating
– point numbers can be as large as 3.4028235 * 1038 and as low as -3.4028235 * 1038. It
has 7-decimal digit.
Syntax:- float var = value;
Example:- float var = 25.26;
7. double :- The double implementation is exactly as the same as float. It has 15-decimal
digit.
Syntax:- double var = value;
Example:- double var = 25.2634;
8. unsigned int :- unsigned int’s (unsigned integers) are the same as the int’s in that they
store a 2-byte value. Instead of storing –ve number, however they only store +ve values,
yielding a useful range of ‘0’ to ‘’65,535’ that is (216−1).
Syntax:- unsigned int var = value;
Example:- unsigned int var = 25;
9. unsigned long :- unsigned long variables are extended size variables for number storage
and store 32-bits (4-bytes). Unlike standard longs unsigned longs won’t store –ve
number, making their range from ‘0’ to ‘4,294,967,295’ (232 −1).
Syntax:- unsigned long var = value;
Example:- unsigned long var = 2525487;
6). Text Data types in Arduino Programming :-
1. char :- In char [(all alphabets small and big letter), (Numbers), (symbol’s), (Maths
symbols like +,-,*,/, etc.)] are used. A data type used to store a character value. Character
literals are written in single quotes. Like this:- ‘A’
syntax:- char var = value;
Example:- char var = ‘A’;
Note:- We also use Decimal values in the place of character, which is given in ASCII
chart.
Program :- char var = 65;
void setup ( )
{ serial.begin(9600);
serial.print(var); }
void loop ( )
{
}
2. string :- A data type used to store set of characters that can also contain spaces and
numbers. String are always defined inside double quote like (“ AbC ”).
Syntax:- String var = value;
Example:- String var = “Abc”;
Program:- String var = “This is Arduino \n Welcome to Our Class”;
Void setup ( )
{
serial.begin(9600);
serial.print(var);
}
7). Array Data Types :-
Array :- An array is a collection of variables that are accessed with an Index Number. In array
we use any data types like int, char, string, etc.
Examples of Array :-
1. int val [6]; where, the value under [ ] – shows the limits of an array.
2. int val [ ] = {2,3,4,5,6,7,8};
3. int value [6] = {2,3,4,5,6,7};
4. char value [ ] = {‘a’, ‘b’, ‘c’, ‘d’};
5. char value [4] = {‘a’, ‘b’, ‘c’, ‘d’};
6. char value = “ abcd ”;
7. char val [4] = “ abcd ”;
Let take 3rd from above point i.e. int val [6] = {2,3,4,5,6,7};
In above array every number has a particular location / Index number, by which we use any
one of them in, given array data.

2 3 4 5 6 7
Data in Array

0 1 2 3 4 5 Index Number or Location of particular Data.

Program :- int val [6] = {2,3,4,5,6,7}; string val [ ] = {“apple”, “mango”, “banana”};
void setup ( ) void setup ( )
{ serial.begin(9600); { serial.begin(9600);
serial.print(val[1]); } serial.print(val[2]); }
void loop ( ) void loop ( )
{ } { }

Output we get :- 3 Output we get :- banana


8). Digital I/O (Input / Output) Functions in Arduino programming :-
 pinMode ( ) It is used to select the Arduino pin.
 digitalWrite ( ) It sends the high or low signal.
 digitalRead ( ) It Reads / Receive the value from a specified digital pin, either HIGH
or LOW.
Syntax :- digitalRead (pin); where, pin is Arduino pin number.
Example:- digitalRead(12);
Program:- control Arduino LED using push – Button –
void setup ( )
{ pinMode(12,INPUT_PULLUP);
serial.begin(9600); }
void loop ( )
{ int val = digitalRead(12);
serial.println(val); }
NOTE:- INPUT_PULLUP makes the digital pin high initially, until it receives a low
signal from an external device.
serial.println() , here we use ln with print to receive every values in next line.
9). Analog I/O Functions in Arduino Programming :-
 analog Read ( ) :- This functions used to read Analog values from an output device. Like
‘Gas Sensor’ which sense gases and send analog values to Arduino board. Here, analog
value varies from 0V to 5V.
It read the value from the specified analog pin. Arduino board contains a multichannel
10-bit analog to digital converter. This means that it will map input voltages between (0V
to 5V) into integer value between 0 and 1023.
Syntax :- analogRead(pin);
Example :- analogRead(A0);
 analog Write( ) :- By using this we can writes an analog value (PWM-wave) to a pin. It
can be used to Light LED at various brightness or drive a motor at various speeds.
This function is not used for analog pins of Arduino Board. Whereas, this function
execute only on PWM-pins of Arduino board and which is Pin Number :- [~3, ~5, ~6, ~9,
~10, ~11].
PWM (Pulse Width Modulation):- It is a technique by using that we can execute an
analog result. By using this technique we can control the 0V to 5V as a pulses due to this
we control brightness of LED or Speed of Motor.
In analogWrite(), there 0V to 5V varies from 0-255 values.
Syntax :- analogWrite(pin,value); where, value is the duty cycle between 0 (always
off) and 255(always On).
Example :-
Program – 1st Program – 2nd
void setup () void setup ( )
{ serial.begin(9600); } {
void loop () }
{ void loop ( )
int val = analogRead(A0); {
Serial.println(val); analogWrite(6, 255);
delay(1000); }
}
10). If-Else control structure in Arduino Programming :-
The if statement checks for a condition and executes the proceeding statement or set of
statements if the condition is true.
Syntax:- if (condition)
{ statement1; }
else
{ statement2; }
Program (Example):- if (temperature > 30)
{ digitalWrite(6,HIGH); }
else
{ digitalWrite(6,LOW); }
// ‘>’ this symbol is known as comparison operator.
Comparison Operator:-
(1) X= = Y (‘X’ is equal to ‘Y’), whereas, ‘=’ is used to assign a value to operator and ‘=’
used to compare the values.
(2) X!= Y (‘X’ is not equal to ‘Y’), here ‘!’ means ‘not’
(3) X< Y (‘X’ is less than ‘Y’)
(4) X >Y (‘X’ is greater than ‘Y’)
(5) X< = Y (‘X’ is less than or equal to ‘Y’)
(6) X > = Y (‘X’ is greater than or equal to ‘Y’).

11). If_ _ _Else If control structure in Arduino Programming :-


The ‘ if ’ statement can be followed by an optional ‘else if_ _ _ _ else’ statement, which is
very useful to test various conditions.
Syntax:- if (condition1)
{ statement1; }
else if (condition2)
{ staement2; }
else
{ staement3; }
Note:- We can use multiple ‘else-if ’ conditions for multiple output of device as we need.
12). Switch….Case control structure in Arduino Programming :-
 Like ‘if ’ statement, switch-case controls the flow of programs by allowing programmers
to specify different code that should be executed in various conditions. In particular, a
switch statement compares the value of a variable to the values specified in case
statements. When a case statement is found whose value matches that of the variable, the
code in that case statement is run.
 The ‘break’ keyword exists the switch statement, and is typically used at the end of each
case. Without a break statement, the switch statement will continue executing the
following expression (“falling-through”) until a break, or the end of the switch statement
is reached.
Syntax:- switch (var)
{ case1:
staement1; //do something when var equals 1.
break;
case2:
staement1; // do something when var equals 2.
break;
default:
staement3; //if nothing matches, then default is happening.
break; }
Program to glow 4-LED one by one by pushing a button:-
int count = 0; case2:
void setup ( ) digitalWrite(4,HIGH);
{ PinMode(2,INPUT_PULLUP); break;
PinMode(3,Output); case3:
PinMode(4,Output); digitalWrite(5,HIGH);
PinMode(5,Output); break;
PinMode(6,Output); } case4:
digitalWrite(6,HIGH);
void loop ( ) break;
{ int val = digitalRead(2); default:
if (val = = 0) digitalWrite(3,LOW);
{ count = count+1; } digitalWrite(4,LOW);
digitalWrite(5,LOW);
Switch(count) digitalWrite(6,LOW);
{ case1: count = 0; }
digitalWrite(3,HIGH);
break; dealy(200);
break; }

13). ‘for loop’ control structure in Arduino Programming :-


The ‘for’ statement is used to repeat a block of statements enclosed in curly braces. An
increment counter is usually used to increment and terminate the loop. The ‘for’ statement is
useful for any repetitive operation, and is often used in combination with arrays to operate on
collections of data/pins.
[‘loop’ means repeating anything again and again] and in ‘for-loop’ we can set the limitation to
repeat a program by own choice.
Syntax:- for (initialization; condition; increment)
{ statement(s); }
Initialization:- happens first and exactly once.
Condition:- Each time through the loop, condition is tested; if it’s true, the statement block and
the increment is executed, then the condition is tested again. When the condition becomes false,
the loop ends.
Increment:- executed each time through the loop when condition is true.
Example:- for (int i=0; i<=255; i++) // or for decrement (i--) used.
{ analogWrite(10,i);
delay(10); }

14). ‘while loop’ control structure in Arduino Programming :-


A while loop will loop continuously, and infinitely, until the expression inside the
parenthesis, ( ) becomes false. Something must change the tested variable, or the while loop will
never exit. This could be in your code such as an incremented variable, or an external condition,
such as testing a SENSOR.
Syntax:- while (condition)
{
statement(s); }
Example:- var = 0;
While(var < 200)
{ //do something repetitive 200 times
V++; }
Program:- when vibration sensor sense, then the led will glow.
void ( )
{ PinMode(4, OUTPUT);
PinMode(5, INPUT); }
void loop ( )
{while (digitalRead(5)= = LOW)
{ digitalWrite(4, LOW); }
digitalWrite(4,HIGH);
delay(1000); }

15). Creating Functions:- Creating your own functions allows you to organize your code into
smaller chunks and treat complicated tasks as a single step.

Example (Program):- void myFunction( )


{ digitalWrite(3,HIGH);
digitalWrite(4,LOW);
delay(2000);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
delay(2000); }

void setup( )
{ myFunction();
delay(1000); }

void loop( )
{ myFunction(); }

 Why to use Functions?


(1) Reusability:- Functions can be used multiple times in a program, making your
code more efficient and easier to maintain.
(2) Readability:-Functions make your code more organized & easier to read,
especially as your programs get larger.
(3) Debugging:- Function Isolate specific tasks, making it easier to debug your
code if something goes wrong.
Syntax:- void myFunction( )
{ //function body; }
Example:- void multiply( )
{ //function body
int value1 = 2;
int value2 = 3; will be called as multiply( );
int multiplied_value = value1 x value2;
serial.println(multiplied_value); }

Syntax:- void myFunction (int value1, float value2)


{ //function body }
It will be called as:- myFunction(2,3);

You might also like