【问题标题】:Servo Speed Control伺服速度控制
【发布时间】:2021-07-03 19:32:11
【问题描述】:

我怎样才能让我的伺服器做到这一点但速度较慢?

if(angle == 140)

角度 = 159;

其他

如果(角度 == 159)

角度 = 140;

这是整个代码:

    #include <Servo.h>

const int TOUCH_SENSOR_PIN   = 6;   // Arduino pin connected to touch sensor's pin
const int TOUCH_SENSOR_PIN2   = 7 ;
const int SERVO_PIN          = 9;   // Arduino pin connected to servo motor's pin

Servo servo; // create servo object to control a servo

// variables will change:
int angle = 140;           // the current angle of servo motor
int lastTouchState;      // the previous state of touch sensor
int currentTouchState;   // the current state of touch sensor
int lastTouchState2;      // the previous state of touch sensor 2
int currentTouchState2;   // the current state of touch sensor 2


void setup() {
  Serial.begin(9600);      // initialize serial
  pinMode(TOUCH_SENSOR_PIN, INPUT);
  pinMode(TOUCH_SENSOR_PIN2, INPUT);
  servo.attach(SERVO_PIN);       // attaches the servo on pin 9 to the servo object

  servo.write(angle);
  currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
  currentTouchState2 = digitalRead(TOUCH_SENSOR_PIN2);
}

void loop() {
  lastTouchState    = currentTouchState;  // save the last state
  currentTouchState = digitalRead(TOUCH_SENSOR_PIN);  //read new state 
  
  lastTouchState2    = currentTouchState2;  // save the last state
  currentTouchState2 = digitalRead(TOUCH_SENSOR_PIN2);  //read new state 

  if((lastTouchState == LOW && currentTouchState == HIGH) || (lastTouchState2 == LOW && currentTouchState2 == HIGH)) {

    if(angle == 140)
      angle = 159;
    else
    if(angle == 159)
      angle = 140;


    // control servo motor arccoding to the angle
    servo.write(angle);
    

  }  }

【问题讨论】:

    标签: arduino arduino-uno servo


    【解决方案1】:

    你可以通过让它旋转几个小步来控制伺服的速度,而不是给它最终的需求角度,就像这样。

      if(angle <= 140){
        for (; angle<= 150; angle += 1) {
          myservo.write(angle);
          delay(15);
        }
      }
      else if(angle >= 150){
        for (; angle >= 140; angle -= 1) {
          myservo.write(angle);
          delay(15);
        }
      }
    
    

    您可以通过更改两个 for 循环中的延迟值来调整速度。 一个完整的工作示例可以在 Arduino IDE 在 Servo -> Sweep I guess 下提供的示例中找到。

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 2017-10-25
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      相关资源
      最近更新 更多