对于我的项目,我正在尝试制作一个由两个伺服器控制的翻页器-一个用于翻转(带喇叭),另一个用于抬起(带轮)。我的目标是,当我按下“向右”按钮时,方向盘应先顺时针旋转一页,然后喇叭会从左向右滑动(与“左”按钮相同但相反)以翻页。
当我尝试调整角度时,我的右按钮舵机可以按照我想要的方式工作,但它却发抖,另一方面,我的右按钮舵机可以平滑连续地工作,但是翻转和转弯两者同时处于活动状态。
#include <Servo.h>
const int swipservo = 8; //yellow
const int liftservo = 9; //green
Servo servo1; // create servo object to control a servo
Servo servo2;
const int leftbutton = 11; //yellow = turn left
const int rightbutton = 12; //blue = turn right
int pos1 = 0; // variable to store the servo position
int pos2 = 0;
void setup() {
servo1.attach (swipservo);
servo2.attach (liftservo);
pinMode(leftbutton , INPUT);
pinMode(rightbutton , INPUT);
}
void loop() {
test();
}
void test() {
if (digitalRead(rightbutton) == HIGH) {
for (pos2 = 0; pos2 < 350; pos2++) {
if (pos2 < 180) {
servo2.write (pos2);
delay(15);
}
if (pos1 > 180) {
servo1.write(0);
delay(15);
servo2.write(0);
delay(15);
}
else if (pos2 >= 180) {
servo1.write(pos1);
delay(15);
pos1 ++;
}
//go back to 0 for POS 2
}
}
if (digitalRead(leftbutton) == HIGH) {
for (pos2 = 160; pos2 > 0; pos2++) {
if (pos2 < 0) {
servo2.write (pos2);
delay(5);
}
if (pos1 >= 180) {
servo1.write(0);
delay(15);
servo2.write(0);
delay(15);
}
else if (pos2 > 0) {
servo2.write(pos2);
delay(15);
}
if (pos2 <= 0) {
servo1.write(pos1);
delay(15);
pos1 ++;
}
}
}
}