Advanced Path (Basic)-011 Arduino API Basics Brochure

Source: Internet
Author: User
Tags first string goto one more line square root

Arduino function API

Program Structure

In Arduino, the standard program entry main function is internally defined, and the user only needs to be concerned with the following two functions:
void Setup ()
void Loop ()
The setup () function is used for initialization, and the loop () function is used for execution. Initialization functions are generally placed at the beginning of the program, to set some pin output/input mode, to initialize the serial communication and other similar work. The code in the loop () function is looped, for example: read-in PIN status, set PIN output status, and so on.

Control statements
If,if...else,for,switch,while,do...while,break,continue,return,goto

Related syntax
; (semicolon), (curly braces),//(single-line comment),/* */(Multiline comment), #define, #include,

Arithmetic operators
= (Assignment), + (plus),-(minus), * (multiply),/(except),% (modulo)

Comparison operators
= = (equals),! = (not equal to),< (less than),> (greater than), <= (less than equals), >= (greater than or equal)

Boolean operator
&& (Logic and), | | (logical OR),! (Logical non)


Pointer operators

* Pointer operator & address operator


Bit arithmetic

& (Bit and), | (bit or), ^ (bitwise XOR), ~ (bitwise NON), << (left), >> (right), port operation


Compound operators

+ + (self-added) 、--(auto-subtract), + = (compound plus),-= (compound minus), *= (composite multiplication),/= (compound), &= (compound and), |= (composite or)


Constant

High | Low
INPUT | OUTPUT
true | False
Integer constants
Floating-point constants
Data type

void (no type)
Word (Word Group) 0~65535//16-bit
Boolean (Boolean) 0~1//1-bit
char (character type) -128~+127//8-bit
unsigned char (unsigned character type) 0~255//8-bit
short-32768~+32767//16-bit
Byte (byte type)//8bit
int (integral type) -2147483648~+2147483647//32-bit
unsigned int (unsigned integer) 0~65535//16-bit
Long (length) -2147483648~+2147483647//32-bit
unsigned long (unsigned length) 0~4297967295//32-bit
Float (single precision) -3.4028235e+38~+3.4028235e+38
Double (dual precision)
String (String)
Array (arrays)


Data type conversions

char (), Byte (), int (), Word (), long (), float ()


Variable Scope & Modifiers

Variable scope

Static variable, volatile (volatile variable), const (immutable variable)


Accessibility tools

sizeof () (sizeof operator), ASCII code table

Basic functions
/************** Digital i/o*************/
Pinmode (Pin,mode); Setting the PIN output status
The Pinmode function is used to configure the PIN as an input or output mode, which is a function with no return value, the function has two parameter pins and the Mode,pin parameter represents the PIN to be configured, the mode parameter represents the set pattern--input (input mode), output (out mode) and Input_pullup (input mode, and pin point position pull high);

Digitalwrite (Pin,value); Setting the output data of the PIN
The purpose of the Digitalwrite function is to set the PIN output pin pin to a high level or ground level. The function is a function with no return value, the function has two parameter pin and the Value,pin parameter represents the PIN to be configured, the value parameter represents the output voltage--high (high point level) and low (level).

int Digitalread (PIN); Pin Data Read

The Digitalread function is used when the function is input, can get the pin voltage condition--high (high level) or low (lower level), the parameter pin parameter represents the PIN to get the voltage value, the function returns the value of type int, which indicates the voltage condition of the pin.


/************* Analog i/o*************/
Analogread (PIN); Read the voltage value of the pin
The Analogread function is used to read the analog voltage value of the pin, which consumes 100us each time it is read. The parameter pin parameter represents the PIN for which the analog voltage value is to be obtained, and the function returns a value of type int that represents the analog voltage value, in the range 0~1023.

