Sunteți pe pagina 1din 22

Mastering Arduino from

beginners to core advance

THE COMPLETE COURSE

Shubham Panchal
Copyright © TELCOMA. All Rights Reserved
Module 3
programming form beginners
to advance

Copyright © TELCOMA. All Rights Reserved


Copyright @ Shubham panchal
Content:
1. Arduino syntax

2. Understanding variables

3. Digital read and serial port

4. Analog read

5. Introduction to pwm

6. Fade a led

7. If conditional statement

8. For loop

9. Using array

Copyright
Copyright © @ Shubham
TELCOMA. All Rights panchal
Reserved
Arduino syntax
Key understanding
the syntax of arduino is same as the c language

Declaring pin and variables


All the pins that needed for functioning are defined in void
setup
One time running of the program
To run program one time put it in void setup

Running program for long time or repeatedly


To run the code multiple time put it into the void loop so it will
keep looping for ideally infinite time
Function
Think of it as a box in which the code is placed and it is
called every time the whole code in the box is needed

Copyright
Copyright © @ Shubham
TELCOMA. panchal
All Rights Reserved
Syntax elements
● ; = this is used to terminate a line and should be placed after every line

● // = this is used to comment a line to exclude it out of the code or line


for user understanding

● /* and */ to comment multiple lines

● () = this parenthesis is used in function and the variable or object is


written in it

● { } = these are the curly brackets, the code is written in it and each
bracket { should be closed with }
Copyright @
Copyright © Shubham
TELCOMA. panchal
All Rights Reserved
Understanding variables and keywords
● Variable = declared by many types, they are given in data types

● Function = this is used when a part of code is to be used multiple times

● Classes = they are used in object oriented programming for multiple functions calling in the
program it is the library of a specific thing that is to be used in the program

● pinMode(pin number, INPUT ) to set pin as input

● pinMode(pin number, OUTPUT ) to set pin as output

● digitalRead(pin number) to read data from pin

● digitalWrite(pin number, HIGH/LOW) = write pin state high or low

Copyright @ Shubham
Copyright © TELCOMA. panchal
All Rights Reserved
List of data types
These are the variable data types that can

Be used in arduino for the variable .

It can be simple true or false for Boolean or

A name string in char type to a float digit

Such as 0.25 in float

Copyright © TELCOMA. All Rights Reserved


Copyright @ Shubham panchal
Demo

Copyright
Copyright ©@ Shubham
TELCOMA. All Rightspanchal
Reserved
Analog read
About analog pin Numerical & Categorical

The analog pin is a type of 10 bit ADC is available on the


data sensing in which the arduino uno.
voltage difference is divided
into any parts form 0 to 5
which is divided into 0 to 1023
as analog values so the pin
can read a difference in
voltage to get the desired
value.

The microcontroller has built in


analog to digital and digital to
analog converter in it to
perform certain action

The visual of the read voltage is


present in the serial monitor as follows
Copyright
Copyright@ Shubham
© TELCOMA. panchal
All Rights Reserved
Digital read and serial port

The digital read is a function that is used by the


microcontroller to read the available voltage state
on the pin It can be a low or high given the vale as
1 for high and 0 for low

All the display of the state can be done by opening


the serial begin function and declaring a baud rate
Say 9600 at begin and then opening the serial monitor
And select baud rate to 9600 to view the visual state

Copyright @ Shubham
Copyright © TELCOMA. panchal
All Rights Reserved
● Source code
● Visual when button pressed in serial monitor

Copyright
Copyright ©@ Shubham
TELCOMA. All Rightspanchal
Reserved
Difference between digital and analog
Both digital and analog are useful for particular operation and both can be input or output .
While digital only passes on and off state ,the analog five a fine value of the voltage difference for better result

difference

digital analog
• The digital signal can posses only two states as • The analog signal are 10 bit signals that gives
1 and 0 2^10 values that means 0 to 1023
• It can write up to 0 to 255
• They are used to control the state such led on or • They are used for a variety of signals such
off, motor stop or running control of speed or fading of led .it can also turn
on or off the led or motor
• All analog pin can be used as digital pins • All digital pins cannot be used as analog pins
,they are specified only can be used
• It uses function digitalRead or digitalWrite • It uses function analogRead or analogWrite

