Arduino 的 main.cpp 程序内容如下:

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}


Arduino 多文件管理可以解决程序较大的问题。
Arduino 程序可以有多个源代码文件,但只有 1 个主文件,即存在 setup,loop 函数的 .ino 文件。
为了方便演示,我们让主文件来控制程序的主逻辑,具体的细节则封装成单个模块,存放在其他文件中。

方法1:直接添加(不推荐)

下面介绍如何创建其他的单个模块的文件。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

方法2:传统的C/C++分离式文件(推荐,但需要具备C++知识)

这种方式对于一个代码模块,我们需要两个文件,源文件(.c / .cpp)和头文件(.h)。
如果是 .c 和 .h 的组合方式,则是 C 语言风格。
如果是 .cpp 和 .h 的组合方式,则是 C++ 风格。
官方推荐 C++,因此我们学习并使用 C++ 风格来举例。

封装之前我们先创建文件结构,包含两个文件:LED.h 和 LED.cpp

在这里插入图片描述
在这里插入图片描述
之后我们需要明确我们的控制功能。规定功能后,先写出头文件(LED.h),然后写出实现(LED.cpp),最有在主文件(LED.ino)中使用这个模块即可。

/*******************
LED.h
*******************/

#ifndef _LED_H__
#define _LED_H__

//导入Arduino核心头文件
#include"Arduino.h"  

class LED
{
     private:
          byte pin;        //控制led使用的引脚
     
     public:
          
          LED(byte p , bool state=LOW );   //构造函数
          ~LED();          //析构函数
          byte getPin();   //获取控制的引脚
          void on();      //打开LED
          void off();     //关闭LED
          bool getState();  //获取LED状态
          void disattach(); //释放引脚与LED的绑定,使得引脚可以控制其他的东西
};

#endif
/*****************
LED.cpp

******************/

#include"LED.h"
#include"Arduino.h"

LED::LED(byte p,bool state):pin(p)
{
   pinMode(pin,OUTPUT);
   digitalWrite(pin,state);
}

LED::~LED()
{
    disattach();
} 

void LED::on()
{
    digitalWrite(pin,HIGH);
}

void LED::off()
{
   digitalWrite(pin,LOW);
}

bool LED::getState()
{
    return digitalRead(pin);
}

void LED::disattach()        //引脚回收,恢复到上电状态
{
    digitalWrite(pin,LOW);
    pinMode(pin,INPUT);
}
/**********************实例化1个LED对象,用7号叫控制,让他闪烁10次,并在串口打印出它的状态。10次完毕后释放回收引脚**********************/
#include"LED.h"

LED led(7);
byte count =0;

void setup() {
   Serial.begin(9600);
}

void loop() {
   if(count<10)   {
     led.on();
     delay(300);
     Serial.print("LED state:");Serial.println(led.getState(),DEC);
     
     led.off();
     delay(300);
     Serial.print("LED state:");Serial.println(led.getState(),DEC);
     
     ++count;
     if(count==10)
        led.disattach();
   }
}

