Arduino Functions

Input and output functions

pinMode (pin, mode)
the number of bits pin (digital pin) designated as input or output.
Such as: pinMode (7, INPUT); // The pin 7 is set to the input mode

digitalWrite (pin, value)
the number of bits specified pin is on or off. Pin must explicitly take effect as input or output mode can digitalWrite through pinMode.
Example: digitalWrite (8, HIGH); // set the pin 8 outputs the high potential

int digitalRead (pin)
the input value read pin, the pin is sensed at a high potential always return HIGH, otherwise back Biography LOW.
Example: val = digitalRead (7); // read values pin 7 and assigned to the Val

int analogRead (PIN)
read analog voltage pin and return a value of 0 to 1023 represent the corresponding 0-5 the voltage value.
Example: val = analogRead (0); // read analog value of bit 0 and pin assigned to the val variable

analogWrite (pin, value)
to change the voltage value of the PWM output pin, the pin will typically 3,5,6, 9, 10 and 11. Value 0-255 variables, for example: an output voltage of 2.5 volts (V), which value is approximately 128.
Example: analogWrite (9,128); // output voltage of about 2.5 volts (V)

unsigned Long the pulseIn (PIN, value) is LOW or HIGH value
set duration pin reading state, for example, an infrared, an acceleration sensor measured at a particular value, in a time unit does not change state.
Example: time = pulsein (7, HIGH ); // set state pin 7 is held at the unit of time is HIGH

shiftOut (dataPin, clockPin, bitOrder, value)
the data transmitted to the digital register for extending output, using the function information indicates a pin, a clock pin expressed. bitOrder representation for movement between bits (LSB LSBFIRST or MSBFIRST MSB), the final value is output as a byte. This function is typically used in extending the output digits.
Example: shiftOut (dataPin, clockPin, LSBFIRST , 255);

 


Time function
of time during the execution of the control and calculation wafer

unsigned long millis ()
return the wafer to the current begin msec
example: duration = millis () - lastTime ; // represents from "lastTime" to the time of day

delay (ms)
how many milliseconds wafer suspend execution
example: delay (500); // half a second pause (500 ms)


Delay Microseconds (US)
to suspend execution wafer number microseconds
Example: delayMicroseconds (1000); // 1 millisecond pause


Mathematics function
trigonometric functions and basic mathematical operations

min (x, y)
return between two smaller number
example: val = min (10,20); // return 10


max (X, Y)
return two numbers larger by
example: val = max (10,20); // return 20 is


ABS (X)
return the absolute value of the number, the number of positive and negative numbers.
Example: val = abs (-5); // return. 5


constrain (x, a, B)
determining a state x variables positioned between a and b. If a return is less than x a; between a and b x return itself; b is greater than b return
Example: val = constrain (analogRead (0 ), 0, 255); // Ignore 255 is greater than the number of


map ( value, fromLow, fromHigh, toLow, toHigh)
the variable value in accordance with fromHigh fromLow range, the conversion to a peer and toHigh tolow range. Often used in the read analog signal, converts the program to the desired range of values.
For example: val = map (analogRead (0 ), 0,1023,100, 200); // read the analog0 to convert the signal to a peer 100-- value between 200.


double pow (base, exponent)
returns a number (base) index value (exponent).
Example: double x = pow (y, 32); // set x to y is 32 power


double sqrt (x)
return patterns taking the square root double value.
Example: double a = sqrt (1138) ; // return the approximation of the square root of 1138 33.73425674438


Double SiN (RAD)
return trigonometric sine value of the angle (radians) of.
Example: double sine = sin (2) ; // approximation .90929737091


Double COS (RAD)
return angle (radians) trigonometric cosine values.
Example: double cosine = cos (2) ; // approximation -0.41614685058


Double Tan (RAD)
return angle (radians) tangent trigonometric function value.
Example: double tangent = tan (2) ; // approximation -2.18503975868


random number function (random number generation)

the randomSeed (SEED)
In fact in the random number in the Arduino can be predictable. So if you need a truly random number, you can call this function to reset generate random number seed. You can use the random number as a random number seed to ensure that the numbers in a random manner appear, usually using analog input as a random number seed, whereby you can generate with the relevant number of random environment (for example: radio waves, cosmic laser lines, telephone and electromagnetic wave) emitted from the fluorescent lamp.
Examples: randomSeed (analogRead (5)) ; // use the analog input as a random number seed

Long Random (max)
Long Random (min, max)
return the specified range of random number, type is long. If you do not specify a minimum value, default is 0.
Examples: long randnum = random (0, 100); // return 0 - number between 99
Long randnum = Random (11); // return a number between 0-10

serial communication


Serial.begin (speed)
you can specify the Arduino from the computer message exchange rate, we usually use 9600 bps. Of course, you can also use other speed, but usually no more than 115,200 bps (bytes per second).
Examples: Serial.begin (9600);
Serial.print (the Data)
Serial.print (the Data, encoding)

via serial port to transmit data, providing the option of encoding. If not specified, the default general text transmission.
Examples: Serial.print (75); // print out the "75"
Serial.print (75, DEC); // print out the "75"
Serial.print (75, HEX); // ". 4B" (75 the hex)
Serial.print (75, the OCT); // "113" (75 in the octal)
Serial.print (75, BIN); // "1001011" (75 binary)
Serial.print ( 75, BYTE); // "K " ( a byte transmitted, displayed in ASCII encoding)


Serial.println (the data)
Serial.println (the data, encoding)
and Serial.print () the same, but at the end of data plus newline characters (). Meaning as after you hit some information on the keyboard and press Enter.

Examples: Serial.println (75); // print out the "75"

Serial.println(75, HEX); // "4B "
Serial.println(75, OCT); // "113 "
Serial.println(75, BIN); // "1001011 "
Serial.println(75, BYTE); // "K "

int Serial.available ()
return the number of bytes (bytes) of data has not been read () function reads, if the return value is 0 on behalf of all the serial port data have been read read () function .
Example: int = Serial.available COUNT ();


int Serial.read ()
reads 1byte
Example: Data = Serial.read int ();

Serial.flush () refresh buffer data
Example: Serial.flush ();

Guess you like

Origin www.cnblogs.com/qxybk/p/12152665.html