上位机程式。
暗风帮我看看程式
本帖最后由 pww999 于 2014-1-22 17:16 编辑
int val
......
void loop(){
//read the signal from bt
if(mySerial.available()>0){
val=mySerial.read();
delay(200);
}
Serial.println(val);
analogWrite(pinLED,val);
}
要放在外面吧?
如果上位机自动发送复位0 时,接收端就要加变量保存大于0的当前值??
Serial.read()读1byte,上位送2bytes
1 byte最大127, 2 byte才能发送255大小的数据.
Belive是对的, 太感谢了! 我修改了上位机程序, 现在正常了. 下位机需要调整一下delay时间为10,以减少滞后,改为10调光比较平顺。谢谢大家的帮助!
本帖最后由 活着就是幸福 于 2014-1-23 09:45 编辑
调整后的程序#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);//Define RX,TX
int pinLED=11;
int val=0;
void setup(){
Serial.begin(9600); //define the bounds rate
mySerial.begin(9600); //define bt bounds rate
pinMode(pinLED,OUTPUT);
}
void loop(){
//read the signal from bt
while(mySerial.available()>0){
val=mySerial.read();//arduino received 1 byte each time.
Serial.println(val);
analogWrite(pinLED,val);
delay(10);
}
}
上位机程序:
有兴趣的哥们可以测试一下。
另外,拓展一下,怎样控制多盏LED呢?我试了一下,但是有个问题,Serial.read()接收不了大于255的数值, 如何识别来自不同slider的信号? 通过不同数值范围看来是行不通了。哪位大虾解答一下?
/* Android control several LEDs through BT*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);//Define RX,TX
int pinLED1=9;
int pinLED2=10;
int pinLED3=11;
int val=0;
void setup(){
Serial.begin(9600); //define the bounds rate
mySerial.begin(9600); //define bt bounds rate
pinMode(pinLED1,OUTPUT);
pinMode(pinLED2,OUTPUT);
pinMode(pinLED3,OUTPUT);
}
void loop(){
//read the signal from bt
while(mySerial.available()>0){
val=mySerial.read();//arduino received 1 byte each time.
if (0<=val<256){ //the LED1 will be controlled as the val between 0-255;
Serial.println(val);
analogWrite(pinLED1,val);
}else if(256<=val<512){ //LED2 will be controlled as the val between 255-510;
Serial.println(val);
analogWrite(pinLED2,val-255);
}else if(512<=val<767){
Serial.println(val);
analogWrite(pinLED3,val-512);
}
delay(10);
}
}
实际上val 不可能获取大于255的数值。所以以上方案不可行。有其它解决办法吗?
活着就是幸福 发表于 2014-1-23 12:08 static/image/common/back.gif
另外,拓展一下,怎样控制多盏LED呢?我试了一下,但是有个问题,Serial.read()接收不了大于255的数值, 如 ...
发送的数据加协议进去,比如下面这个链接的
http://www.geek-workshop.com/forum.php?mod=viewthread&tid=8415
没看明白, 我需要int格式数据.
没人解答, 只好自力更生, 尝试了几种方案, 终于可以了, 但是还是有个bug, 就是当slider快速滑动时, 输出的数据不正常, 不知道为什么. 估计是上位机的原因. 以下是控制三盏灯的程序: #include <SoftwareSerial.h>
/*
this sample tells us how to get the specific format demand from the Serial.
Letter+intNum+@ different letter leads to different command, and how to pick up
the intNum data from the string and then send it to the PWM pin.
initiated by Jerry Zhong Jan-27-14
*/
SoftwareSerial mySerial(2,3);
String comdata = "";
int numdata=0;
int pinLED1=12;
int pinLED2=11;
int pinLED3=10;
void setup(){
Serial.begin(9600);
mySerial.begin(9600);
pinMode(pinLED1,OUTPUT);
}
void loop(){
int i=0,j=1;
while(mySerial.available()>0){
comdata+=char(mySerial.read());
delay(2);
i++;
//Serial.println(comdata);
switch(comdata){
case 'a'://check if the first letter is 'a', if yes, get the PWM data
if(comdata=='@'){//to check if the string command is over
//Serial.println(comdata);
for (j=1;j<(comdata.length()-1);j++){
numdata=numdata*10+(comdata-'0');
}//transfer the character into integer number which could be recognized by analogWrite();
// Serial.println(j);
Serial.println(numdata); //print out the data to check if it's correct
analogWrite(pinLED1,numdata);//write the PWM data to the output pinLED.
comdata="";//clean out the varial
numdata=0;//clean out the numdata vairal
}
break;
case 'b':
if(comdata=='@'){
Serial.println(comdata);
for(j=1;j<(comdata.length()-1);j++){
numdata=numdata*10+(comdata-'0');
}
Serial.println(numdata);
analogWrite(pinLED2,numdata);
comdata="";
numdata=0;
}
break;
case 'c':
if(comdata=='@'){
Serial.println(comdata);
for(j=1;j<(comdata.length()-1);j++){
numdata=numdata*10+(comdata-'0');
}
Serial.println(numdata);
analogWrite(pinLED3,numdata);
comdata="";
numdata=0;
}
break;
//default:
//Serial.println(comdata);
}
}
}
点按slider运行正常, 输出的数据也是正常的. 快速滑动有时会出现非正常数据.
修订后的上位机程序: 希望有兴趣的帮忙测试一下, 发送数据格式是 '字母'+0~255+'@', 第一个字母代表不同的LED, '@' 表示字符串结束, 中间部分是调节亮度的值, 按照TEXT方式发送.
快速滑动输出的数据出现超出0-255范围
222
-9462
128
125
120
-25965
105
-10238
出现超出范围的数据时, 无法继续调节
过年在家,没办法帮你了,等回去给你验证一下,顺便给你简化一下