我目前正在从事一个项目,该项目涉及使用蓝牙模块(HC-06)和一个应用程序控制连接到arduino uno微控制器的LED灯条。
基本设置是该应用程序由控制条带某些方面的按钮和滑块组成。发送给arduino的每次按摩均构造为以下字符串:
两个初始字符代表要控制的方面(例如bt表示亮度)
后跟一个“ +”号,指示发送的值的开头。
后跟一个值(此范围可以从单个数字到三个数字,以表示要在HSL到RGB转换中使用的0-255的值范围。
消息然后以“!”结尾指示消息结束。
我现在的问题是当我进行离散的按摩传输时,例如打开灯光或轻按亮度滑块上的某个点,一切正常。但是,一旦我在亮度滑块上滑动手指,蓝牙接收到的字符就会混杂在一起,从而导致值波动并遍及整个地方。
我尝试使用readString而不是逐个字符地接受按摩,但这只会使事情变得非常缓慢,尽管它可以正常工作,但它却无法实现在更改滑块时平滑过渡的目的。
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
// Which pin on the Arduino is connected to the NeoPixels?
#define LED_PIN 6
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 60
// Declare the NeoPixel strip and Bluetooth objects:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
SoftwareSerial BlueTooth(9, 10); // (TXD, RXD) of HC-06
// A dim curve that smoothes out the transition of color and brightness to the eye.
const byte dim_curve[] = {
0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11,
11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20,
20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35,
36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47,
48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82,
83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109,
110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255,
};
char input; // to store input character received via BT.
String data;
// getRGB function stores RGB values in this array
// use these values for the red, blue, green led.
int rgb_colors[3];
int hue = 255; // to store the current value of hue for the leds.
int saturation = 255; // to store the current saturation of the leds.
int brightness = 255; // to store the current brightness of the leds.
void setup()
{
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED).
strip.show(); // Turn OFF all pixels ASAP.
strip.setBrightness(100); // Set BRIGHTNESS to about 1/5 (max = 255).
BlueTooth.begin(9600); // Initialises the Bluetooth serial.
Serial.begin(9600);
}
void loop()
{
if (BlueTooth.available())
{
input=(BlueTooth.read());
if (input != '!') {
data += input;
}
else{
String message_id = String(data.substring(0,2)); //gets only the command.
data.remove(0,3); //removes two characters for command and the '+' sign.
int message_value = data.toInt();
if(message_id == "bt"){
brightness = message_value;
changeBrightness(brightness);
}
}
}
}
void changeBrightness(int aBright){
brightness = aBright;
getRGB(hue,saturation,brightness,rgb_colors);
setColor(strip.Color(rgb_colors[0],rgb_colors[1],rgb_colors[2]), false);
}
void changeColour(int h, boolean whipe){
hue = h;
getRGB(hue,saturation,brightness,rgb_colors);
setColor(strip.Color(rgb_colors[0],rgb_colors[1],rgb_colors[2]), whipe);
}
void getRGB(int hue, int sat, int val, int colors[3]) {
/* convert hue, saturation and brightness ( HSB/HSV ) to RGB
The dim_curve is used only on brightness/value and on saturation (inverted).
This looks the most natural.
*/
val = dim_curve[val];
sat = 255-dim_curve[255-sat];
int r;
int g;
int b;
int base;
if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
colors[0]=val;
colors[1]=val;
colors[2]=val;
} else {
base = ((255 - sat) * val)>>8;
switch(hue/60) {
case 0:
r = val;
g = (((val-base)*hue)/60)+base;
b = base;
break;
case 1:
r = (((val-base)*(60-(hue%60)))/60)+base;
g = val;
b = base;
break;
case 2:
r = base;
g = val;
b = (((val-base)*(hue%60))/60)+base;
break;
case 3:
r = base;
g = (((val-base)*(60-(hue%60)))/60)+base;
b = val;
break;
case 4:
r = (((val-base)*(hue%60))/60)+base;
g = base;
b = val;
break;
case 5:
r = val;
g = base;
b = (((val-base)*(60-(hue%60)))/60)+base;
break;
}
colors[0]=r;
colors[1]=g;
colors[2]=b;
}
}
void setColor(uint32_t color, boolean whipe) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
if(whipe){
delay(30);
}
}
}