【Arduino】開發入門【八】舵機操作+源代碼
1.servo類成員函數
函數 |
說明 |
attach() |
設定舵機的接口,只有9或10接口可利用。 |
write() |
用於設定舵機旋轉角度的語句,可設定的角度范圍是0°到180°。 |
writeMicroseconds() |
用於設定舵機旋轉角度的語句,直接用微秒作為參數。 |
read() |
用於讀取舵機角度的語句,可理解為讀取最后一條write()命令中的值。 |
attached() |
判斷舵機參數是否已發送到舵機所在接口。 |
detach() |
使舵機與其接口分離,該接口(9或10)可繼續被用作PWM接口。 |
2.Arduino舵機實現代碼
// Sweep // by BARRAGAN <http://barraganstudio.com> // This example code is in the public domain. #include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for(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(pos = 180; pos>=1; 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 } }
3.Arduino舵機連接圖
實物圖中,舵機GND(棕色線)、VCC(紅色線)、Signal(橙色線)
【淘寶】【Arduino入門套件 Arduino UNO R3】
線路圖中,舵機GND(黑色線)、VCC(紅色線)、Signal(黃色線)
需要獲取源代碼的朋友,可以通過下面三種方式獲取
(0)下載地址 http://download.csdn.net/detail/fnext/5252736
(1)請在評論中填寫郵件地址,會通過郵箱發送源碼。
(2)直接拷貝上面的代碼
相關文章
【淘寶】【Arduino入門套件 Arduino UNO R3】
【Arduino】開發入門教程【一】什么是Arduino
【Ardunio】開發入門教程【二】數據類型
【Arduino】開發入門教程【三】Arduino開發工具
【Arduino】開發入門教程【四】Arduino驅動安裝
【Arduino】開發入門教程【五】Hello Arduino
【Arduino】開發入門教程【六】數據類型轉換函數
【Arduino】開發入門教程【七】7段顯示器實現+源代碼
【Arduino】開發入門教程【八】舵機操作+源代碼
//////////////////////////////////////////////
原創聲明 轉載請注明
本文出自 Ray-Ray的博客
文章地址 http://www.cnblogs.com/rayray/archive/2013/04/15/3015172.html
感謝大家的推薦和收藏
你的支持! 我們的動力!