[更多语法]
说明
#include
用于在您的草图中包含外部库。这使程序员可以访问大量标准 C 库(一组预制函数),以及专门为 Arduino 编写的库。
AVR C 库的主要参考页面(AVR 是对 Arduino 所基于的 Atmel 芯片的参考)是 这里 。
请注意,#include
与 #define
类似,没有分号终止符,如果添加一个,编译器将产生神秘的错误消息。
用法
#include <LibraryFile.h>
#include "LocalFile.h"
参数
LibraryFile.h
:使用尖括号语法时,将在库路径中搜索文件。
LocalFile.h
: 当使用双引号语法时,文件所在的文件夹使用#include
指令将搜索指定的文件,如果在本地路径中找不到,则搜索库路径。将此语法用于草图文件夹中的头文件。
示例代码
此示例包含伺服库,因此其函数可用于控制伺服电机。
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (int pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
相关用法
- Arduino #define用法及代码示例
- Arduino long用法及代码示例
- Arduino Arduino_EMBRYO_2 - setLengthXY()用法及代码示例
- Arduino ~用法及代码示例
- Arduino ArduinoBLE - bleDevice.advertisedServiceUuidCount()用法及代码示例
- Arduino const用法及代码示例
- Arduino Ethernet - server.begin()用法及代码示例
- Arduino ArduinoBLE - BLEService()用法及代码示例
- Arduino digitalWrite()用法及代码示例
- Arduino ArduinoBLE - bleCharacteristic.subscribe()用法及代码示例
- Arduino Servo - attach()用法及代码示例
- Arduino write()用法及代码示例
- Arduino Arduino_LSM9DS1 - readGyroscope()用法及代码示例
- Arduino ArduinoSound - FFTAnalyzer.input()用法及代码示例
- Arduino MKRGSM - gprs.attachGPRS()用法及代码示例
- Arduino WiFiNINA - WiFi.config()用法及代码示例
- Arduino MKRGSM - sms.read()用法及代码示例
- Arduino MKRNB - getCurrentCarrier()用法及代码示例
- Arduino Scheduler - Scheduler.startLoop()用法及代码示例
- Arduino Arduino_LSM9DS1 - magneticFieldAvailable()用法及代码示例
- Arduino MKRWAN - available()用法及代码示例
- Arduino ArduinoBLE - BLE.poll()用法及代码示例
- Arduino ArduinoBLE - bleCharacteristic.hasDescriptor()用法及代码示例
- Arduino Ethernet - EthernetUDP.parsePacket()用法及代码示例
- Arduino WiFi101 - WiFi.subnetMask()用法及代码示例
注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 #include。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。