这是我期末的时候做的一个Arduino课程设计,可以通过手机APP来控制呼吸灯亮灭及颜色变化。
主要实验器材:
- Arduino UNO开发板
- 共阳三色雾状LED灯
- 面包板、普通LED灯、电阻、杜邦线若干
- HC-05蓝牙模块
- 支持蓝牙功能的Android手机
HC-05蓝牙模块与Arduino连接方式
HC-05 VCC ----- Arduino VIN
HC-05 GND ----- Arduino GND
HC-05 TXD ----- Arduino RXD
HC-05 RXD ----- Arduino TXD
Android蓝牙模块
手机APP连接上HC-05后,在TextView中输入要发送的字符串,定义s、c、f、b分别为小LED灯开、关、和三色彩灯的开、关;通过蓝牙串口将字符串发送出去。
Android蓝牙通信程序关键代码
- 打开蓝牙:
// 获取蓝牙适配器
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//请求开启蓝牙
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
- 搜索设备:
// 将已配对的设备添加到列表中
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
mDevicesArray.add(device.getName() + "\n" + device.getAddress());
deviceList.add(device);
}
}
// 注册广播接收器,以获取蓝牙设备搜索结果
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
// 搜索蓝牙设备
mBluetoothAdapter.startDiscovery();
// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
deviceList.add(device);
// Add the name and address to an array adapter to show in a ListView
mDevicesArray.add(device.getName() + "\n" + device.getAddress());
// Notify ListView to update
devicesListAdapter.notifyDataSetChanged();
}
}
};
- 匹配并建立连接
BluetoothSocket socket = null;
try {
// 蓝牙串口服务对应的UUID。如使用的是其它蓝牙服务,需更改下面的字符串
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
socket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (Exception e) {
Log.d("log", "获取Socket失败");
return;
}
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
socket.connect();
Log.d("log", "连接成功");
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
Log.d("log", "连接失败");
try {
socket.close();
} catch (IOException closeException) { }
return;
}
- 收发数据
mmInStream = socket.getInputStream();
mmOutStream = socket.getOutputStream();
//接收数据
byte[] buffer = new byte[1024];
try {
bytes = mmInStream.read(buffer);
} catch (IOException e) {
break;
}
//发送数据
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
共阳三色雾状LED灯介绍
顾名思义它可以产生三种颜色,分别是红、绿、蓝,也可以同时调整三种颜色的亮度,产生彩色呼吸灯的效果。
系统原理图:
Arduino源码:
#define small_led 8 //Pin 8 连接上小led灯正极
#define led_red 9 //Pin 9 连接上 led的红色引脚
#define led_green 10 //Pin 10 连接上 led的绿色引脚
#define led_blue 11 //Pin 11 连接上 led的蓝色引脚
void setup() {
Serial.begin(9600); //设置波特率9600
pinMode(small_led, OUTPUT); //设置为输出模式
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_blue, OUTPUT);
analogWrite(small_led, 0); //首先,先把所有的灯都关闭
analogWrite(led_red, 255);
analogWrite(led_green, 255); //可以随意设置开启关闭哪些灯,搭配出不同组合有不同颜色
analogWrite(led_blue, 255);
}
void loop() {
if (Serial.available() > 0)
{
char data = Serial.read();
switch (data)
{
case 's': analogWrite(small_led, 255); break; //小灯亮
case 'c': analogWrite(small_led, 0); break; //小灯灭
case 'f':
for (int i = 200; i > 0; i--) //渐亮
{
analogWrite(led_red, i); //慢慢开启红色和绿色会搭配出黄色
analogWrite(led_green, i);
analogWrite(led_blue, 255);
delay(10);
}
for (int i = 0; i < 200; i++) //渐灭
{
analogWrite(led_red, 255); //慢慢关闭绿色和蓝色会搭配出较鲜艳的蓝色
analogWrite(led_green, i);
analogWrite(led_blue, i);
delay(10);
}
break;
case 'b':
analogWrite(led_red, 255); //关闭呼吸灯
analogWrite(led_green, 255);
analogWrite(led_blue, 255);
break;
}
Serial.println(data);
}
delay(10);
}
实际连接图:
注意事项:
- 防止LED灯被烧坏,在接5v电源的同时接上220Ω电阻。
- 在烧录代码的时候要把Arduino的TXD、RXD与HC-05的RXD、TXD断开连接,否则会有干扰,代码烧录不进去。