Analogwrite (Pin,value); Set PIN output analog (PWM)
By specifying the PIN output analog (PWM), it can be used to enable the LEDs to illuminate at different brightness or to drive the motor to rotate at different speeds.
At the end of the Analogwrite () output, the pin will produce a stable special PWM waveform until the next call to Analogwrite (or the same pin using Digitalread () or Digitalwrite ()).
The frequency of the PWM signal is approximately 490Hz.
On most Arduino boards (ATmega168 or ATmega328), only pins 3,5,6,9,10 and 11 can achieve this function. On the Aduinomega, the pin 2~13 can implement this function. Older Arduino versions of the board (ATMEGA8) only 9,10,11 can use Analogwrite ().
You do not need to use Pinmode () to set the pin before using Analogwrite (). The Analogwrite function is not directly related to the analog pin, Analogread function.

Analogreference (type);//Set analog reference voltage
The function of the analogreference is to configure the reference voltage of the pin. After the pin obtains the analog voltage value in the embedded application, the analog value is converted to the 0~1023 according to the reference voltage. The function is no return value function, refer to the mode type, there are 3 types (default/internal/external), the specific meaning is indicated below.
Nefault: Pre-set, reference voltage 5V
INTERNAL: Low voltage mode with bias reference. The internal voltage reference of the
Internal1v1:1v (Arduinomega only)
internal2v56:56v (can only be used in Arduinomega)
EXTERNAL: Extended mode, The reference voltage is obtained through the Aref pin
Actual voltage = (Analogread (PIN) * reference voltage)/1024;


/************* Advanced i/o************/
Shiftout (Datapin, Clockpin, Bitorder, Val); To move a data out of this
The purpose of this function is to move the characters in a character group of one set of data out of a character group at a time. Start with the most significant bit (leftmost) or least significant bit (rightmost). Each character is written to the data pin in turn, and the clock pin is pulled higher or lower to indicate that the data just now is valid.
Note: If the linked device clock type is a rising edge, be sure that the clock pin is low before calling Shiftout (), such as calling Digitalwrite (Clockpin,low).
This is a feature of software implementation.
Arduino provides a hardware-implemented ISP library that is faster, but only works on specific pins.
Datapin: Data output pin, each character of the data will be output successively. Pin mode needs to be set to output.
Clockpin: Clock output pin to provide clock for data output, pin mode needs to be set to output.
Bitorder: Data Displacement Order selection character, this parameter is byte type, there are two types can be selected, namely high-first-entry msbfirst and low-first-in Lsbfirst
Val: Data for all outputs
The Shiftout function can output the data serially in the PIN, which is equivalent to the synchronous serial communication in the general sense, which is a common communication mode between controller and controller, controller and sensor.

Pulsein (PIN, state, timeout); Pulse duration on the read pin

Pin: The PIN number of the Read pulse
State: The level status of the pulse of the read pin
Timeout: Maximum read duration (UM)

/************ Time Function ************/
Millis ();
The Millis function can be obtained by returning a value, returning the length of time it takes to run the machine, in Ms. The system's maximum recording time is 9 hours and 22 minutes, if the time out is 0. The function returns a value of unsigned long with no arguments.

Delay (MS);
The delay function is a delay function, used in the Blink program, the parameter represents the extended time of the set, Unit Ms.

Delaymicroseconds (US);
The Delaymicroseconds function is a delay function, and the parameter represents the extended time of the setting, in units of us.


/************** Math Library ************/
Min (x, y); Returns the smaller of the two parameters.
Max (x, y); Returns the larger value of two parameters.
ABS (x); Returns the absolute value of the parameter.
Constrain (AMT, low, high); Interval
If Amt<low, the value of low is returned.
If Amt>high, the value of high is returned.
If Low<amtMap (x, In_min, In_max, Out_min, Out_max); Interval mapping
The function of the map function is to map the x ratio within the [In_min,in_max] range to the [Out_min,out_max] range. This means that the function can be converted from the original scale range to another scale range.
X: the number judged.
In_min: The smallest value of the original number range.
In_max: The largest value of the original number range.
Out_min: The smallest value after the calculated range.
Out_max: The maximum number of values after the calculated range.

Pow (base, exponent); Seeking the exponent of base
sqrt (x); Find square root


/************ Trigonometric ***********/
Sin (RAD); Sinusoidal
cos (RAD); Cosine
Tan (RAD); Tangent


