Arduino舵机控制
0赞
发表于 6/24/2016 2:20:11 PM
阅读(2204)
红色的是电源正极,黑色的是电源负极,白色的是信号线。有些舵机线是红棕橘三色,分别对应红黑白。
#include <Servo.h>
Servo myservo;
int pos = 90;
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
myservo.attach(9);
}
博客:http://www.cnblogs.com/xiaobo-Linux/
void loop() {
char val = Serial.read();
if(val=='z')
{
pos+=30; // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(150); // waits 15ms for the servo to reach the position
Serial.println(" servo_right");
}
if(val=='x')
{
pos-=30; // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(150); // waits 15ms for the servo to reach the position
Serial.println(" servo_left");
}

