Arduino study notes

1. Pin

Two, function introduction

1.pinMode(pin, mode)

The pin is configured as input or output, where mode can be INPUT or OUTPUT

For example:
pinMode(7, INPUT); // Define pin 7 as an input interface

2.digitalWrite(pin, value)

Turn on a value pin and assign it to high or low level. This pin must be the input or output mode defined above, otherwise digitalWrite will not take effect.

value can be HIGH or LOW

For example:
digitalWrite(8, HIGH); // Give high level to pin 8

3.digitalRead(pin)

Read the pin value of an input state. When the pin is in the high state, it returns HIGH, otherwise it returns LOW.

E.g:

val = digitalRead(7); // read the value of pin 7 and assign it to val

4.analogRead(pin)

Read the value of the analog input pin and express it as a value between 0 and 1023, corresponding to a voltage of 0 to 5V.

For example:
val = analogRead(0); // Read the value of analog interface 0 and assign it to val

5.analogWrite(pin, value)

Change the PWM output value of this pin, the pin can be 3, 5, 6, 9, 10, 11. The changing range of the PWM value is 0 to 255, and the corresponding voltage output value is 0 to 5V.

For example:
analogWrite(9, 128); // light up the LED on pin 9 to 50% brightness

6.Serial.begin(speed)

To prepare for communication with the Arduino serial port, we can detect the return value through the Arduino host computer software. Here we set the communication baud rate. We usually use 9600. We can also use other communication baud rates, but the maximum is 115200.
For example:
Serial.begin(9600);

7.Serial.print(data)
   Serial.print(data, encoding)

Send data back through the serial port, encoding indicates the type of data return, the default is in plain text format.
For example:
Serial.print(75); //display 75
Serial.print(75, DEC); //same as above
Serial.print(75, HEX); //"4B" (75's hexadecimal expression)
Serial. print(75,OCT); //"113" (the octal representation of
75 ) Serial.print(75, BIN); //"1001011" (the binary representation of
75 ) Serial.print(75, BYTE); //" K" (The ASCII code value of K is 75)

8.Serial.println(data)
   Serial.println(data, encoding)

Same as Serial.print(data), but add a newline character (\r\n) at the end of the returned data. The meaning of the newline character is equivalent to the enter key you type after typing some text.
For example:
Serial.println(75); //Display "75\r\n"
Serial.println(75, DEC); //Same as above
Serial.println(75, HEX); //"4B\r\n"
Serial .println(75,OCT); //"113\r\n"
Serial.println(75, BIN); //"1001011\r\n"
Serial.println(75, BYTE); //"K\r \n"

9.pulseIn(pin, value)

Read the pulse duration of a pin, for example using an infrared sensor or accelerometer. They are all sensors that use different pulses per unit time to obtain state values.
For example:
time = pulseIn(7,HIGH); // Read the high level time of pin 7

10.delay(ms)

Delay for a certain millisecond.
For example:
delay(500); // delay 500ms

11.delayMicroseconds(us)

Delay for a certain microsecond.
For example:
delayMicroseconds(1000); //Delay 1000us

12.min (x, y)

The minimum value of x and y is returned.
For example:
val = min(10, 20); // The value of val is 10

13.max(x, y)

The maximum value of x and y is returned.
For example:
val = max(10, 20); // the value of val is 20

14.abs(x)

Returns the absolute value of x. The absolute value of a positive number is itself, and the absolute value of a negative number is its opposite.

For example:
val = abs(-5); // The value of val is 5

15.map(value, fromLow, fromHigh, toLow, toHigh)

The value of value is converted to the range of toLow and toHigh according to the range of fromLow and fromHigh. It is usually used to read similar signals and convert them to the required range in the program.
For example:
val = map(analogRead(0), 0,1023, 100, 200); //Convert the value of 0~1023 read by analog interface 0 into a value of 0~100

16.pow(base, exponent)

Return the exponent value of a number (base)
For example:
double x = pow(y, 32); //make x be the 32th power of y

17.sqrt(x)

Go to the square root of x.
For example:
double a = sqrt(1138); // The square root of 1138 is 33.73425674438

18.sin (rad)

Returns the sine of an angle (in radians).
For example:
double sine = sin(2); // The sine of 2 radians is approximately 0.90929737091

There are cos(rad), tan(rad)...

shiftOut(dataPin, clockPin, bitOrder, value)

Send data to the shift register to expand the digital output range. When this function is used, one pin is used as data output, the other pin is used to represent the clock, and bitOrder is used to represent the byte format (LSBFIRST is the least significant bit, MSBFIRST is the most significant bit), value is to output the value of the byte.

例如:
shiftOut(dataPin, clockPin, LSBFIRST, 255);

millis()

Check the time from the start of program execution to the current time.
For example:
duration = millis()-lastTime; //Indicates the time from lastTime to the current

constrain(x, a, b)

Determine the relationship between x and a and b. If x is less than a, return a; if x is between a and b, return x itself; if it is greater than b, return b.
For example:
val = constrain(analogRead(0), 0, 255); // Ignore numbers greater than 255

randomSeed(seed)

Resetting the Arduino's random number generator will generate a series of random numbers. Although these numbers appear to be randomly generated, their order can actually be predicted. So if we need a real set of random numbers, we have to reset the seed of the random number. If we do not connect an analog value pin, it can obtain random noise from the surrounding environment (radio waves, cosmic rays, electromagnetic interference from mobile phones or fluorescent lights, etc.).
For example:
randomSeed(analogRead(5)); // Use the noise of pin 5

long random(max)
long random(min, max)

Returns a long integer random number in the specified interval. If no minimum value is specified, the default minimum value is 0.
For example:
long randnum = random(o, 100); //returns a number between 0 and 100
long randnum = random(11); //returns a number between 0 and 10

int Serial.available()

Return a value to tell the host computer how many bytes have not been read by the read() function. If the return value of Serial.available() is 0, it means that the serial data has been read by read().
For example:
int count = Serial.available();

int Serial.read()

Read one byte of serial data
For example:
int data = Serial.read();

Serial.flush()

Because the data transmission speed is greater than the Arduino program processing speed, the Arduino will store the data in the buffer first. If necessary, we can use the Serial.flush() function to clear the buffer to ensure that the data in the buffer is up to date.
For example:
Serial.flush();

Three, program operation

1. An Arduino program is divided into two parts:

void setup()

prevents the initialization of the Arduino program in this function, so that the main loop program can set relevant parameters before starting.

void loop()

This is the main function of Arduino. This set of procedures will be repeated until the power is disconnected.

 

2. Arduino language has some keywords with special meaning.

For example:

HIGH and LOW are used to indicate that you are on or off (high level or low level);

A pin of Arduino, INPUT and OUTPUT are used to set a specific pin to be an input interface or an output interface;

INPUT_PULLUP is used to set a pin as an input and is pulled high;

True and False, as their literal meanings; indicate whether a condition or an operation is true or false.

3. Grammar rules such as C language

Guess you like

Origin blog.csdn.net/seek97/article/details/87897043