Chapter 2 Basics
Arduino language and program structure
Arduino language
- Arduino uses C/C++ language to write programs
- Generally speaking, the Arduino language refers to a collection of various reference program programming interfaces (APIs) provided by the Arduino core library files
- Enhance program readability and improve development efficiency
pinMode(13,OUTPUT);
digitalWrite(13,High);
- Here
pinMode
that is disposed pin mode, this pin 13 is set to the output mode; anddigitalwrite(13,HIGH)
is to make 13-pin output high digital signal.
Arduino program structure
- No main() function
- The definition of the main() function is hidden in the Arduino core library file.
void setup()
{
//在这里填写setup()函数代码,它只运行一次
}
void loop()
{
//在这里填写loop()函数代码,它会不断重复运行
}
- The basic structure of an Arduino program consists of two functions, setup() and loop().
- Setup()
- After the Arduino controller is powered on or reset, it will start to execute the program in the setup() function, and the program will only be executed once.
- Usually complete the Arduino's initial settings in the setup() function, such as configuring the I/O port status and initializing the serial port.
- loop()
- After the program in the setup() function is executed, Arduino will continue to execute the program in the loop() function. The loop () function is an infinite loop , in which the program will continue to run repeatedly.
- The main functions of the program are usually completed in the loop() function, such as driving various modules and collecting data.
C/C++ language foundation
#Only writing some differences, not the grammar part~
- Integer int type and unisgned int type occupy 4 bytes (32 bits)
- Floating point
- In Arduino, there are two floating-point types, float and double, but the precision of the two is the same, and both occupy 4 bytes (32 bits) of memory space.
- The operation of floating-point data is slow and has certain errors. Therefore, the floating-point data is usually converted to integer to process related operations. Such as 9.8cm, usually converted to 98mm to calculate.
- Character type
char col = 'c';
- Boolean
- Its value is only two false (false) and true (true), occupying 1 byte of memory space.
- String
char 字符串名称 [字符个数];
String 字符串名称;
- Using the String type to define a string will take up more storage space.
Electronic components and Arduino expansion modules
Breadboard
Use of the extended version of the sensor
Use of digital I/O
Digital signal
- High level is digital signal 1, and low level is digital signal 0.
- Every pin with a digital number on the Arduino is a digital pin, including analog input pins with a number "A". Use these pins to complete the function of input/output digital signals.
- Before using the output or output function, through the need to
pinMode()函数
configure the output pin or output mode to mode, i.e.,
pinMode(pin,mode);
Where pin is the pin number of the specified configuration, and the parameter mode is the specified configuration mode. Three modes can be used
Pattern name | Description |
---|---|
INPUT | Input mode |
OUTPUT | Output mode |
INPUT_PULLUP | Enter pull-up mode |
pinMode(13,OUTPUT)
Is to configure pin 13 as output mode
- After configured as output, it is also required
digitalWrite()函数
that the output pin is high or low.
digitalWrite(pin,value);
The parameter pin is the pin number of the specified output. The parameter value is the output level to be specified. Use HIGH to specify the output high level and LOW to specify the output low level.
- Digital processing an output signal pin, but also can be used
digitalRead()函数
to read the digital signal from the external input, it calls the form:
digitalReal(pin);
- In the Arduino core library, OUTPUT is defined as 1, INPUT is defined as 0, HIGH is defined as 1, and LOW is defined as 0. Therefore, these definitions can also be replaced by numbers. Such as:
pinMode(13,1);
digitalWrite(13,1);
/*
* Blink
* 等待一秒种,点亮LED,再等待一秒钟,熄灭LED,如此循环
*/
//在大多数Arduino控制板上,13号引脚都链接了一个标有“L”的LED灯
//给13号引脚链接的设备设置一个别名“led”
int led = 13;
//在板子启动或者复位重启后,setup部分的程序只会运行一次
void setup() {
// 将“led”引脚设置为输出状态
pinMode(led,OUTPUT);
}
// setup部分程序运行完后,loop部分的程序会不断重复运行
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
- Set an alias "led" to the device connected to pin 13. This writing method can improve the readability of the program and facilitate modification.