|
这个例子中要演示一下如何利用BlueShield和Arduino一起配合,来达到遥控led小灯的效果。
我们准备直接使用Arduino编辑器自带的例子Examples->Communication->Dimmer,这个例子原本是利用串口输入数字,控制led的亮度。在这个基础上加上BlueShield,遥控就变得简单了。
准备材料
- microduino一个,这相当于一个缩小版的arduino uno
- Arduino的蓝牙模块BlueShield一块
- 发光二极管一个
- 220欧电阻一个
- 面包板一块
- 杜邦线若干
最重要的,你手上要有一个搭载了蓝牙4.0的设备比如iPhone 4S/5,iPad mini/4等等。
开始接线
由于MicroDuino和arduino uno是兼容的,所以图中以uno的图片代替。接线图如下
实际接线图
测试
- 下载软件LightBlue
- 写入测试程序Dimmer到Arduino
- 打开LightBlue,连上BlueShield,找到D3E60005-8F36-40D6-B2D5-C5D9F5E81869这个characteristic,输入hex例如ff,这是最亮,01是最暗的情况
演示视频
附上Dimmer的源码- const int ledPin = 9; // the pin that the LED is attached to
-
- void setup()
- {
- // initialize the serial communication:
- Serial.begin(9600);
- // initialize the ledPin as an output:
- pinMode(ledPin, OUTPUT);
- }
-
- void loop() {
- byte brightness;
-
- // check if data has been sent from the computer:
- if (Serial.available()) {
- // read the most recent byte (which will be from 0 to 255):
- brightness = Serial.read();
- // set the brightness of the LED:
- analogWrite(ledPin, brightness);
- }
- }
复制代码 |
|