我正在使用一个应用程序,要么打开一个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;发布于 2017-12-04 06:12:36
不可能使用数字引脚0或1作为输入:
如前所述,这些都是串行发送和接收引脚。如果您通过USB为您的计算机供电,如果您试图使用它们,它可能会干扰,因为它是从USB读取到串行和引脚。此外,当您尝试编程时,您也会遇到任何相关的问题(嗯,不是任何事,而是为了安全起见将其移除)。如果您使用的引脚按预期,并程序,然后运行电池,它应该是没有问题的。 我们大多数人都远离它,因为这有点麻烦
根据您的Arduino模型,servo.attach只支持9和10引脚。
注意,在Arduino 0016和更早版本中,Servo库只支持两个引脚上的伺服:9和10。
或者你还是可以用上其中的一个。
https://stackoverflow.com/questions/47626763
复制相似问题