舵机的函数

Servo类

image.png

网上代码

  1. #include <Servo.h>//包含舵机头文件
  2. Servo myservo; // create servo object to control a servo
  3. // a maximum of eight servo objects can be created
  4. int pos = 0; //舵机的旋转角度
  5. void setup()
  6. {
  7. myservo.attach(9); //设置舵机的接口,只有9,10
  8. }
  9. void loop()
  10. {
  11. for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
  12. { // in steps of 1 degree
  13. myservo.write(pos); //使舵机旋转
  14. delay(15); // waits 15ms for the servo to reach the position
  15. }
  16. for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
  17. {
  18. myservo.write(pos); // tell servo to go to position in variable 'pos'
  19. delay(15); // waits 15ms for the servo to reach the position
  20. }
  21. }

常用

  1. #include <Servo.h>
  2. Servo muservo;
  3. int position=0;
  4. void(){
  5. myservo.attach(9);
  6. }
  7. void loop(){
  8. myservo.write(position);
  9. }