Copyright © TELCOMA. All Rights Reserved


Copyright @ Shubham panchal
Analog and digital codes

difference

Copyright © TELCOMA. All Rights Reserved


Copyright @ Shubham panchal
●The switching of the voltages in milliseconds
can be observed on a oscilloscope as
Introduction to pwm
●The pulse width modulation or PWM is the
method of getting a analog output by
switching the digital voltage between 0 to 5
volt in different duty cycles.

●This method gives a very useful output as it


can bee used for several application such as

Speed control of a dc motor or fading of a


LED bulb .
This shows the percentage of current
This method is also used for reading of analog allowed in a complete cycle called as duty
voltages by converting them to digital cycle percentage
voltages

Copyright
Copyright ©@ Shubham
TELCOMA. All Rightspanchal
Reserved
Fade a led

The led fading is a simple but


useful method to view the
pwm duty cycle change to
cause the led to change
percentage of glow with time
in a loop

There are only few pins in


UNO that can perform pwm
operation as 3,5,6,9,10,11 and
all analog pins

Copyright @ Shubham panchal


Copyright © TELCOMA. All Rights Reserved
Fade code explanation

●The led is connected between pin 3 and


ground

●Analog write function is performed to work


as a pwm function on pin 9 and change duty
cycle as declared in variable BRIGHTNESS.

●The program is written as it increases the


value of variable BRIGHTNESS from 0 to 255
in a continuous increasing order and when it
reaches 255 gets decreasing to 0 and the loop
continues in this way with a delay of 30 milli
seconds in each step change

Copyright
Copyright ©@ Shubham
TELCOMA. All Rightspanchal
Reserved
If conditional If statement

statement The if statement can be used in a branched manner


The conditional statement in programming is Such as when the condition 1 is fulfilled ,it will take
used when condition is to be observed the other condition into consideration and fulfil the
required condition 2 if the parameters are satisfied
The statement tells that if the required thing
happens then perform a certain action or it
will follow the act said in the else statement 1 1

These are the conditional statements , the if 2 2


can be standalone but the else statement
always needs a if statement 3 else

5
The if statement can be can be put up inside Independent conditions
Another if statement or can be performed in else
multiple if independent statements in the same loop
Inter dependent conditions
And a common else statement

Copyright © TELCOMA. All Rights Reserved


Copyright @ Shubham panchal
Condition
declared
Statement
to run Use curly braces on both ends

Next independent condition

Else condition

Else statement

Copyright
Copyright ©@ Shubham
TELCOMA. All Rightspanchal
Reserved
For loop
Define
integer

increment
for loop is used for running a certain
statement until the condition is not fulfilled.
For loop
The for loop has main parts as
condition
The condition, the difference and the addition
on each step Statement
to run
The statement to run until the condition is to
be fulfilled For loop
close
The exit of loop when the condition is met bracket

Copyright © TELCOMA. All Rights Reserved


Copyright @ Shubham panchal
While loop Define
integer

The while loop is a conditional statement which states


that the statement will run for infinite long time until
the condition is not met , till then as per the condition
of the while loop it will run as per the condition of the
while loop increment
The statement is simple as While loop
condition
While the condition is met such as x >4
the statement will run for infinite long time
If the condition is changed say x <4 the while loop will
Terminate
Statement
this is a useful loop in c programming to run

Copyright © TELCOMA. All Rights Reserved


Copyright @ Shubham panchal
Using array
Definition
The array can be described as a list of elements such as integers or strings which
are placed in a variable . It can be a single list or a 2 dimensional list even it can
be multi dimensional

Copyright
Copyright ©@ Shubham
TELCOMA. All Rightspanchal
Reserved
Next Module :
Physical computing basics

Copyright © TELCOMA. All Rights Reserved


Copyright @ Shubham panchal

S-ar putea să vă placă și