简体   繁体   English

串行和Arduino伺服库的问题

[英]Issue with Serial and Arduino Servo Library

today I tried to setup a servo motor running along with this sketch on Arduino. 今天,我尝试在此Arduino上设置随此草图一起运行的伺服电机。 For some reason, when I add the setup lines for the servo, the software serial port doesn't receive data normally. 由于某种原因,当我添加伺服的设置线时,软件串行端口无法正常接收数据。 I tried to change the software serial to the normal Serial port on Arduino but the same thing happens. 我试图将软件串行更改为Arduino上的常规串行端口,但是发生了同样的事情。

#include<SoftwareSerial.h>
#include<Servo.h>

Servo myServo;
SoftwareSerial BT1(3,2); // TX, RX 

int val;
int servoPin = 7;
int ledPin = 10;

void setup() {
  pinMode(ledPin,OUTPUT);
  BT1.begin(9600);

  myServo.attach(servoPin);
  myServo.writeMicroseconds(2000); //2 ms for Tower Pro SG 90 Servo
  myServo.write(90); //Initialize at servo's middle point

}

void loop() {

  if (BT1.available()) {
    int i = (int)BT1.read();
    setIntensity(i);
  }

}

void setIntensity(int value) {
  if (value >= 0 && value <= 110) {
    analogWrite(ledPin, value);
  }
}

When I comment out the setup lines for myServo, the Arduino works like a charm. 当我注释掉myServo的设置行时,Arduino就像一个吊饰一样工作。 How can I solve this? 我该如何解决? I'm using an Arduino Duemilanove. 我正在使用Arduino Duemilanove。

NeoSWSerial is a suitable replacement for SoftwareSerial . NeoSWSerialSoftwareSerial的合适替代品。 NeoSWSerial won't interfere with Servo, because it doesn't use any extra resources, and it doesn't block interrupts for long periods of time. NeoSWSerial不会干扰Servo,因为它不会使用任何额外的资源,并且不会长时间阻塞中断。

It's also available from the IDE Library Manager, under the menu Sketch -> Include Library -> ManageLibraries . 也可以从IDE库管理器中的“草图”->“包含库”->“管理库”菜单中获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM