这是我的代码:
#include <SoftwareSerial.h>
#include <Servo.h> // servo library
Servo servo1; // servo control object
Servo servo2;
Servo servo3;
SoftwareSerial bluetooth(5, 6); // RX, TX
int servo1Pin = 11;
int servo2Pin = 10;
int servo3Pin = 9;
int motor1pin = 8;
int flag = 0; //Sets integer for HIGH or LOW LED case
String bdata = "";
char c = ' ';
void setup()
{
bluetooth.begin(9600);
pinMode(motor1pin, OUTPUT);
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
servo3.attach(servo3Pin);
}
void loop()
{
int position;
while (bluetooth.available()) {
c = bluetooth.read();
bdata += c;
//~~~~~~Servo 1~~~~~~~~
if (bdata == "open1")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close1")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Servo 2~~~~~~~~
if (bdata == "open2")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo2.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close2")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo2.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Servo 3~~~~~~~~
if (bdata == "open3")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo3.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close3")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo3.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Motor 1~~~~~~
if (bdata == "on")
{
flag = 1; //Turns LEDs on using a high digital signal
}
if (bdata == "off")
{
flag = 0; //Turns LEDs off using a low digital signal
}
}
digitalWrite(motor1pin, flag);
delay(1000);
bdata = "";
}
它被上传到一个 Arduino Uno,在那里我通过面包板连接了 3 个伺服系统和直流电机。它旨在为我制作的 3D 打印机械臂提供动力。阅读时,您可以看到它也使用 HC-05 模块和手机通过蓝牙进行控制。我遇到了麻烦,因为伺服 1 的命令工作正常,3 也一样,但是当伺服 2 的命令从电话伺服 3 发送时,伺服 3 移动,伺服 3 命令也移动伺服 3。我已经尝试查看变量我手机上的命令毫无意义。所以我在这里。任何人都可以帮忙吗?