舵机的函数
Servo类
网上代码
#include <Servo.h>//包含舵机头文件 Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; //舵机的旋转角度void setup() { myservo.attach(9); //设置舵机的接口,只有9,10 } void loop() { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(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 } }
常用
#include <Servo.h>Servo muservo;int position=0;void(){myservo.attach(9);}void loop(){myservo.write(position);}