0% found this document useful (0 votes)
287 views24 pages

Built in Function

This document summarizes built-in functions for digital input/output, analog input/output, advanced input/output including sound, serial data, pulse width reading, time functions, math functions, trigonometry, random numbers, bit manipulation, external interrupts, and more. It provides details on the purpose and syntax of these functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
287 views24 pages

Built in Function

This document summarizes built-in functions for digital input/output, analog input/output, advanced input/output including sound, serial data, pulse width reading, time functions, math functions, trigonometry, random numbers, bit manipulation, external interrupts, and more. It provides details on the purpose and syntax of these functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Built in Function

Digital Input/Output
• pinMode(pinNo,mode)
pinNo = 0..13, A0 ..A5
mode = INPUT, OUTPUT, or INPUT_PULLUP*
* Arduino IDE >= 1.01
• digitalWrite(pinNo,HiLo)
pinNo = 0..13, A0 ..A5
HiLo = HIGH or LOW
• byte digitalRead(pinNo)
pinNo = 0..13, A0 ..A5
return = HIGH or LOW
Analog Input/Output

• analogReference(source)
source = DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56,
or EXTERNAL
* Voltage Reference Source for analogRead() - ADC
• word analogRead(pinNo) - ADC
pinNo = A0 .. A5
return = 0 .. 1023 (karena 10 bit, maka menggunakan “word”)
• analogWrite(pinNo,value) - PWM
pinNo = 3, 5, 6, 9, 10, or 11
value = 0 .. 255
Advanced Input/Output (1)
Sound Output

• tone(pinNo,freq)
tone(pinNo,freq,duration)
pinNo = 0 .. 13 , A0 ..A5
freq = 0 .. 32767 Hertz
duration = 1 .. 4294967295 mili Second,
0 => the wave continues until a call to noTone()
• noTone(pinNo)
Stops the generation of a square wave triggered by tone().
pinNo = 0 .. 13 , A0 ..A5
Advanced Input/Output (2)
Serial Data Input/Output

• shiftOut(dataPin, clockPin, bitOrder, value )


dataPin = 0 .. 13 , A0 ..A5 the pin on which to output each bit
clockPin = 0 .. 13 , A0 ..A5 the pin to toggle once the dataPin has
been set to the correct value
bitOrder = MSBFIRST or LSBFIRST
value = the data to shift out (byte)
• byte shiftIn(dataPin, clockPin, bitOrder )
dataPin = 0 .. 13 , A0 ..A5 the pin on which to input each bit
clockPin = 0 .. 13 , A0 ..A5the pin to toggle to signal a read from
dataPin
bitOrder = MSBFIRST or LSBFIRST

Syntax:
byte incoming = shiftIn(dataPin, clockPin, bitOrder)
Advanced Input/Output (3)
Reads a pulse width

• unsigned long pulseIn(pinNo, value )


unsigned long pulseIn(pinNo, value, duration )

Reads a pulse width (either HIGH or LOW) on a pin. For


example, if value is HIGH, pulseIn() waits for the pin to go HIGH,
starts timing, then waits for the pin to go LOW and stops timing.
Returns the length of the pulse in microseconds. Gives up and
returns 0 if no pulse starts within a specified time out.

pinNo = 0 .. 13, A0 .. A5
value = type of pulse to read: either HIGH or LOW.
duration = the number of microseconds to wait for the pulse to start;
default is one second (unsigned long)
Time
• unsigned long millis()
Returns the number of milliseconds since the Arduino board
began running the current program. This number will overflow
(go back to zero), after approximately 50 days.
• unsigned long micros()
Returns the number of microseconds since the Arduino board
began running the current program. This number will overflow
(go back to zero), after approximately 70 minutes.
• delay(ms)
Pauses the program for the amount of time (in miliseconds)
ms = 1 .. 4294967295 mili Second
• delayMicroseconds(us)
Pauses the program for the amount of time (in microseconds)
us = 1 .. 4294967295 micro Second
Math (1)
• min(x,y)
Calculates the minimum of two numbers.
• x: the first number, any data type
• y: the second number, any data type
Return : The smaller of the two numbers.
Really is a macro definition as :
#define min(x,y) ((x) < (y) ? (x) : (y))
• max(x,y)
Calculates the maximum of two numbers.
• x: the first number, any data type
• y: the second number, any data type
Return : The larger of the two numbers.
Really is a macro definition as :
#define max(x,y) ((x) > (y) ? (x) : (y))
Math (2)
• abs(x)
Computes the absolute value of a number.
• x: the number, any data type
Return : x: if x is greater than or equal to 0.
-x: if x is less than 0.
Really is a macro definition as :
#define abs(x) ((x) > 0 ? (x) : (-x))
• constrain(x,a,b)
Constrains a number to be within a range.
• x: the number to constrain, all data types
• a: the lower end of the range, all data types
• b: the upper end of the range, all data types
Return : x: if x is between a and b
a: if x is less than a
b: if x is greater than b
Really is a macro definition as :
#define constrain(x,a,b) ((x<a) ? a : (x>b) ? b : x)
Math (3)
• double pow(value, exponent)
Calculates the value of a number raised to a power. Pow() can
be used to raise a number to a fractional power. x: the first
number, any data type
• value : the number (float)
• exponent : the power to which the base is raised (float)
Return : The result of the exponentiation (double)
• double sqrt(x)
Calculates the square root of a number.
• x: the number, any data type
Return : the number's square root (double)
Math (4)