/************** Random number ************/
Randomseed (seed); Generates a pseudo-random number. (Call the Random function)
Long myrandomnumber = random (max);
Random (Howbig); Generates a pseudo-random number.
The random function returns an arbitrary number between 0~howbig-1. Analog noise signals can be made.

Random (Howsmall, howbig);

The/********* string (string) **********/
CharAT (); Gets the nth character of a string
String1.charat (n); String is the head pointer of a string

CompareTo (); Comparison of two strings
The CompareTo function compares two strings, and returns the difference of two strings that are currently compared to a string. Front-Rear
Syntax: String1.compareto (string2);
String1: First string
string2: Second string

Concat (); string concatenation
String1.concat (string2); Stitch the string2 behind the string1.

EndsWith (); String tail judgment Comparison
String1.endswith (string2); Determine if the string1 tail is a string string2, and of course you can use it to judge ' \ n '

Equals (); Determines whether strings are equal.
String1.equals (string2);
String1: The string to be judged
string2: Judging the string
The return value is a Boolean type, True and False

Equalslgnorecase (string2); To interpret whether the string ports are equal.
String1.equalslgnorecase (string2);
String1: The string to be judged
string2: Judging the string
The return value is a Boolean type, True and False

GetBytes (); The copy of the string and the function ToCharArray () function are very familiar.
String1.getbytes (Buf,len);
String1: The original string
BUF: The target variable to be moved
Len: String length

IndexOf (); Selects a specific character in the string and returns the position of the function function. (forward)
If you want to find this particular keyword in a very long string, you can use this function.
String1.indexof (Val);
String1.indexof (Val,from);
String1: The original string
Val: The keyword that you want to find can be a char or string.
From: Optional parameter, you can specifically specify from that location to start looking for this keyword.
Return value: Return position successfully, failure return-1

Lastlndexof (); Selects a specific string in the string and returns the location of the function function. Reverse
String1.lastlndexof (Val);
String1.lastlndexof (Val,from);
String1: The original string
Val: The keyword that you want to find can be a char or string.
From: Optional parameter, you can specifically specify from that location to look back for this keyword.
Return value: Return position successfully, failure return-1

Length (); Measure character length
String1.length ();
String1: The string to be measured.
Return value: The length of the string.

Replace (); String substitution
String1.replace (STRING2,STRING3);
String1: the original string.
String2: The string to be replaced in the string.
String3: The new string to replace.
return value: None

Setcharat (); Character substitution
String1.setcharat (I,charl);
String1: the original string.
I: The position of the character in the string to be replaced.
Charl: To replace the character, note that there is only one character, not a string.
return value: None

StartsWith (); Determines whether a string has started with a particular string
String1.starts with (string2);
String1: the original string.
String2: Judging is not the beginning of this string.
Return value: Boolean algebra, True and False

SUBSTRING (); Used to intercept a position in the middle of a string. In addition is the decision there to end.
String1.substring (from); Returns the
String1.substring (from,to); Return between the From and to
String1: The original string
From: The string to intercept starts with the first word.
To: intercept the words that follow.
Return value: The returned string.

ToCharArray (); Copy the string into char
ToCharArray function is a common function in string processing, you can think of him as a conversion function of string to char[], or a function of copying text, his function and getBytes () very similar, ToCharArray () function is converted to render text, The GetBytes () function converts the number to be rendered.
String1.tochararray (Buf,len);
String: the original strings.
BUF: The location of the specified char[], note the space of char[], one to be equal to or greater than the size of the copy, otherwise the memory and the program will produce unexpected problems.
Len: The length of the string to copy.
Return value: The length of the string to copy.

ToInt (); String Goto INT
String1.toint ();
String1: string, such as "123"
Return value: integer, such as 123

toLowerCase (); Convert all English to lowercase
String1.tolowercase ();
String1: string (all in English)
return value: string (lowercase)

toUpperCase (); Convert all English to uppercase
String1.touppercase ();
String1: string (all in English)
return value: String (uppercase)

Trim (); Automatically clears the front and back blank data in the string.
String1.trim ();
String1: the original string.
return value: None.

