0

对于我的项目,我正在尝试制作一个由两个伺服系统控制的翻页器——一个用于翻转(带喇叭),一个用于提升(带轮子)。我的目标是当我按下 RIGHT 按钮时,我的滚轮应该首先顺时针滚动一页,然后我的喇叭将从左向右滑动(与我的 LEFT 按钮相同但相反)来翻页。

当我尝试调整角度时,我的按钮右舵机按我想要的方式工作,但它在晃动,另一方面,我的按钮右舵机工作平稳且连续,但翻转和转弯都处于活动状态同时。

#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 ++;
      }
    }
  }
}
4

0 回答 0