• map(value, fromLow, fromHigh, toLow, toHigh)


Re-maps a number from one range to another. That is, a value of
fromLow would get mapped to toLow, a value of fromHigh to toHigh,
values in-between to values in-between, etc. x: the first number, any
data type
• value: the number to map
• fromLow: the lower bound of the value's current range
• fromHigh: the upper bound of the value's current range
• toLow: the lower bound of the value's target range
• toHigh: the upper bound of the value's target range
Return : The mapped value.
Trigonometry
• double sin(rad)
Calculates the sine of an angle (in radians). The result will be
between -1 and 1.
• rad: the angle in radians (float)
Return : the sine of the angle (double)
• double cos(rad)
Calculates the cosine of an angle (in radians). The result will be
between -1 and 1.
• rad: the angle in radians (float)
Return : the cpsine of the angle (double)
• double tan(rad)
Calculates the tangent of an angle (in radians). The result will be
between negative infinity and infinity.
• rad: the angle in radians (float)
Return : the tangent of the angle (double)
Random Numbers
• randomSeed(seed)
randomSeed() initializes the pseudo-random number generator,
causing it to start at an arbitrary point in its random sequence. This
sequence, while very long, and random, is always the same.
If it is important for a sequence of values generated by random() to
differ, on subsequent executions of a sketch, use randomSeed() to
initialize the random number generator with a fairly random input,
such as analogRead() on an unconnected pin.
• seed : pass a number to generate the seed (long int)
• long int random(min,max)
• long int random(max)
The random function generates pseudo-random numbers.
• min - lower bound of the random value, inclusive (optional)
• max - upper bound of the random value, exclusive
Return : a random number between min and max-1 (long int)
Bits and Bytes(1)
• byte lowByte(x)
Extracts the low-order (rightmost) byte of a variable (e.g.
a word).
• x: a value of any type
Return : low-order (rightmost) byte
• byte highByte(x)
Extracts the high-order (leftmost) byte of a word (or the
second lowest byte of a larger data type)
• x: a value of any type
Return : high-order (leftmost) byte
Bits and Bytes(2)
• byte bitRead(x, n)
Reads a bit of a number.
• x: the number from which to read
• n: which bit to read, starting at 0 for the least-
significant (rightmost) bit
Return : the value of the bit (0 or 1).
• bitWrite(x, n, b)
Writes a bit of a numeric variable.
• x: the numeric variable to which to write
• n: which bit of the number to write, starting at 0 for
the least-significant (rightmost) bit
• b: the value to write to the bit (0 or 1)
Bits and Bytes(3)
• bitSet(x, n)
Sets (writes a 1 to) a bit of a numeric variable.
• x: the numeric variable whose bit to set
• n: which bit to set, starting at 0 for the least-significant
(rightmost) bit
• bitClear(x, n)
Clears (writes a 0 to) a bit of a numeric variable.
• x: the numeric variable whose bit to clear
• n: which bit to clear, starting at 0 for the least-significant
(rightmost) bit
• byte bit(n)
Computes the value of the specified bit (bit 0 is 1, bit 1 is 2, bit 2 is
4, etc.).
• n: the bit whose value to compute
Return : the value of the bit
External Interrupts(1)
• attachInterrupt(interrupt, function, mode)
Specifies a function to call when an external interrupt occurs.
Replaces any previous function that was attached to the interrupt.
Most Arduino boards have two external interrupts: numbers 0 (on
digital pin 2) and 1 (on digital pin 3). Anjuran INPUT_PULLUP
• interrupt: the number of the interrupt (int)
• function: the function to call when the interrupt occurs; this
function must take no parameters and return nothing. This
function is sometimes referred to as an interrupt service
routine.
• mode: defines when the interrupt should be triggered. Four
contstants are predefined as valid values:
– LOW to trigger the interrupt whenever the pin is low,
– CHANGE to trigger the interrupt whenever the pin changes value
– RISING to trigger when the pin goes from low to high,
– FALLING for when the pin goes from high to low.
External Interrupts(2)

