根据蓝牙输入改变舵机角度
Change servo angle based on Bluetooth input
我正在使用一个应用程序根据按下的按钮(使用 Arduino)打开 LED 或改变微伺服的角度。我的代码适用于 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;
您cannot使用数字引脚0
或1
作为输入:
As mentioned, those are serial send and receive pins. If you power your computer through USB, it can interfere if you try to use them, since it's reading from both the USB to Serial and the pin. Also you have an issue with anything connected (well, not anything, but remove it just to be safe) when you're trying to program. If you use the pins as intended, and program then run it off battery, it should be no problem at all.
Most of us stay away from it because it's a bit of a hassle
根据您的 Arduino 型号,servo.attach
仅支持引脚 9
和 10
:
Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.
或者您也可以只使用其中之一。
我正在使用一个应用程序根据按下的按钮(使用 Arduino)打开 LED 或改变微伺服的角度。我的代码适用于 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;
您cannot使用数字引脚0
或1
作为输入:
As mentioned, those are serial send and receive pins. If you power your computer through USB, it can interfere if you try to use them, since it's reading from both the USB to Serial and the pin. Also you have an issue with anything connected (well, not anything, but remove it just to be safe) when you're trying to program. If you use the pins as intended, and program then run it off battery, it should be no problem at all.
Most of us stay away from it because it's a bit of a hassle
根据您的 Arduino 型号,servo.attach
仅支持引脚 9
和 10
:
Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.
或者您也可以只使用其中之一。