Royal University of Phnom Penh
Faculty of Engineering
Programming Arduino
Standard Arduino
Library
Table of Contents
7.1. Random Number 7.4. Advanced I/O
7.2. Math Function 7.4.1. Generating Tones
7.3. Bit Manipulation 7.4.2. Feeding Shift Register
7.5. Interrupts
Arduino library គត្រ
ី ូវបានសរសេរឡង
ើ ដោយ CឬC++
។អ្នកទាំងអស់គ្នា ប្រហែលជាជួប
រួចមកហយ
ើ នូវpinMode, digitalWrite, and analogWrite ។ប៉ុន្តែប្រហែលជាអ្នកមន
ិ ដឹងទេថា
វានូវមានមុខងារជាច្រន
ើ ទៀតដូចជា doing math, making random numbers, manipulating bits,
detecting pulses on an input pin, and using something called interrupts។
Random Numbers
Math Function
On rare occasions, you will need to do a lot of math on an Arduino, over and above
the odd bit of arithmetic. But, should you need to, there is a big library of math functions
available to you. The most useful of these functions are summarized in the following table:
Math Functions
Function Description Example
abs (12) return 12
abs Return the unsigned value of its argument.
abs (-12) return 12
Constrain to stop it from exceeding a range. The first argument is the number constrain (8 , 1,10) return 8
to constrain , the second is the start of the range , and third is the end of the constrain (11 , 1,10) return 10
constrain
allow range of number. constrain (0, 1,10) return 1
Map a number in one range into another range. The first argument is the
number to map, the second and third are the ‘’ from ‘’ range or (source
map map ( x ,0,1023, 0 , 5000)
range), and the last two are the ‘’ to ‘’ range ( or destination range. The
function useful for remapping analogue input values.
max Return the larger of its two argument. max (10,11) return 11
min Return the smaller of its two argument. max (10,11) return 10
pow Return the first argument raised to the power of the second argument. pow (2,5 ) return 32
sqrt Return the square root of a number. sqrt (16) return 4
sin, cos, tan Perform trigonometric function. They are no often used.
log Calculate the temperature from logarithmic thermistor (for example)
Bits Manipulation
A bit is a single digit of binary information, that is, either 0 or 1.
bit is a contraction of binary digit.
Most of the time, we use int variables that actually comprise 16 bits
Actually, unless we are running short of memory, being wasteful is less of a problem than creating difficult-to-
understand code, but sometimes it is useful to be able to pack your data tightly.
Each bit in the int can be thought of as having a decimal value of all the bits that are a 1.
When you are thinking about individual bits, decimal values do not really work very well.
For that reason, programmers often use something called hexadecimal, or, more commonly, just hex.
Hex is number base 16 ( 0 to 9 and extra 6 digits, A to F)
Bits Manipulation
The Arduino standard library provides the function bitRead to return the value of a particular bit in an int.
As you would expect, the counterpart to bitRead is bitWrite, which takes three
arguments.
The first is the number to manipulate.
The second is the bit position.
The third is the bit value.
Bits Manipulation
Conversion Binary To Decimal
Example:
Decimal to Binary
111
Example:
1) 14 ={? = 1 * 23 + 1 * 22 + 1 * 21 + 0 * 20
=
14
So, 111 = 14
Note : For bitwise NOT (-) is an unary operator that flips the
bits of the number i.e.,
if the i th bit is 0, it will change it to 1 and vice versa.
So, 14 = {1110 }2
Example:
N = 5 = (101)2
-N = -5 = -(101)2 = (010)2 = 2
Advanced I/O
There are some useful little functions that you can use to make your life easier when
performing various input/output tasks.
Generating tone
The tone function allows you to generate a square-wave signal on one of the digital output pins.
The most common reason to do this is to generate an audible tone using a loudspeaker or buzzer.
The function takes either two or three arguments.
tone( pin, frequency)
tone( pin, frequency, duration)
Pin number
Frequency of the tone Duration of tone
in hertz(Hz)
Generating tone
To stop a tone that is playing, you use the function noTone(). This function has just one
argument, which is the pin on which the tone is playing.
noTone(pin-Number)
Feeding Shift Registers
A shift register allows you to expand the number of I/O pins you can use from
your Arduino
To help you use this technique, there is a handy function called shift-Out. This function
takes four arguments:
• The number of the pin on which the bit to be sent will appear.
• The number of the pin to be used as a clock pin. This toggles every time a bit is sent.
• A flag to determine whether the bits will be sent starting with the least significant bit or
the most significant. This should be one of the constants MSBFIRST or LSBFIRST.
• The byte of data to be sent.
Interrupts
Normally, Arduino can do only one thing at a
time but Arduino can get multiple execution is the use
of interrupts.
Two of interrupts pins are (D2,D3).
This mean, Arduino’s processor will susp9end
whatever it was doing and run a function attached
to that interrupts.
Note: You can simulate an interrupt by
1. Connecting pin D2 and GNG and
2. Using the internal pull-up resistor
Interrupts
First argument, it’s mean which one of (D2 or D3)
that you want to us. They let 0 for D2 and 1 for D3.
Second argument, is the name of function that is
to be called when interrupt.
Final, is one of FALLING, RAISING, CHANGE
1. We can disable interrupt by function noInterrupts
2. We can resume interrupt by function interrupts
Conclusion
In this chapter, we learn some features that the Arduino standard library provides.
1. The features will save you some programing effort and
2. Being able to use high-quality work done by other people.
Thank you for your attention