伺服转矩控制实例:伺服速度控制(arduino servo sweep)

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

if(angle == 140)

角度 = 159;

else

如果(角度 = = 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);
    
  }  }
2

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

  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 下提供的示例中找到-& gt;扫描我猜

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(424)
Cba薪资:使用SQL根据作业id编辑薪资(sql jobs salary)
上一篇
Hone star:Bloxorz a-Star搜索
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(5条)