app控制蓝牙连接的RGB
各位盆友你们好,最近做了个RGB的三色灯,调试的时候总是出点小差,故此,想拿上来和各位盆友讨论讨论:)希望大家不要喷哦,虽然东西垃圾了点:P
讨论点:为什么串口上面的数据会出错(不出错的话应该是0-255)——>因为用的是pwm波调的...?
为什么手机APP发送多组数据后,突然发送后串口上数据不变了(我估计是接收不到了)——>不知道蒙的对不对?
注:串口上面的数据详情看附件
附上一段代码吧--
char state;
int x, y , z ;
float q, w, e;
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
analogWrite(led1, 15);
analogWrite(led2, 15);
analogWrite(led3, 15);
analogWrite(led4, 15);
Serial.begin(9600);
}
void loop()
{
int j = 0;
int i = 0;
int mark = 0;
while (Serial.available() > 0) //串口获取可用数据
{
for (int i = 0; i < 10; i++)
{
state = char(Serial.read()); //把串口收到的数据寄存到state
delay(100);
}
delay(200);
x = (state - 0x30 ) * 100 + (state - 0x30 ) * 10 + (state - 0x30 ) * 1;
y = (state - 0x30 ) * 100 + (state - 0x30 ) * 10 + (state - 0x30 ) * 1;
z = (state - 0x30 ) * 100 + (state - 0x30 ) * 10 + (state - 0x30 ) * 1;
q = x * 2.55;
w = y * 2.55;
e = z * 2.55;
analogWrite(led1, q);
analogWrite(led2, w);
analogWrite(led3, e);
analogWrite(led4, 100);
Serial.print(" ");
Serial.print(q);
Serial.print(" ");
Serial.print(w);
Serial.print(" ");
Serial.println(e);
x = 0;
y = 0;
z = 0;
}
}
围观一下,这是你自己写的代码吗? luna 发表于 2016-8-15 14:25
围观一下,这是你自己写的代码吗?
。。。恩啊。。。。 学习学习 开始学习啦,哈哈~~~~~~
页:
[1]