arduino基础-函数(arduino基础-函数).doc

  • 格式:doc
  • 大小:89.00 KB
  • 文档页数:11

arduino基础-函数(arduino基础-函数)Input output functionArduino contains a switching function of some processing input and output, is believed to have the book from a program example・PinMode (pin, mode)Specifies the digital foot (digital, pin) as input or output・Example:PinMode (7, INPUT); / / pin 7 is set as input modeDigitalWrite (pin, value)Specifies the number of pins to be on or off. The pin must firstbe passed through the pinMode as input or output mode, and the digitalWrite will take effect・Example:DigitalWrite (8, HIGH) ; / / set the high potential output pin 8Int digitalRead (PIN)Read the value of the input pin and return HIGH when it is sensed that the foot is at high potentia 1. Otherwise, it will return LOW.Example:Vai 二digitalRead (7) ; / / read pin 7 values and assigned to val Int analogRead (PIN)Read the voltage of the analog foot and return a value from 0 to 1023 to indicate the corresponding 0 to 5 voltage value・Example:Val 二analogRead (0) ; / / read analog pin 0 values and assigned to the variable valAnalogWrite (pin, value)Change the output voltage of the PWM pin, and the feet are usually at 3, 5, 6, 9, 10 and 11. The Value range is 0-255, for example, the output voltage is 2.5 volts (V), and the value is about 128.Example:AnalogWrite (9128); / / output voltage of about 2. 5 volts (V)Unsigned, long, pulseln (pin, value)Sets the duration of the read position of the foot, for example, when an infrared or accelerometer is used to measure a certain value, without changing the state in the time unit・Example:Time 二pulsein (7, HIGH) : / / set foot 7 state in the unit oftime is HIGHShiftOut (dataPin, clockPin, bitOrder, value)The data is passed to a register used to extend the digital output ・ The function uses one foot to represent the data and one foot to indicate the pulse・ BitOrder is used to represent the movement of bits (the lowest significant bit of LSBFIRST or the most significant bit of MSBFIRST), and finally the value output in byte form・ This function is usually used in the output of the extended digit・Example:ShiftOut (dataPin, clockPin, LSBFIRST, 255);Time functionControl and calculate the time during execution of the chipUnsigned, long, millis ()The return chip begins to execute to the current millisecondExample:Duration 二millis (-lastTime); / / said that since the〃lastTime〃the present timeDelay (MS)How many milliseconds does the pause chip execute?Example:Delay (500) ; / / suspended for half a second (500 milliseconds) Delay Microseconds (US)How many microseconds does the pause chip execute?Example:DelayMicroseconds (1000); / / suspend 1 milliseconds Mathematical functionTrigonometric functions and basic mathematical operationsMin (x, y)Return between the smallerExample:Vai 二min (10), 20); / / return 10Max (x, y)Return between largerExample:Vai 二max (10,20); / / return 20ABS (x)Returning the absolute value of the number, you can turn negative numbers into positive numbers・Example:Vai 二ABS (-5); / / return 5Constrain (x, a, b)Determine the state of the X variable between a and B・ X if less than a, return a; between a and B, return x itself; greater than B, return BExample:Vai 二constrain (analogRead (0), 0, 255); / / ignore a number greater than 255Map (value, fromLow, fromHigh, toLow, toHigh)Converts value variables to toLow and tolligh ranges in accordance with the fromLow and fromHigh ranges・ Often used to read analog signals and convert them to the range values required by the program.Such as:Vai = map (analogRead (0), 01023100, 200) ; / / the value analogO the read signal equivalence to 100 - 200.Double pow (base, exponent)An index (exponent) value that returns a number (base)・Example:Double x 二pow (y, 32); / / set X to 32 yDouble sqrt (x)Returns the square root value of the double type・Example:Double a 二sqrt (1138) ; / / return 33. 73425674438 approximation of the square root of 1138Double sin (RAD)The trigonometric function sine value of the return angle (radians)・Example:Double sine 二 sin (2) ; / / approximate value of 0. 90929737091Double cos (RAD)The trigonometric function cosine value of the return angle (radians)・Example:Double cosine 二cos (2); / / -0.41614685058 approximation Double Tan (RAD)The trigonometric function tangent value of the return angle (radians)・Example:Double tangent 二Tan (2) ; / / -2.185******** approximation Random number functionGenerating random numbersRandomSeed (seed)In fact, the random number in Arduino is predictable・ So if you need a really messy number, you can call this function to reset and generate a random seed・ You can use the random number as a random number seed, to ensure that the figures appear in a random way, usually use analog input as a random seed, which can produce environment related random number (e.g., radio, telephone, and cosmic ray ray fluorescent lamps emitted electromagnetic wave)・RandomSeed (analogRead (5)): / / use analog input as a random seedLong random (max)Long random (min, max)Returns the random number of the specified interval, the type is long. If no minimum is specified, the default is 0.Example:Long randnum 二random (0, 100); / / return number between 99 and 0 -Long randnum 二random (11) ; / / return number between 0 TO Sequence communicationIn the fifth chapter, you can see some examples of using serial ports to exchange messages with computers. Here is a functional explanation.Seria1. begin (speed)You can specify the rate at which Arduino can exchange messages from a computer, usually using 9600 bps・ Of course, you can use other speeds, but usually no more than 115200 BPS (bytes per second)・Seria1. begin (9600);Seria1. print (data)Seria1. print (data, encoding)Transmit data through serial ports, providing options for encoding ・ If not specified, the default is sent in plain text.Example:Seria1. print (75); / / print "75〃Seria1. print (75, DEC); / / print 〃75〃Seria1. print (75, HEX); / / (75 sixteen carry)Seria1. print (75, OCT); / / 〃113〃(75 in eight carry)Serial, print (75, BIN): / / 〃1001011〃(75 bit)Seria1. print (75, BYTE); / / 〃K〃(with byte transmission, display with ASCII encoding)Seria1. printin (data)Seria1. printin (data, encoding)Same as Seria1. print (), but a newline character is added at theend of the data・ It means that you press Enter after you have hit some data on the keyboard.Example:Seria1. printin (75); / / print "75〃Seria1. printin (75, DEC); / / print 〃75〃Seria1. printin (75, HEX); / / 4B〃Seria1. printin (75, OCT); / / 113 〃〃Seria1. printin(75, BIN); / / 1001011 〃〃Seria1. printin (75, BYTE); / / K〃Int, Seria 1. available ()Data returned by a number of bytes (bytes) has not been read by the read () function, and if the return value is 0, all data on the serial port have been read by the read () function.Example:Int count 二Seria1. available ();Int, Seria 1. read ()Read lbyte sequence dataExample:Int data 二Seria 1. read ();Seria 1. flush ()Sometimes you can use this function to clear the data in the buffer because the data is too fast to exceed the speed at which the data is processed by the program・ This function ensures that the data in the buffer (buffer) is up to date・Example:Seria 1. flush ();。