2.1 Startup process

Generally speaking, our C language program starts with a main function, but in the previous tutorial we found that there are only two functions, setup and loop, in the file generated by the IDE, so how does Arduino call them?

In fact, the real main function lives in our Arduino library file (located in Arduino->main.cpp), which is defined as follows:

int main(void)
{
// do some hardware and variable initialization work
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
// call the setup() function we wrote
setup() ;
    
for (;;) {
// call the loop() function we wrote
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}

You can see that the two functions we wrote setup and loop will be called in main. Of course, how the related files are organized and compiled is the function provided by the Arduino tool chain. We will not do in-depth understanding here. In the beginner stage, we only care about how to use it.

2.2 Commonly used functions

Arduino provides a variety of functions for us to use, the details can be found in the Arduino API manual.

Don't memorize related functions, make good use of IDE's intelligent completion and search engine, you can get started quickly

Next, we briefly introduce several commonly used functions by explaining the above lighting code:

void setup(): Initialize related pins and variables

When the program runs in Arduino, the setup() function will be called first , which is used to initialize variables, set the output/input type of pins, configure serial ports, import class library files, and so on. The setup function only runs once each time the Arduino is powered up or restarted, for example:

void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED port to output mode
}

After that, the loop() function will be executed. As the name implies, this function will loop continuously during the running of the program until the chip is powered off.

void loop()
{
delay(300); // wait for 300ms
digitalWrite(LED_BUILTIN, HIGH);// built-in LED output high level, turn on the light
delay(300);
digitalWrite(LED_BUILTIN, LOW);// built-in LED output low level, off, etc.
}

The code in the loop turns the lights on/off every 300ms to achieve the effect of flashing lights. The following is a detailed explanation of the constants and functions used:

constant

HIGH | LOW: indicates the level of the digital IO port, HIGH indicates a high level (1 means the output voltage is "lit"), LOW means a low level (0 means no output voltage "off")

INPUT | OUTPUT: indicates the direction of the digital IO port, INPUT indicates the input (high-impedance state, that is, the input voltage signal can be read as if the resistance is very large), and OUTPUT indicates (the output voltage signal)

Digital I/O

pinMode(pin, mode): The definition function of the input and output mode of the digital IO port, the parameter mode can be INPUT or OUTPUT

digitalWrite(pin, value): The function of defining the output level of the digital IO port, the parameter value can be HIGH or LOW, which can be used to light up the LED

int digitalRead(pin): digital IO port read input level function, the return value is HIGH or LOW, which can be used to read digital sensors

Note: The range of parameter pin value is 0~13, which refers to 14 pins.

time function

delay(ms): delay function (unit ms)

The above are common functions, you don’t need to remember to have an impression, and you will remember them when you use them frequently later.