/*
  Arduino.h - Main include file for the Arduino SDK
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef Arduino_h
#define Arduino_h

#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>

#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#include "binary.h"

#ifdef __cplusplus
extern "C"{
#endif

void yield(void);

#define HIGH 0x1
#define LOW  0x0

#define INPUT 0x0
#define OUTPUT 0x1
#define INPUT_PULLUP 0x2

#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398
#define TWO_PI 6.283185307179586476925286766559
#define DEG_TO_RAD 0.017453292519943295769236907684886
#define RAD_TO_DEG 57.295779513082320876798154814105
#define EULER 2.718281828459045235360287471352

#define SERIAL  0x0
#define DISPLAY 0x1

#define LSBFIRST 0
#define MSBFIRST 1

#define CHANGE 1
#define FALLING 2
#define RISING 3

#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  #define DEFAULT 0
  #define EXTERNAL 1
  #define INTERNAL1V1 2
  #define INTERNAL INTERNAL1V1
#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  #define DEFAULT 0
  #define EXTERNAL 4
  #define INTERNAL1V1 8
  #define INTERNAL INTERNAL1V1
  #define INTERNAL2V56 9
  #define INTERNAL2V56_EXTCAP 13
#else  
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
#define INTERNAL1V1 2
#define INTERNAL2V56 3
#else
#define INTERNAL 3
#endif
#define DEFAULT 1
#define EXTERNAL 0
#endif

// undefine stdlib's abs if encountered
#ifdef abs
#undef abs
#endif

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define round(x)     ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
#define sq(x) ((x)*(x))

#define interrupts() sei()
#define noInterrupts() cli()

#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )

#define lowByte(w) ((uint8_t) ((w) & 0xff))
#define highByte(w) ((uint8_t) ((w) >> 8))

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitToggle(value, bit) ((value) ^= (1UL << (bit)))
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))

// avr-libc defines _NOP() since 1.6.2
#ifndef _NOP
#define _NOP() do { __asm__ volatile ("nop"); } while (0)
#endif

typedef unsigned int word;

#define bit(b) (1UL << (b))

typedef bool boolean;
typedef uint8_t byte;

void init(void);
void initVariant(void);

int atexit(void (*func)()) __attribute__((weak));

void pinMode(uint8_t pin, uint8_t mode);
void digitalWrite(uint8_t pin, uint8_t val);
int digitalRead(uint8_t pin);
int analogRead(uint8_t pin);
void analogReference(uint8_t mode);
void analogWrite(uint8_t pin, int val);

unsigned long millis(void);
unsigned long micros(void);
void delay(unsigned long ms);
void delayMicroseconds(unsigned int us);
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode);
void detachInterrupt(uint8_t interruptNum);

void setup(void);
void loop(void);

// Get the bit location within the hardware port of the given virtual pin.
// This comes from the pins_*.c file for the active board configuration.

#define analogInPinToBit(P) (P)

// On the ATmega1280, the addresses of some of the port registers are
// greater than 255, so we can't store them in uint8_t's.
extern const uint16_t PROGMEM port_to_mode_PGM[];
extern const uint16_t PROGMEM port_to_input_PGM[];
extern const uint16_t PROGMEM port_to_output_PGM[];

extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];

// Get the bit location within the hardware port of the given virtual pin.
// This comes from the pins_*.c file for the active board configuration.
// 
// These perform slightly better as macros compared to inline functions
//
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
#define analogInPinToBit(P) (P)
#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )

#define NOT_A_PIN 0
#define NOT_A_PORT 0

#define NOT_AN_INTERRUPT -1

#ifdef ARDUINO_MAIN
#define PA 1
#define PB 2
#define PC 3
#define PD 4
#define PE 5
#define PF 6
#define PG 7
#define PH 8
#define PJ 10
#define PK 11
#define PL 12
#endif

#define NOT_ON_TIMER 0
#define TIMER0A 1
#define TIMER0B 2
#define TIMER1A 3
#define TIMER1B 4
#define TIMER1C 5
#define TIMER2  6
#define TIMER2A 7
#define TIMER2B 8

#define TIMER3A 9
#define TIMER3B 10
#define TIMER3C 11
#define TIMER4A 12
#define TIMER4B 13
#define TIMER4C 14
#define TIMER4D 15
#define TIMER5A 16
#define TIMER5B 17
#define TIMER5C 18

#ifdef __cplusplus
} // extern "C"
#endif

#ifdef __cplusplus
#include "WCharacter.h"
#include "WString.h"
#include "HardwareSerial.h"
#include "USBAPI.h"
#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL)
#error "Targets with both UART0 and CDC serial not supported"
#endif

uint16_t makeWord(uint16_t w);
uint16_t makeWord(byte h, byte l);

#define word(...) makeWord(__VA_ARGS__)

unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
void noTone(uint8_t _pin);

// WMath prototypes
long random(long);
long random(long, long);
void randomSeed(unsigned long);
long map(long, long, long, long, long);

#endif

#include "pins_arduino.h"

#endif


C++分离式编译


C++类构造函数/析构函数

构造函数

类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。
构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值。

析构函数

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。
析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。


自写的小车运行库

/*******************
UGV.h
*******************/

#ifndef _UGV_H__
#define _UGV_H__

//导入Arduino核心头文件
#include"Arduino.h"  

class UGV
{
  private:
    byte LF;        //控制led使用的引脚
    byte LB;        //控制led使用的引脚
    byte RF;        //控制led使用的引脚
    byte RB;        //控制led使用的引脚
     
  public:     
    UGV(byte IN1, byte IN2, byte IN3, byte IN4);   //构造函数
    ~UGV();          //析构函数
    void forward(byte LSp, byte Rsp);
    void backward(byte LSp, byte Rsp);
    void turnright(byte LSp, byte Rsp);
    void turnleft(byte LSp, byte Rsp);
    void parking();
};

#endif
/*****************
UGV.cpp

******************/

#include"UGV.h"
#include"Arduino.h"

UGV::UGV(byte IN1, byte IN2, byte IN3, byte IN4):LF(IN1), LB(IN2), RF(IN4), RB(IN3)
{
   pinMode(LF,OUTPUT);
   pinMode(LB,OUTPUT);
   pinMode(RF,OUTPUT);
   pinMode(RB,OUTPUT);
}

UGV::~UGV()
{
//    disattach();
} 

void UGV::forward(byte LSp, byte RSp)
{
  analogWrite(LF, LSp); analogWrite(LB, 0);
  analogWrite(RF, RSp); analogWrite(RB, 0);
}

void UGV::backward(byte LSp, byte RSp)
{
  analogWrite(LF, 0); analogWrite(LB, LSp);
  analogWrite(RF, 0); analogWrite(RB, RSp);
}

void UGV::turnleft(byte LSp, byte RSp)
{

  analogWrite(LF, LSp); analogWrite(LB, 0);
  analogWrite(RF, 0); analogWrite(RB, RSp);
}

void UGV::turnright(byte LSp, byte RSp)
{
  analogWrite(LF, 0); analogWrite(LB, LSp);
  analogWrite(RF, RSp); analogWrite(RB, 0);
}

void UGV::parking()
{
  analogWrite(LF,0); analogWrite(LB, 0);
  analogWrite(RF,0); analogWrite(RB, 0);
}
#include"UGV.h"   // 自己编写的库文件,记得导入 Arduino 安装目录

UGV UGV(6, 9, 10, 11);

void setup() {
  delay( 1000 ); // power-up safety delay
}

void loop() {
  UGV.forward(255, 255); delay(1000); 
  UGV.parking(); delay(1000); 
  
  UGV.backward(255, 255); delay(1000); 
  UGV.parking(); delay(1000);   
  
  UGV.turnleft(255, 255); delay(1000);
  UGV.parking(); delay(1000); 
  
  UGV.turnright(100, 100); delay(1000);
  UGV.parking(); delay(1000); 
}

Ref:
[1] 如何编写自己的Arduino库?
[2] 【C++】C++中的分离式编译
[3] C++ 类构造函数 & 析构函数