我尝试使用arduino舵机库去控制舵机,里面俩个库文件一个是可以让用户输入角度去控制舵机,另一个是用电位器去控制旋转,请问一下如何可以将这两个程序放在一起,使得用户可以既输入角度角度去控制也能通过调节电位器进行控制。
#include <Servo.h>
#define PIN_SERVO 9 //用户输入角度
int potPin = A0;
int val;
Servo myservo;
void setup()
{
myservo.attach(9);
}
void loop()
// waits for the servo to get there
{
myservo.write(0);
delay(1000);
myservo.write(90);
delay(700);
myservo.write(0);
delay(1000);
exit(0);
}
#include <Servo.h>//电位器控制
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it for use with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
我试过直接把 void loop 里的程序放在一起,但是舵机只能自动旋转,不能用电位器旋转
你需要考虑使用情况。
如果用户输入 90,而电位器是 180,应该旋转多少?
或许可以考虑加一个 3 脚开关来选择是 用户输入 还是 电位器。