当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Arduino #include用法及代码示例


[更多语法]

说明

#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.cc大神的英文原创作品 #include。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。