0

如果按下按钮,我试图使连续旋转伺服顺时针移动,如果按下按钮,pin2则逆时针移动pin3。我希望伺服器继续按照按钮设置的方向移动,直到释放按钮。这是我到目前为止的代码(我是arduino的新手):

#include <Servo.h>

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

// CONSTANTS

    // PINS
    const int crServo = 12; // sets pin 12 as servo
    const int buttonPinCW = 2; // sets pin 2 as button; CW => clockwise => FOCUS FAR
    const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR
    const int ledPin = 10; // sets pin 10 as LED

    // SERVO PROPERTIES
    const int crSpeedDefault = 1500; // 1500 is the stay still position, motor should not turn
    const int crSpeedCW = 1300; // 1300 turns the motor full speed clockwise
    const int crSpeedCC = 1700; // 1700 turns the motor full speed counter-clockwise
    const int crStepDefault = 2;

// SET BUTTON STATES
    int buttonStateCW = 0; //sets button 1 as off
    int buttonStateCC = 0; // sets button 2 as off

void setup()
{
    myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
    pinMode (buttonPinCW, INPUT); // sets button as input
    pinMode (buttonPinCC, INPUT); // sets button as input
    pinMode (ledPin, OUTPUT); // sets led as output
    myservo.write(crSpeedDefault); // default servo to crSpeedDefault
}

int slowFocusPull(int x){
  int result;
  result = abs(x - crSpeedDefault) / crStepDefault;
  return result;
}

void loop()
{
    buttonStateCW = digitalRead(buttonPinCW);
    buttonStateCC = digitalRead(buttonPinCC);
    // clockwise rotation
    if (buttonStateCW == HIGH) {
        digitalWrite(ledPin, HIGH);
        myservo.write(slowFocusPull(crSpeedCW));
    // counterclockwise rotation
    } else if (buttonStateCC == HIGH) {
        digitalWrite(ledPin, HIGH);
        myservo.write(slowFocusPull(crSpeedCC));
    } else {
        digitalWrite(ledPin, LOW);
    }
}

问题在于功能slowFocusPull。基本上我只是希望能够通过speed修改常数来调整。没有这个功能一切正常。


更新:最终循环供参考

void loop()
{
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);
  // clockwise rotation
  if (buttonStateCW == HIGH) {
    digitalWrite(ledPinR, HIGH);
    float speed = crSpeedCW;
    Serial.print("CLOCKWISE-ROTATION \n");
    for (int i = 0; i < t * 5; i++) {
      speed += ((float)crSpeedDefault - speed)/ 10;
      Serial.print(speed);
      Serial.print("\n");
      myservo.write((int)speed);
      delay(100);
    }
    myservo.write(crSpeedCW);
  } 
  else if (buttonStateCC == HIGH) {
      digitalWrite(ledPinG, HIGH);
      float speed = crSpeedCC;
      Serial.print("COUNTER-CLOCKWISE-ROTATION \n");
      for (int i = 0; i < t * 5; i++) {
        speed += ((float)crSpeedDefault - speed) / 10;
        Serial.print(speed);
        Serial.print("\n");
        myservo.write((int)speed);
        delay(100);
      }
      myservo.write(crSpeedCC);
    } 
  else {
    myservo.write(crSpeedDefault);
    digitalWrite(ledPinR, LOW); 
    digitalWrite(ledPinG, LOW);     // turn the LED off by making the voltage LOW
  }
}
4

1 回答 1

4

看起来您的项目将受益于使用Hardware Interrupts,当事件(如按钮按下)发生时异步调用函数(这些非常适合控制器,并消除轮询的开销)。

尝试连接两个引脚并将按钮连接到引脚 2 和 3,如下图所示:

在此处输入图像描述

硬件中断实际上会中断代码,uno 有两个这样的引脚:数字引脚 2 和数字引脚 3(这对机器人技术非常有用,mega 也有 6 个这样的引脚!)

这是您的代码可能想要的外观的骨架

void setup() {
  attachInterrupt(0, goClockwise, RISING); //the "0" places arduino uno's interrupt pin 2 (for uno r3)
  attachInterrupt(1, goCounterClockwise, RISING); //the "1" places interrupt for arduino uno's pin 3
  }

void loop() {
  delay(1000); dummy delay, code is handled in interrupt functions
}

void goClockwise () {
 //runs when pin 2's button is pressed
 //code for making servo go clockwise
}

void goCounterClockwise () { 
 //code triggered when pin 3's button is pressed
 //code for ccw goes here
}

如果您有任何问题,我很乐意与您一起解决问题。

这是硬件中断的 Arduino 参考页面的链接:

单击此处了解有关 arduino 硬件中断的更多信息

于 2014-01-29T19:54:12.810 回答