Built in Function
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
Syntax:
byte incoming = shiftIn(dataPin, clockPin, bitOrder)
Advanced Input/Output (3)
Reads a pulse width
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)
• 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.