當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。