• detachInterrupt (interrupt)
Turns off the given interrupt.
• interrupt: the number of the interrupt to disable
(see attachInterrupt() for more details).
External Interrupts(3)
• interrupt (void)
Re-enables interrupts (after they've been disabled by noInterrupt()).
Interrupts allow certain important tasks to happen in the background
and are enabled by default. Some functions will not work while
interrupts are disabled, and incoming communication may be
ignored. Interrupts can slightly disrupt the timing of code, however,
and may be disabled for particularly critical sections of code.
• noInterrupt (void)
Disables interrupts (you can re-enable them with interrupts()).
Interrupts allow certain important tasks to happen in the background
and are enabled by default. Some functions will not work while
interrupts are disabled, and incoming communication may be
ignored. Interrupts can slightly disrupt the timing of code, however,
and may be disabled for particularly critical sections of code.
Serial Communication (1)
Setup Utilities
• if (Serial)
Indicates if the specified Serial port is ready. (Arduino 1.01 and
above)
• int Serial.available()
Get the number of bytes (characters) available for reading from
the serial port.
• Serial.begin(baud)
Initialize the Serial Port and sets the data rate in bits per second
(baud) for serial data transmission.
baud = 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800,
38400, 57600, or 115200
• Serial.end()
Disables serial communication, allowing the RX pin 0) and TX (pin
1) pins to be used for general input and output.
Serial Communication (2)
Output Functions
• Serial.print(data)
Prints data to the serial port as human-readable ASCII text. This
command can take many forms. Numbers are printed using an ASCII
character for each digit. Floats are similarly printed as ASCII digits,
defaulting to two decimal places. Bytes are sent as a single character.
data = any kind of data
• Serial.println(data)
This function takes the same forms as Serial.print(), except the text
followed by a carriage return character.
data = any kind of data
• Serial.write(data)
Writes binary data to the serial port. This data is sent as a byte or series
of bytes; to send the characters representing the digits of a number use
the print() function instead.
data = any kind of data
• Serial.flush()
Waits for the transmission of outgoing serial data to complete. (Prior to
Arduino 1.0, this instead removed any buffered incoming serial data.)
Serial Communication (3)
Input Functions (A)
• byte Serial.peek()
Returns the next byte (character) of incoming serial data without
removing it from the internal serial buffer.
• byte Serial.read()
Reads incoming serial data from the serial port into a buffer.
• Serial.readBytes(buffer, length )
Reads characters from the serial port into a buffer. The function
terminates if the determined length has been read, or it times out
buffer = the buffer to store the bytes in (char[] or byte[])
length = the number of bytes to read (int)
• Serial.readBytesUntil(character, buffer, length )
Serial.readBytesUntil() reads characters from the serial buffer
into an array. The function terminates if the terminator character
is detected, the determined length has been read, or it times out
character = the character to search for (char)
Serial Communication (4)
Input Functions (B)
• boolean Serial.find(target)
Find the serial buffer until the target string of given length is found. The
function returns true if target string is found, false if it times out.
target : the string to search for (char)
• Serial.findUntil(target, terminal )
Reads data from the serial buffer until a target string of given length or
terminator string is found.
target : the string to search for (char)
terminal : the terminal string in the search (char)
• Serial.setTimeout(time)
Serial.setTimeout() sets the maximum milliseconds to wait for serial
data when using Serial.readBytesUntil() or Serial.readBytes(). It defaults
to 1000 mS.
time : timeout duration in milliseconds (long).
Serial Communication (5)
Input Functions (C)

• float Serial.parseFloat()
Returns the first valid floating point number from the Serial
buffer. Characters that are not digits (or the minus sign) are
skipped. parseFloat() is terminated by the first character that is
not a floating point number.
• int Serial.parseInt()
Returns the first valid integer number from the Serial buffer.
Characters that are not digits (or the minus sign) are skipped.
parseInt() is terminated by the first character that is not a integer
number.

You might also like