這是我期末的時(shí)候做的一個(gè)Arduino課程設(shè)計(jì)慈格,可以通過手機(jī)APP來控制呼吸燈亮滅及顏色變化眷茁。
主要實(shí)驗(yàn)器材:
- Arduino UNO開發(fā)板
- 共陽三色霧狀LED燈
- 面包板、普通LED燈、電阻佳魔、杜邦線若干
- HC-05藍(lán)牙模塊
- 支持藍(lán)牙功能的Android手機(jī)
HC-05藍(lán)牙模塊與Arduino連接方式
HC-05 VCC ----- Arduino VIN
HC-05 GND ----- Arduino GND
HC-05 TXD ----- Arduino RXD
HC-05 RXD ----- Arduino TXD
Android藍(lán)牙模塊
手機(jī)APP連接上HC-05后凭迹,在TextView中輸入要發(fā)送的字符串罚屋,定義s、c嗅绸、f脾猛、b分別為小LED燈開、關(guān)鱼鸠、和三色彩燈的開猛拴、關(guān)羹铅;通過藍(lán)牙串口將字符串發(fā)送出去。
Android藍(lán)牙通信程序關(guān)鍵代碼
- 打開藍(lán)牙:
// 獲取藍(lán)牙適配器
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//請(qǐng)求開啟藍(lán)牙
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
- 搜索設(shè)備:
// 將已配對(duì)的設(shè)備添加到列表中
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
mDevicesArray.add(device.getName() + "\n" + device.getAddress());
deviceList.add(device);
}
}
// 注冊(cè)廣播接收器愉昆,以獲取藍(lán)牙設(shè)備搜索結(jié)果
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
// 搜索藍(lán)牙設(shè)備
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 {
// 藍(lán)牙串口服務(wù)對(duì)應(yīng)的UUID职员。如使用的是其它藍(lán)牙服務(wù),需更改下面的字符串
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;
}
- 收發(fā)數(shù)據(jù)
mmInStream = socket.getInputStream();
mmOutStream = socket.getOutputStream();
//接收數(shù)據(jù)
byte[] buffer = new byte[1024];
try {
bytes = mmInStream.read(buffer);
} catch (IOException e) {
break;
}
//發(fā)送數(shù)據(jù)
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
共陽三色霧狀LED燈介紹
顧名思義它可以產(chǎn)生三種顏色跛溉,分別是紅焊切、綠、藍(lán)芳室,也可以同時(shí)調(diào)整三種顏色的亮度专肪,產(chǎn)生彩色呼吸燈的效果。
系統(tǒng)原理圖:
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的藍(lán)色引腳
void setup() {
Serial.begin(9600); //設(shè)置波特率9600
pinMode(small_led, OUTPUT); //設(shè)置為輸出模式
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_blue, OUTPUT);
analogWrite(small_led, 0); //首先堪侯,先把所有的燈都關(guān)閉
analogWrite(led_red, 255);
analogWrite(led_green, 255); //可以隨意設(shè)置開啟關(guān)閉哪些燈嚎尤,搭配出不同組合有不同顏色
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); //慢慢開啟紅色和綠色會(huì)搭配出黃色
analogWrite(led_green, i);
analogWrite(led_blue, 255);
delay(10);
}
for (int i = 0; i < 200; i++) //漸滅
{
analogWrite(led_red, 255); //慢慢關(guān)閉綠色和藍(lán)色會(huì)搭配出較鮮艷的藍(lán)色
analogWrite(led_green, i);
analogWrite(led_blue, i);
delay(10);
}
break;
case 'b':
analogWrite(led_red, 255); //關(guān)閉呼吸燈
analogWrite(led_green, 255);
analogWrite(led_blue, 255);
break;
}
Serial.println(data);
}
delay(10);
}
實(shí)際連接圖:
注意事項(xiàng):
- 防止LED燈被燒壞,在接5v電源的同時(shí)接上220Ω電阻伍宦。
- 在燒錄代碼的時(shí)候要把Arduino的TXD诺苹、RXD與HC-05的RXD、TXD斷開連接雹拄,否則會(huì)有干擾收奔,代碼燒錄不進(jìn)去。