/************** bit Operation ************/
Lowbyte ();
Highbyte ();
Bitread ();
Bitwrite ();
BitSet ();
Bitclear ();
Bit ();

/********* Set Interrupt function ***********/
Attachinterrupt (interrupt, function, mode); Interrupt Setup function
The Attachinterrupt function is used to set an external interrupt, which has 3 parameters: Interrupt, function, mode, respectively, represent the interrupt source, interrupt handler function, and trigger mode. Parameter options are selectable values of 0 or 1, which generally correspond to 2nd and 3rd pins in the Arduino; The parameter interrupt handler is used to specify the processing function in the middle, the parameter value is the function pointer,
There are 4 modes of triggering:
Low (lower level trigger),
Change (triggered when changing),
RISING (low point level to high level),
Falling (High point level to low level trigger).
Attention:
The delay function is not available in the interrupt function.
The use of the Millis function always returns the value before entering the break.
If the serial data is read, it may be lost.
The variables used in the break function need to be defined as volatile types.
Detachinterrupt (interrupt); Cancel Interrupt
Interrupt is the source of the interrupt to be canceled.

/*********** Switch Interrupt ************/
Interrupts (); New-enabled nointerrupts off interrupts
Nointerrupts (); Turn off interrupts that have been turned on


/*********** Serial Communication ************/
Serial.begin (speed); Set the communication rate of the serial port
Speed:int integer that is used to set the transfer rate, usually the number of these: 300,600,1200,2400,4800,9600,14400,19200,28800,18400,57600 or 115200
return value: None

Serial.available (); Gets the amount of data for the front-port buffer pool. The buffer of the serial port is only 64 bytes by default, but the size can be changed, for details see: http://www.cnblogs.com/jikexianfeng/p/6250564.html
Serial.read (); Used to read data and only one character can be read at a time.
The Serial.available function and the Serial.read function are usually used together, and the serial.available function is used to determine if there is data to be sent in, and we then use the Serial.read function to read the data.
Serial.flush (); Emptying the buffer data, the function of the Serial.flush () function is not the same as the version before and after arduino1.0. The previous version is used to clear the input buffer has entered the serial port data, after 1.0 version, to wait for the serial port data to be sent complete. Details: http://www.cnblogs.com/jikexianfeng/p/6250638.html

Serial.print (data); Serial output
SERIAL.PRINTLN (data); Serial output (one more line break than the tail of print.) )

/************ Extension Library ************/
If you want to use some existing libraries, you can select the menu "Sketch->import Library" and select the appropriate library. The sketch tool will itself include the header file (#include) of the library at the beginning of the code.
After the library is introduced, the volume of the program will also increase (because of the code that contains the library). For libraries that are not necessary, you can delete the corresponding # include statements directly.
Official Expansion Gallery

The following is the official Arduino extension library, which is included in the development tool by default.
EEPROM-Read and write EEPROM storage.
Ethernet-Ethernet support.
Firmata-Serial Port-based communication protocol.
LIQUIDCRYSTAL-LCD LCD display.
SERVO-Servo motor.
Softwareserial-Software implementation of the serial port, you can choose any pin.
Stepper-Stepper motor.
WIRE-I2C protocol support.
matrix-led display array control.
Sprite-led shows the control of the array neutron block.
Third-party extension libraries

This contains only the relevant libraries for the Roboduino Extension Board, and other library information is available at: Arduino online documentation online~.
Roboduino Expansion Board:
Roboduinomotor-roboduino Motor Expansion Board drive.
If you need to design your own library, refer to the code for the relevant library in the "Arduino\hardware\libraries" directory.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////

Reference book: Http://blog.csdn.net/powenko/article/details/48570043#plain

Reference Document: https://www.douban.com/note/493189178/

Reference website: http://wiring.org.co/reference/

Reference page: http://www.cnblogs.com/jikexianfeng/p/6031291.html

Reference page: http://www.cnblogs.com/rayray/p/3198091.html

Reference page: http://www.cnblogs.com/jikexianfeng/p/6250638.html

Advanced Path (Basic)-011 Arduino API Basics Brochure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.