Programming Aruduino C7
Programming Aruduino C7
Faculty of Engineering
Programming Arduino
Standard Arduino
Library
Table of Contents
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
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
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.
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
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.
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.
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