【问题标题】:Change servo angle based on Bluetooth input根据蓝牙输入改变舵机角度
【发布时间】:2017-12-04 22:39:33
【问题描述】:

我正在使用应用程序来打开 LED 或根据按下的按钮更改微伺服的角度(使用 Arduino)。我的代码适用于 LED(按下按钮时,LED 亮起),但是当我按下用于将伺服角度更改为 40 的按钮时没有任何反应。

// Bluetooth serial:
#include <SoftwareSerial.h> // import the serial library

// setup the bluetooth coms
SoftwareSerial BTSerial(8,7);

#include <Servo.h>

int servoPin = 0;

Servo servo;

int angle = 0; // servo position in degrees
int input = 0;
int led2 = 13;

void setup() {
  // put your setup code here, to run once:
  servo.attach(servoPin);
  Serial.begin(9600);   // coms w/ computer
  BTSerial.begin(9600); // coms w/ Bluetooth
  pinMode(led2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  if (BTSerial.available())
  {
    input = BTSerial.read();
    digitalWrite(led2, LOW);

    switch(input) {
      case 'E':
        angle = 40;
        break;

      case 'C':
        digitalWrite(led2, HIGH);
        break;
    }

    servo.write(angle);
  }
}

输入是正确的,因为我还打开了 LED,以防“E”正常工作。我也尝试过在 case 函数中使用servo.write(),但这也没有用。

case 'E':
   servo.write(40);
   break;

【问题讨论】:

  • 没有错误信息,伺服根本不动
  • 是的,我做了servo.attach,我试过Servo::refresh();但它给出了错误:“刷新”不是“伺服”的成员。
  • 你把它贴在哪个别针上?编辑您的问题以显示complete example
  • 附加到引脚 0
  • 我猜Arduino是通过USB连接到你的电脑的?

标签: bluetooth arduino servo


【解决方案1】:

cannot 使用数字引脚 01 作为输入:

如前所述,这些是串行发送和接收引脚。如果您通过 USB 为计算机供电,如果您尝试使用它们,它可能会受到干扰,因为它同时从 USB 到串行和引脚读取。当您尝试编程时,您还会遇到任何连接的问题(嗯,不是任何东西,但为了安全起见将其删除)。如果您按预期使用引脚,然后编程然后用电池运行,那应该没问题。

我们大多数人都远离它,因为它有点麻烦

根据您的 Arduino 型号,servo.attach 仅支持引脚 910

请注意,在 Arduino 0016 及更早版本中,Servo 库仅支持两个引脚上的舵机:9 和 10。

或者你也可以只使用其中之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 2020-10-14
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多