前段时间我开始着手做一个项目,需要用到蓝牙模块来控制多个舵机,但是我搜索了很多网站、找了很多代码但是运行起来都有各种各样的问题,无法满足我的运行需求,最后我整理了一下我所知较简单的arduino蓝牙控制多个舵机的代码,现在分享给大家。
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(1, 0); // TX, RX pins of Bluetooth module
// Servo library
#include <Servo.h>
Servo servo1;
Servo servo2;
//Servo servo3;
// ...
void setup() {
Bluetooth.begin(9600); // Bluetooth serial communication baud rate
servo1.attach(4); // Servo 1 connected to pin 4
servo2.attach(5); // Servo 2 connected to pin 5
//servo3.attach(6); // Servo 3 connected to pin 6
// ...
}
void loop() {
if (Bluetooth.available()) {
char command = Bluetooth.read();
if (command == 'a') {
servo1.write(0);
servo2.write(0);
//servo3.write(0);
// ...
} else if (command == 'b') {
servo1.write(90);
servo2.write(90);
//servo3.write(90);
// ...
} else if (command == 'c') {
servo1.write(180);
servo2.write(180);
//servo3.write(180);
// ...
}
}
}
用蓝牙模块需要安装一个蓝牙调试器,