连续旋转伺服(arduino)响应按钮按下

时间:2013-09-05 00:23:13

标签: arduino

如果按下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
  }
}

1 个答案:

答案 0 :(得分:3)

看起来您的项目将受益于使用Hardware Interrupts,它会在事件(如按钮按下)发生时异步调用函数(这些对于控制器来说是完美的,并且可以消除轮询的开销)。

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

enter image description here

硬件中断实际上会中断代码,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参考页面的链接:

click here to learn more about arduino hardware interrupts